1
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?

1 Answer 1

3

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>.

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

1 Comment

I wasn't thinking about that you are wright, thank you very much.

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.