Reputation: 831
public List<string[]> arrList = new List<string[]>();
public string[] ArrayOfstring = new string[9];
public void ubaciStavku(string Rb, string Tip, string Servis, string Izlaz,
string Status, string Komentar, string DeoSeBr, string RegBr, string flag)
{
StavkaTrebova StavkaTrebova = StavkaTrebova.instanciraj(); /* Singleton Pattern */
StavkaTrebova.prosledi(Rb, Tip, Servis, Izlaz, Status, Komentar, DeoSeBr, RegBr);
ArrayOfstring[0] = flag;
ArrayOfstring[1] = Rb;
ArrayOfstring[2] = Tip;
ArrayOfstring[3] = Servis;
ArrayOfstring[4] = Izlaz;
ArrayOfstring[5] = Status;
ArrayOfstring[6] = Komentar;
ArrayOfstring[7] = DeoSeBr;
ArrayOfstring[8] = RegBr;
arrList.Add(ArrayOfstring); // hire all values in arrays are the same
}
I was debugging this and as soon as values are passed to the ArrayOfstring all values are set to values before that insert
im coding this in ASP.NET values are passed from textbox in to the method that is passing values to method "ubaciStavku"
Anyone knows the answer why is this happening?
Upvotes: 1
Views: 86
Reputation: 101681
Because arrays are reference types and you are modifying the same array in each time.Instead you should create a new array and then add it to your list. You can simply move your declaration inside of your method:
string[] ArrayOfstring = new string[9];
ArrayOfstring[0] = flag;
ArrayOfstring[1] = Rb;
...
arrList.Add(ArrayOfstring);
Or you can use a shorthand:
arrList.Add(new [] { flag, Rb, Tip, Servis, Izlaz,
Status, Komentar, DeoSebr, RegBr });
Also you might want to use a custom type instead of an array of strings then have a List<YourCustomType>
.
Upvotes: 3