-1

I need to create several 2D arrays of mixed datatypes in C#. The arrays are actually coming from a database and I need to be able to store that data in 2D arrays that will take mixed datatypes. I saw a similar question here: Defining two dimensional Dynamic Array with different types, and tried what was suggested by creating a class of the datatypes I'll be using for the project and creating an array of the class I made but doing so gave me errors.

Errors :

CS0029 Cannot implicitly convert type 'Client.Aggregate.Arrays' to 'int'

CS0029 Cannot implicitly convert type 'Client.Aggregate.Arrays' to 'System.DateTime'

CS0029 Cannot implicitly convert type 'Client.Aggregate.Arrays' to 'string'

and the vice versa:

CS0029 Cannot implicitly convert type 'int' to 'Client.Aggregate.Arrays'

CS0029 Cannot implicitly convert type 'System.DateTime' to 'Client.Aggregate.Arrays'

and so on...

public class Arrays {
        public string String { get; set; }
        public char C { get; set; }
        public int Int { get; set; }
        public double Double { get; set; }
        public DateTime DateTime { get; set; }
    }
void Calculation(Arrays[,] SupArray, Arrays[,] Source)
    {
                DateTime ValidDate = Source[2, 2];
        string LineName = SupArray[4, 10];
        string LineModel = SupArray[6, 71];


//Here's some sample code you can use to test/see the other errors I'm getting. 
//It throws an error whether I go from standard data type to Arrays or Arrays to standard data type.

                Arrays[,] DArray = new Arrays[50, 50];
                DArray[1, 3] = "DMG";
                DArray[1, 2] = "Y"; 
                DArray[1, 4] = 4126;
                DArray[1, 1] = new DateTime(2019, 12, 30);

}
//This is a simplified version of my code. I have quite a few arrays I need to make
//capable of storing mixed data types so ideally solutions should be around 
//how to do this with arrays (not lists or dictionaries) as arrays are the only thing
//that really works for my project. 
Zay
  • 11
  • 4
  • 1
    Quite unclear what you hope to gain by having array of mixed types... Consider to [edit] question with your final goal. So far it looks like `dynamic`, `object` or some hacky [union](https://stackoverflow.com/questions/63258596/option-for-union-datatype-in-c-sharp) are obvious (not-exactly-type-safe) options - you may want to clarify why those did not work and in what way you hope to achieve type safety. – Alexei Levenkov Feb 08 '23 at 21:38
  • Essentially the data coming from the datasource is a table of mixed datatypes. Dates, Model names, utilization and other numerical values, etc. So I needed to store that mixed data in a single array without having to pull the table apart into its individual datatypes to do so. I figured there'd be a way to do this. That being said, the answer below has helped clarify where I went wrong so I'll take the approach Cetin suggested. – Zay Feb 09 '23 at 15:42

1 Answers1

0

That wouldn't even compile. You are declaring a 2 dimensional array where all members are of type Arrays. Then you are trying to assign string, int, DateTime to its members. Each member of that array should be of type of Arrays. Your code more should be like this (not saying you should write it like this, just trying to show where you went wrong):

void Calculation(Arrays[,] SupArray, Arrays[,] Source)
{
    Arrays[,] DArray = new Arrays[50, 50];
    DArray[0, 0] = new Arrays {String="A String", C='C', Double=1.2, Int=6, DateTime=DateTime.Now};


    DateTime ValidDate = Source[2, 2].DateTime;
    string LineName = SupArray[4, 10].String;
    string LineModel = SupArray[6, 71].String;
}

And a little bit curiosity:

"how to do this with arrays (not lists or dictionaries) as arrays are the only thing that really works for my project"

Why is that? It looks like you have a misperception about them. You seem to be working with data and works well with Lists, Dictionaries where it is appropriate.

Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39
  • To answer your curiosity, this project is a conversion from VBA to C#. Because its built up of arrays and I still have a ways to go before the program is completely converted to its C# equivalent, I'm hesitant to make any kind of sweeping change like changing out all the arrays for lists or dictionaries. I worry about the impact it'll have down the line or during optimization when I go in to make improvements after the conversion is complete. – Zay Feb 09 '23 at 14:55
  • @Zay, so it was just a personal preference. I can understand that. On the long run, remember, lists might be easier to handle these. – Cetin Basoz Feb 09 '23 at 15:14