Reputation: 1888
I know this is a simple question, but it had been bothering me for a whole day long. I try to set the length of my array through a variable inside a method(setLength). But after i call the method setLength
, the length of my array still 0, doesn't it was set to 5 when i calling the method setLength
?
C# code snippet:
class myProject
{
public static int length = 0;
public static string[] myArray1 = new string[length];
public static string[] myArray2 = new string[length];
public static void setLength()
{
length = 5;
}
public static void process()
{
setLength();
for(int i=0; i<length; i++)
{
.....code for my array
}
}
Upvotes: 1
Views: 3021
Reputation: 54532
What you should do is not initalize the size of your array in the Class Declaration, initialize it in the setLength Method. The problem as the previous answers state is that once you initialize your array, it is an expensive operation to resize it. If this is going to be something that you are doing more than once you need to look into using a List<string>
.
class myProject
{
public static int length = 0;
public static string[] myArray1;
public static string[] myArray2;
public static void setLength(int value)
{
myArray1 = new string[value];
myArray2 = new string[value];
}
public static void process()
{
length = 5;
setLength(length);
for(int i=0; i<length; i++)
{
.....code for my array
}
}
}
Upvotes: 2
Reputation: 5571
Since myArray1
and myArray2
were both defined with a length of length
where length
was equal to 0
, their length will be always 0
unless you change it because the IDE compiles the project line by line.
However, if you are willing to change the array length, you may use Array.Resize<T>(ref T[] array, int newSize)
where array
is the array to resize and newSize
is the new length of the array.
Example
class myProject
{
public static int length = 0; //Initialize a new int of name length as 0
public static string[] myArray1 = new string[length]; //Initialize a new array of type string of name myArray1 as a new string array of length 0
public static string[] myArray2 = new string[length]; //Initialize a new array of type string of name myArray2 as a new string array of length 0
public static void setLength()
{
length = 5; //Set length to 5
}
public static void process()
{
setLength(); //Set length to 5
Array.Resize(ref myArray1, length); //Sets myArray1 length to 5
//Debug.WriteLine(myArray1.Length.ToString()); //Writes 5
}
Notice: In most cases, it's better to use System.Collections.Generic.List<T>
as it's easier to manage and dynamically sized.
Example
List<string> myList1 = new List<string>(); //Initializes a new List of string of name myList1
private void ListItems()
{
AddItem("myFirstItem"); //Calls AddItem to add an item to the list of name myFirstItem
AddItem("mySecondItem"); //Calls AddItem to add an item to the list of name mySecondItem
AddItem("myThirdItem"); //Calls AddItem to add an item to the list of name myThirdItem
string[] myArray1 = GetAllItemsAsArray(myList1); //Calls GetAllItemsAsArray to return a string array from myList1
/* foreach (string x in myArray1) //Get each string in myArray1 as x
{
MessageBox.Show(x); //Show x in a MessageBox
} */
}
private void RemoveItem(string name)
{
myList1.RemoveAt(myList1.IndexOf(name)); //Removes an item of name "name" from the list using its index
}
private void AddItem(string name)
{
myList1.Add(name); //Adds an item of name "name" to the list
}
private string[] GetAllItemsAsArray(List<string> list)
{
string[] myArray = new string[list.Count]; //Initialize a new string array of name myArray of length myList1.Count
for (int i = 0; i < list.Count; i++) //Initialize a new int of name i as 0. Continue only if i is less than myList1.Count. Increment i by 1 every time you continue
{
myArray[i] = list[i]; //Set the item index of i where i represents an int of myArray to the corresponding index of int in myList1
}
return myArray; //Return myArray
}
Thanks,
I hope you find this helpful :)
Upvotes: 3
Reputation: 415881
The size of the array is set using the value of your length variable at time your object is constructed. There is no variable relationship created in the array: the size is set once and is fixed thereafter.
If you want an array that changes sizes, use a collection type like List<string>.
Upvotes: 5
Reputation: 31204
You have to create a new array.
public static void setLength()
{
length = 5;
myArray1 = new string[length];
}
You can't just change the length at will. c# arrays don't support that.
If you want to use a variable-length structure, I'd suggest using a List<string>
Upvotes: 7