CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2010
    Posts
    1

    How to: create a array with undetermined length?

    hi guys!
    i'm trying to make a app on Visual Studio but i have a question ...
    I want to create a window in which the user enters a value and he can press two buttons: In one, ends action and other keep asking for the user insert more values.
    My only problem is that I need to create an array with unspecified size to go for storing whatever how much data the User you want to add.


    var data_x = new int[quanti_dados];
    var data_y = new int[quanti_dados];
    var times = 0;
    quanti_simu = Convert.ToInt32(textBoxSimulacoes.Text);
    do
    {
    var textodados1 = "Digite o primeiro dado para a " + (times + 1) + "ª coluna.";
    var textodados2 = "Digite o segundo dado para a " + (times + 1) + "ª coluna.";
    var formdados = new Dados();
    formdados.labelInserirDadosX.Text = textodados1;
    formdados.labelInserirDadosY.Text = textodados2;
    formdados.ShowDialog(this);
    dados_x[times] = Convert.ToInt32(formdados.textBoxInserirDadosX.Text);
    dados_y[times] = Convert.ToInt32(formdados.textBoxInserirDadosY.Text);
    }while (DialogResult == DialogResult.OK);


    I want the variables data_Y and data_X to have an indefinite or infinite size, so I can go adding values to without have to declare its size.

    thanks a lot all of u

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: How to: create a array with undetermined length?

    If you are always adding to the end of the 'array', then use List<Int32> instead.

    Not quite sure how you would do it if say the array was currently 10 elements in size and you added one at position 50. What would you expect to be in elements 10-49?

    You could wrap the List<Int32> in your own class, and by using indexes, you could make your class look like an array from the outside, so, in the previous case, when you add an element at position 50, your class takes care of initialising elements 10-49 internally, transparent to the function adding the element.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    Mar 2007
    Posts
    59

    Re: How to: create a array with undetermined length?

    you can also use a Dictionary<int, int> and create a method to get the values in the proper order if you need it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured