Sign in to follow this  
slayemin

How do I make dynamic C# arrays?

Recommended Posts

slayemin    6103
///working array but not enough flexible
string[,] ArrayDetails = new string[10,11];

///what I want:
string[,] ArrayDetails = new string[*,11];
I've been thrashing my brain and online msdn resources trying to figure out how to create a dynamically sized array with the first element being the dynamic size (*). Is there any fast way I can accomplish this in managed C# without getting into linked lists? Gracious Thanks!

Share this post


Link to post
Share on other sites
intrest86    742
That is definately 100% impossible, and I doubt you can find another language that supports that at least I've never heard of one). Here is what you can do, though: Make a one dimensional array, then use a small function to map the 2 dimensions to the one. It would look something like this:

int[] myArray = new int[width*height];

//To get [x,y] you instead do
myArray[y*width + x]

This way you can use whatever width and height you need when you create the array. If you want to be able to dynamically resize it though, it would be a bit more complicated. Is that what you are trying to do?

Share this post


Link to post
Share on other sites
joanusdmentia    1060
slayemin, you've got a couple of options here. First and probably easiest is to use intrest86's approach but using the ArrayList class (it's a dynamic array). Alternatively you could use ArrayList like this:

// Setup
int fixedSize = 11;
ArrayList ArrayDetails = new ArrayList();
ArrayDetails.Add(new object[fixedSize]);
ArrayDetails.Add(new object[fixedSize]);
ArrayDetails.Add(new object[fixedSize]);

// Modify an element
ArrayDetails[2][0] = "value";

// Increase the size of the first index
ArrayDetails.Add(new object[fixedSize]);

// Modify the new element
ArrayDetails[3][0] = "newvalue";

// Decrease the size of the first index
ArrayDetails.RemoveAt(3);




You can wrap this up in a class without much difficulty. If you want it to work specifically for strings you can use System.Collections.Specialized.StringCollection

[Edited by - joanusdmentia on June 29, 2005 3:41:43 AM]

Share this post


Link to post
Share on other sites
intrest86    742
Quote:
Original post by joanusdmentia
slayemin, you've got a couple of options here. First and probably easiest is to use intrest86's approach but using the ArrayList class (it's a dynamic array). Alternatively you could use ArrayList like this:
*** Source Snippet Removed ***

You can wrap this up in a class without much difficulty. If you want it to work specifically for strings you can use

Yeah, the only reason I didn't suggest using ArrayLists is because saying they are resizable makes it seem like they will be correctly resized automatically. Just make sure that if you do use ArrayLists and then resize the array you need to go through and move the information so that the index mapping is still correct. If you don't all of your information will be shifted to lower positions then it should be, and it just wouldn't be pretty.

If you want to resize it regularly, you might want to look for another solution, because I imagine that moving so much memory with every resize might be an amazing drain on resources.

Share this post


Link to post
Share on other sites
slayemin    6103
Thank you very much for the help!
The ArrayList was what I was looking for, and using it is easier then trying to create my own linked list datastructure. I love how ArrayList doesn't care what type of information you give it (I <3 C# now).

I had a database query which does a select * from a table and the number of rows I could receive was a variable number, so here is the coded solution I came up with:


/// <summary>
/// Accesses the data-layer and retrieves all permits for a student or employee,
/// returns the data in an array list with each array list element of size 11.
/// </summary>
/// <param name="SID_EID">Get the student/employee Unique ID</param>
/// <returns>Array list containing all rows of data</returns>
public ArrayList GetPermitDetails(string SID_EID)
{
//create and initialize local variables
Data.Permit Permit = new Data.Permit();
int TotalPermits = 0;
ArrayList PermitList = new ArrayList();

//Get all the permits for this customer and the number of permits that they have
DataSet DataGrid = Permit.GetPermitDetails(SID_EID, out TotalPermits);

//Go through each permit and add its value to the local array
for(int a=0;a<TotalPermits;a++)
{
string[] PermitDetails = new string[11];

//Get the colums and store them into a string array
if(!DataGrid.Tables["Information"].Rows[a].IsNull("Permit_Id")) {PermitDetails[0] = DataGrid.Tables["Information"].Rows[a]["Permit_Id"].ToString();}
if(!DataGrid.Tables["Information"].Rows[a].IsNull("Permit_No")) {PermitDetails[1] = DataGrid.Tables["Information"].Rows[a]["Permit_No"].ToString();}
if(!DataGrid.Tables["Information"].Rows[a].IsNull("Customer_Id")) {PermitDetails[2] = DataGrid.Tables["Information"].Rows[a]["Customer_Id"].ToString();}
if(!DataGrid.Tables["Information"].Rows[a].IsNull("Permit_Type")) {PermitDetails[3] = DataGrid.Tables["Information"].Rows[a]["Permit_Type"].ToString();}
if(!DataGrid.Tables["Information"].Rows[a].IsNull("Payment_Amount")) {PermitDetails[4] = DataGrid.Tables["Information"].Rows[a]["Payment_Amount"].ToString();}
if(!DataGrid.Tables["Information"].Rows[a].IsNull("Payment_Date")) {PermitDetails[5] = DataGrid.Tables["Information"].Rows[a]["Payment_Date"].ToString();}
if(!DataGrid.Tables["Information"].Rows[a].IsNull("Payment_Method")) {PermitDetails[6] = DataGrid.Tables["Information"].Rows[a]["Payment_Method"].ToString();}
if(!DataGrid.Tables["Information"].Rows[a].IsNull("Transaction_No")) {PermitDetails[7] = DataGrid.Tables["Information"].Rows[a]["Transaction_No"].ToString();}
if(!DataGrid.Tables["Information"].Rows[a].IsNull("Active_Quarter")) {PermitDetails[8] = DataGrid.Tables["Information"].Rows[a]["Active_Quarter"].ToString();}
if(!DataGrid.Tables["Information"].Rows[a].IsNull("Permit_Type_Description")) {PermitDetails[9] = DataGrid.Tables["Information"].Rows[a]["Permit_Type_Description"].ToString();}
if(!DataGrid.Tables["Information"].Rows[a].IsNull("Permit_Price")) {PermitDetails[10]= DataGrid.Tables["Information"].Rows[a]["Permit_Price"].ToString();}

//store the string array as a row in the array list
PermitList.Add(PermitDetails);
}

return PermitList;
}



Hope this helps someone else in the future.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this