How can I Instantiate a dynamic array and still be dynamic after?

Hello,

I have a program which pulls config information in from a file. The files length will vary depending on how many items are configured in the application. the application stores the config information in an array while reading the file. since the file's length varies the array must be dynamic after instantiated. From all the information I have found I know I can prep a dynamic array but I cannot find any info on instantiating it dynamically. All the info shows is instantanting the dynamic array to a static one. anyone have information on keeping the array dynamic

Either that or is there a way to determine how many lines in the file are left from the current Stream position It would have to be in lines, not characters.

Thanks for any help.

Quilnux



Answer this question

How can I Instantiate a dynamic array and still be dynamic after?

  • e. ogas

    Unfortunately (say a few) C# does not have any way to make an array resizable in a manner similar to the VB.NET ReDim command, instead the recommended way is to either do the resizing on your own through the creation of a new array and copying all of the elements over to the new one or using a class that automatically does this work for you behind the scenes such as the List<> class (only available in 2.0) or the ArrayList class.

  • libra08

    Thanks; very helpful.

    For C#, you also need ref:

    Array.Resize(ref YourArray, NewSize);


  • akin_l

    Thanks. Works beautifully now.

    Quilnux


  • jitendra badkas

    While I agree with Brendan that the generic List is a better approach for you, .NET 2 (any language) does have a way to make an array resizable:

    Array.Resize(YourArray, NewSize)

    Although it is limited to one dimensional arrays.

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Clear VB: Cleans up VB code
    C# Code Metrics: Quick metrics for C#



  • How can I Instantiate a dynamic array and still be dynamic after?