mshvw

Hello,

I can't find a good solution for creating and filling an array like filling a table with records where each record has the same structure but has different variable types like each record containing an 'int' and a 'string' and a 'Panel object'

//I used a structure like...

struct stuRecord{

private int i;

private string s;

private Panel pnl;

//constructor

stuRecord('args')(

//store args in privates

)

}

//...and an array of myRecord objects...

stuRecord[] myTable = new stuRecord[12];

//... each filling with a record...

myTable[0] = new stuRecord(123,"test1",myPanelObject1);

myTable[1] = new stuRecord(456,"test2",myPanelObject2);

I also used an ArrayList to store these structure objects but both methods somehow 'dont feel good'...

I feel that there should be a nicer solution.

All tips welcome,

regards,

Henk



Re: Visual C# General How to fill an array with different variable types?

timvw

I would start with discovering the properties that all the types share and wrap these in an interface... (And then make sure that each of my types implements this interface)

eg:

Code Snippet

interface ICommonData
{
int Id { get; }
string Name { get; set; }
}

class Person : ICommonData
{
private string name;

public int Id { get { return 1; } }
public string Name { get { return this.name; } set { this.name = value; } }
}



And then i would probably choose to store all these in a generic list:

Code Snippet

List<ICommonData> commonData = new List<ICommonData>();
commonData.Add(new Person));
commonData.Add(new Car());
...







Re: Visual C# General How to fill an array with different variable types?

Wole Ogunremi

mshvw wrote:

Hello,

I can't find a good solution for creating and filling an array like filling a table with records where each record has the same structure but has different variable types like each record containing an 'int' and a 'string' and a 'Panel object'

//I used a structure like...

struct stuRecord{

private int i;

private string s;

private Panel pnl;

//constructor

stuRecord('args')(

//store args in privates

)

}

//...and an array of myRecord objects...

stuRecord[] myTable = new stuRecord[12];

//... each filling with a record...

myTable[0] = new stuRecord(123,"test1",myPanelObject1);

myTable[1] = new stuRecord(456,"test2",myPanelObject2);

I also used an ArrayList to store these structure objects but both methods somehow 'dont feel good'...

I feel that there should be a nicer solution.

All tips welcome,

regards,

Henk


I don't see anything bad with your solution. The only improvement i would suggest, similar to Tim's suggestion is to use a generic list instead of an array or arraylist, ie
List<stuRecord> myTable = new List<stuRecord>();
myTable.Add(new stuRecord
(123,"test1",myPanelObject1));
etc...




Re: Visual C# General How to fill an array with different variable types?

mshvw

Thanks both of you!