9

I want to create two dimension array of different type like I can add to that array two values one of them is controlname and second is boolean value.

2
  • I'm not sure I understand your question, but won't a Dictionary work for what you trying to do? Are you trying to store Key/Value pairs? Commented Dec 21, 2009 at 10:58
  • Using a list with tuples public static List<Tuple<string, Point>> pixelsArr = new List<Tuple<string, Point>>(); Commented May 28, 2018 at 21:28

9 Answers 9

15

You can't do that. Instead, you should create a class that contains these two properties, then you can create an array of that type:

public class MyClass
{
    public string ControlName {get;set;}
    public bool MyBooleanValue {get;set;}
}

public MyClass[] myValues=new MyClass[numberOfItems];

Or, as Anders says, use a dictionary if one of the properties is meant to be used to perform lookups.

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

Comments

5

A dictionary will work for what you are trying to do then.

Dictionary<string, bool> controllerDictionary = new Dictionary<string, bool>();

To set a value

if (controllerDictionary.ContainsKey(controllerName))
    controllerDictionary[controllerName] = newValue;
else
    controllerDictionary.Add(controllerName, newValue);

To get a value

if (controllerDictionary.ContainsKey(controllerName))
    return controllerDictionary[controllerName];
else
    //return default or throw exception

2 Comments

You can just use controllerDictionary[controllerName] = newValue;, the ContainsKey and Add aren't required
Ye, that's true, it's only needed for the the get
4

You can't to that with an array.

Perhaps you should be using a Dictionary?

A generic dictionary of Dictionary<string,bool> appears to be the kind of thing that will work for your description.

Comments

2

It depends on how you want to use your array. Do you want to look up the value by a key, or by its index? Konamiman suggested a class. But a class with two types is nothing more than a Dictionary<type of key, type of value>. You can use a dictionary if you want to obtain the value by a key. Like so:

Dictionary<string, int> MyDict = new Dictionary<string, int>();
MyDict.Add("Brutus", 16);
MyDict.Add("Angelina", 22);
int AgeOfAngelina = MyDict["Angelina"];

Now the disadvantage of a dictionary is that, you can't iterate over it. The order is undetermined. You can not use MyDict[0].Value to obtain the age of Brutus (which is 16).

You could use a

List<KeyValuePair<string, int>> MyList = new List<KeyValuePair<string, int>>();

to iterate through a 2D array of two different types as a List supports iteration. But then again, you cannot obtain the age of Angelina by MyList["Angelina"].Value but you would have to use MyList[0].Value.

But you could use a datatable as well. But it requires somewhat more work to initialize the table with its columns.

2 Comments

Of course you can extend with LINQ to use queries on your data.
Since when can't you iterate over a dictionary?
1

If you want to lookup/set a boolean by control name, you could use a Dictionary<string, bool>.

Comments

1

Use Dictionary<string,bool>. If, for some reason, you really need an array, try object[,] and cast its values to the types you want.

Comments

1

Another way of doing it is to create an array of type object, and then add this to an arraylist. Here is some sample code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    using System.Collections.Generic;

    namespace Collections
    {
        class Program
        {
            static void Main(string[] args)
            {
                ArrayList ar = new ArrayList();
                object[] o = new object[3];
                // Add 10 items to arraylist
                for (int i = 0; i < 10; i++)
                {
                    // Create some sample data to add to array of objects of different types.
                    Random r = new Random();
                    o[0] = r.Next(1, 100);
                    o[1] = "a" + r.Next(1,100).ToString();
                    o[2] = r.Next(1,100);
                    ar.Add(o);
                }
            }
        }
    }

Comments

0

"A multi-dimensional array is an array: all elementsin all dimensions have the same type"

Comments

0

Actually you can do it with a List. I suggest you take a look at Tuples.
How to easily initialize a list of Tuples?

You can use a list of type object too if you want

Like so :
List<List<object>> Dates = new List<List<object>>() { };
Dates.Add(new List<object>() { 0 ,"January 1st" });

Comments

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.