Reputation: 331
hi i have two string of arrays. But i can't figure out how to add the some of the value from an array into another.
here is my code.
string result[];
string[] hidden;
for (var cek = 1; cek < st.Count(); cek++)
{
hidden[cek] = result[cek];
}
the first string has already filled with values. and i want to add some values from result into hidden. but why does it says use of unassigned local variable?
is there some mistake or should i use other methods rather than this?
EDIT 1 i cannot determine the length of hidden array because it's not always the same, the length is determine by the input words from a textbox that can be count from st.count()
Upvotes: 0
Views: 104
Reputation: 3888
Well th answer is easy string[] hidden=new string[st.count()];
and if you want to resize the hidden
array you should try this Array.Resize(ref hidden,NewLenght);
.
And you keep getting the error : use of unassigned local variable because result is empty.
Upvotes: 0
Reputation: 700362
The reason for the error message is that you have only created a reference for the array, not the array itself. Use the new
keyword to create the array.
There are two more problems with the code:
Length
property of the array. The Count
method will loop through the array to find out how many items there are, so that's very inefficient.:
// input: string result[]
string[] hidden = new string[result.Length];
for (int cek = 0; cek < result.Length; cek++) {
hidden[cek] = result[cek];
}
You can also use the CopyTo
method for this:
string[] hidden = new string[result.Length];
result.CopyTo(hidden, 0);
Upvotes: 0
Reputation: 43046
"use of unassigned local variable" because you have not assigned a value to hidden
. You must initialize it:
int elementCountOfHiddenArray = // something
string[] hidden = new string[elementCountOfHiddenArray];
Upvotes: 0
Reputation: 547
You have to assign your hidden array :
string[] hidden = new String[100]; //write length of your array
Upvotes: 0
Reputation: 4891
For a start, your variable names are terrible. You use the same variable - cek - inside the for loop and outside. This makes it really hard to see what you mean. What do you want this line to do :
string result[cek];
Secondly, you haven't given hidden a size so it's unassigned. I presume this is where you are getting the exception.
Thirdly, what do you want hidden to include when the loop is finished? If you can sort this out I think LINQ might definitely help you to work with the arrays.
Upvotes: 1