C++ Equivalent to VB ReDim Preserve





























C++ Equivalent to VB ReDim Preserve

Resizing arrays while preserving the existing contents is easily done in VB using ReDim Preserve.  In C++/CLI, you have other alternatives:

  • Use one of the existing .NET collections, such as ArrayList or the generic List, that handle dynamic resizing automatically.
  • If the Array has only one dimension then you can use the Array.Resize method.
  • For multi-dimensional arrays, use the same logic that VB executes behind the scenes for a ReDim Preserve:

VB:
ReDim Preserve YourArray(1, 2)

C# (assuming an integer array):
array<int, 2> ^tempReDim1 = gcnew array<int, 2>(2,3);
if (YourArray != nullptr)
{
    for (int Dim0 = 0; Dim0 < YourArray->GetLength(0); Dim0++)
    {
        int CopyLength = System::Math::Min(YourArray->GetLength(1), tempReDim1->GetLength(1));
        for (int Dim1 = 0; Dim1 < CopyLength; Dim1++)
        {
            tempReDim1[Dim0, Dim1] = YourArray[Dim0, Dim1];
        }
    }
}

If you need to convert from VB.NET to C++/CLI and you are depending on the results being reliable and accurate, then you will want to have Instant C++ VB Edition, the best VB.NET to C++ converter, at your fingertips.

 

 
 
Email your questions and comments to:  info@tangiblesoftwaresolutions.com

[Site Map]        [Home]


Copyright © 1997 - 2009 Tangible Software Solutions Inc.