Brett
Brett

Reputation: 4061

Build and use generic list?

Can anyone help me figure out how I could simplify this code, without using an ArrayList?

List<Pair> material = new List<Pair>();
List<string> list = new List<string>();

string tempStr;
int integer = -1;
foreach (string s in ((string)Value).Split(new char[1] { ',' }))
{
  if (int.TryParse(s, out integer))
  {
     tempStr = NameValue.AllKeys[integer];

     if (someCondition == true)
     {
        material.Add(new Pair(tempStr, integer));
     }
     else
     {
        list.Add(tempStr);
     }
  }
}
if(someCondition == true)
{
  return material.ExtensionMethodForLists();
}
else
{
  return list.ExtensionMethodForLists();
}

When I've tried something like (below) I get an error for not initializing an implicityly-typed variable.

var list;
if(someCondition == true)
{
  list = new List<Pair>();
}
else
{
  list = new List<string>();
}

Upvotes: 2

Views: 578

Answers (4)

Tanzelax
Tanzelax

Reputation: 5714

Don't know if this actually simplifies, but something like:

    public DataTable ParseToTable(string Value)
    {
        if (someCondition)
            return ParseInternal<Pair>(Value, (s, i) => new Pair(s, i));
        else
            return ParseInternal<string>(Value, (s, i) => s);
    }

    private DataTable ParseInternal<T>(string Value, Func<string,int,T> newItem)
    {
        List<T> list = new List<T>();

        string tempStr;
        int integer = -1;
        foreach (string s in ((string)Value).Split(new char[1] { ',' }))
        {
            if (int.TryParse(s, out integer))
            {
                tempStr = NameValue.AllKeys[integer];
                list.Add(newItem(tempStr, integer));
            }
        }
        return list.ExtensionMethodForLists();
    }

Upvotes: 1

Paul Sasik
Paul Sasik

Reputation: 81429

Something like that would in Python or some other dynamically typed language. Though if you are using .net 4.0 you could use the dynamic keyword instead of var.

But, you're using C# which is meant to be strongly typed and the dynamic keyword is really meant to interface with dynamically typed languages and if the type system is getting in your way, you should reconsider your design. Also, if you do really need to manage two types of collections like that you should wrap it in a class and hide such gory details from the client code.

Upvotes: 0

IAbstract
IAbstract

Reputation: 19881

If you are going to use var you must assign the value immediately. Otherwise, as @Marc suggests is perfect... (well, as he says - satisfies the requirement)

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062600

If you are using different types, you would need to use a non-generic type for the variable:

IList list;
if(someCondition == true)
{
  list = new List<Pair>();
}
else
{
  list = new List<string>();
}

or

IList list = someCondition ? (IList)new List<Pair>() : new List<string>();

Personally, I'm not sure it is a great design, but it satisfies the requirement.

Upvotes: 6

Related Questions