8

I want to create a 2-dimensional array, without knowing the size of the first dimension.

For example I have an unknown number of rows, when I create an array. Every row represent an account. Exit 4 columns for every row: ID,name,user,password

I tried with jagged array but it is not possible to have:

int[][] jaggedArray = new int[][3];

Also I looked for ArrayList, implementation with clases and a little about Generics.

I'm searching for a solution that can permit easy manipulation of data as:

  • add to list,select,input elements
  • using elements in database queries
  • using as parameters in other functions

Because I'm a newbie in .NET (C#) please provide me with code solutions, instead of generic(look for) solutions

3 Answers 3

17

IMO, since the "columns" are fixed, declare a class for that:

public class Account
{
    public int ID {get;set;}
    public string Name {get;set;}
    public string User {get;set;}
    public string Password {get;set;} // you meant salted hash, right? ;p
}

now have a :

List<Account> list = new List<Account>();

this has everything you need:

add to list,select,input elements

list.Add etc

using elements in database queries using as parameters in other functions

vague without more info, but you can pass either the Account or invidual values, or the entire list.

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

6 Comments

thank you. I have another question: How do I recover the class members values for a specific list item ?
@user you mean like list[0].Name ?
My case has one minor difference. Both rows and columns are not known. How can I achieve the same result for 2-D Arrays?
@barnes then... var arr = new YourChosenType[len1, len2]; (could be int[,], string[,], etc) - or use something like DataTable - and to be clear: that isn't a "minor" difference
Thanks :) . Knew my "minor" is "major", I was being polite ;).
|
2

There is no such thing as dynamic length arrays in .NET. Use a List<> instead.

The array bounds all need to be known when you instantiate an array. What may have confused you is that this seems to be different for jagged arrays, but it's not: since it is an array of arrays, when you instantiate it it will be an array of uninstantiated arrays (e.g. null references). You then need to allocate each of those arrays again to use them.

Comments

0

As long as I know, we cant instantiate array without knowing its size. Why dont you try a Array of List? Like this:

List<int>[] a = new List<int>[yourDesireColumnNumber];

With List, add, select, input elements is trivial. If you want to give it as parameter in other functions, just define Type.

3 Comments

Don't you mean a list of fixed int array's, not a fixed array of list of int's?
@Scrum is right - if you chose this approach (and I wouldn't ;p) it would be a List<int[]>, not a List<int>[]
Your should try it before judge it. It's a Array of List<int>. By the way, Marc approach is great. ;) I learn from that.

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.