Array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aleksey

    Array

    Hi All!

    Does anybody know how can I change length of existing array. In Delphi I
    used dynamic array and procedure SetLength. Is any similar procedure in C#.
    For instance, I have array with 100 elements, and I need change its lenght
    to 20 without lost data in it.

    Thanks a lot


  • Mattias Sjögren

    #2
    Re: Array

    >Does anybody know how can I change length of existing array. In Delphi I[color=blue]
    >used dynamic array and procedure SetLength. Is any similar procedure in C#.
    >For instance, I have array with 100 elements, and I need change its lenght
    >to 20 without lost data in it.[/color]

    You can't change the length of an array in C#. You may want to use an
    ArrayList instead.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • David Anton

      #3
      RE: Array

      There is an equivalent - i.e., similar code to what VB is doing behind the
      scenes and similar performance (produced with our Instant C# VB to C#
      converter):
      e.g.,
      VB:
      Dim YourArray() As Integer
      ....
      ReDim Preserve YourArray(i)

      C#:
      int[] YourArray;
      ....
      int[] temp = new int[i + 1];
      if (YourArray != null)
      Array.Copy(Your Array, temp, Math.Min(YourAr ray.Length, temp.Length));
      YourArray = temp;

      As the other poster said, ArrayList will very often be a better solution,
      but at least you have the equivalent now.

      --
      David Anton
      Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

      Home of:
      Instant C#: VB.NET to C# Converter
      Instant VB: C# to VB.NET Converter
      Instant J#: VB.NET to J# Converter


      "Aleksey" wrote:
      [color=blue]
      > Hi All!
      >
      > Does anybody know how can I change length of existing array. In Delphi I
      > used dynamic array and procedure SetLength. Is any similar procedure in C#.
      > For instance, I have array with 100 elements, and I need change its lenght
      > to 20 without lost data in it.
      >
      > Thanks a lot
      >
      >
      >[/color]

      Comment

      • Chad Z. Hower aka Kudzu

        #4
        Re: Array

        "Aleksey" <alexey.timonin @il.quest.com> wrote in
        news:ukPrD2siFH A.1204@TK2MSFTN GP12.phx.gbl:[color=blue]
        > Does anybody know how can I change length of existing array. In Delphi
        > I used dynamic array and procedure SetLength. Is any similar procedure
        > in C#. For instance, I have array with 100 elements, and I need change
        > its lenght to 20 without lost data in it.[/color]

        Be careful if using this in Delphi.NET. It supports it for compatibility, but it actually creates a new
        array and copies the old one into it on .NET.


        --
        Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
        "Programmin g is an art form that fights back"

        Get your ASP.NET in gear with IntraWeb!

        Comment

        Working...