0

I've got a function which takes a ArrayList and loads it with strings along the line of

void func( ref ArrayList data )
{
    if( data[0].GetType() == typeof(String) )
        data[0] = "Jimmy";
}

In the function that calls func() I am having to create strings to put in the array:

ArrayList data = new ArrayList(1);
string str = "";
data.Add(str);

Is it possible to give the ArrayList the object types without having to create an object of that type? This:

ArrayList data = new ArrayList(1);
string str;
data.Add(str);

Gives a "Use of unassigned local variable 'str'" error.

@James and Guffa: Thanks for the 'stylistic' hints, I'm new to c# and the advice is much appreciated

1
  • 1
    It is less verbose to use is rather than `GetType() == typeof(String). Commented Aug 26, 2010 at 11:16

4 Answers 4

3

No, that is not possible. What you want is a reference that points to a string, and that is only possible if it actually points to a string. The fact that the variable that holds the reference is declared as a string reference doesn't matter once you have copied the reference from the variable to the list.

I think that you should rethink the entire concept. I don't know your reason for sending in a list of values and replace them like that, but there has to be a more object oriented way of doing it.

The ArrayList class is practically obsolete, you should use a generic List<T> instead (even if you can't find any other common base class than object).

As you are always sending in a list object to the method and not replacing it with a new list object, you should not use the ref keyword. That's only for when you want to change the reference to the list, not the contents of the list.

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers Guffa. I'm doing it this way as func extracts data from a comms link but each message has different data types in different orders. The ArrayList seemed like a good way of telling func what data types were where.
0

You can send two parameters, one ref ArrayList in which members will be assigned and the other an ArrayList and the called function can assign at indexes in data where the type is String

Comments

0

If you are only wanting to keep this type safe then could you not use a List<T> class here instead e.g. List<string>?

Comments

0

What you're trying to do in your method with the above code is to call GetType() on null, which is not possible.

Why not use a generic list List<string> in this case? You can elegantly skip the part of using GetType() to determine the type with this approach:

static void func(List<string> data)
{
    if(data.Count > 0)
        data[0] = "Jimmy";
}

static void Main(string[] args)
{
    List<string> lst = new List<string>(1);

    string str = "";

    lst.Add(str);

    func(lst);

    System.Console.WriteLine(lst[0]); //Prints out 'Jimmy'
}

1 Comment

Sorry, not a perfect question, the list contains different data types

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.