Re: migrating from VB.NET
dunno much this but try this one, because i'm not a good coder also. :bigyello:
VB Code:
int[] tempArray = new int[]{1,2,3};
int newsize = tempArray.Length;
int[] i = new int[newsize];
'redim preserve the value of tempArray to i.
System.Array.Copy(tempArray,i,tempArray.Length);
for(int j=0;j<i.Length;j++)
MessageBox.Show(i[j].ToString());
Re: migrating from VB.NET
No Redim in C#..Just read it last week, so talking from the top of my head.
Got to copy to a new array with the new size or something like that
Re: migrating from VB.NET
To redim an array , just redefine its total number of elements like this :
Code:
Byte[] b=new byte[10];
MessageBox.Show("Before =" + b.Length.ToString());
//Redim the array
b=new byte[30];
MessageBox.Show("After =" + b.Length.ToString());
For the Indexer question , see how it's done (from MSDN):
Code:
using System;
class IndexerClass
{
private int [] myArray = new int[100];
public int this [int index] // Indexer declaration
{
get
{
// Check the index limits.
if (index < 0 || index >= 100)
return 0;
else
return myArray[index];
}
set
{
if (!(index < 0 || index >= 100))
myArray[index] = value;
}
}
}
//Usage
public class MainClass
{
public static void Main()
{
IndexerClass b = new IndexerClass();
// Call the indexer to initialize the elements #3 and #5.
b[3] = 256;
b[5] = 1024;
for (int i=0; i<=10; i++)
{
Console.WriteLine("Element #{0} = {1}", i, b[i]);
}
}
}
If you don't understand (I'm a bad teacher :rolleyes: ) just read the MSDN Help .
Re: migrating from VB.NET
about the indexers, i can create one using 'this', but is it possible to create one using a identifer (i.e. public int MyProp[int param1, string param2] {get {...) ? So you can use IndexerClass.MyProp[2,"str"] = 5 ?
Re: migrating from VB.NET
Here's a helpful method that simulates Redim Preserve in VB for a byte array.
Code:
private static byte[] Redim(byte[] OriginalArray, int DesiredSize)
{
byte[] DestinationArray = new byte[DesiredSize];
Array.Copy(OriginalArray, DestinationArray, DesiredSize-1);
return DestinationArray;
}
Re: migrating from VB.NET
hi all, thanks for your help.
i would like to share something i accidentally stumbled on about this problem: calls to ReDim Preserve are actually calls to the Microsoft.VisualBasic namespace so naturally not supported in C#. If you would like to see more details i made a post in the VB.NET forums. The link is http://www.vbforums.com/showthread.p...13#post2011613
btw thanks all for your help with this
Re: migrating from VB.NET
Quote:
Originally Posted by nemaroller
Here's a helpful method that simulates Redim Preserve in VB for a byte array.
Code:
private static byte[] Redim(byte[] OriginalArray, int DesiredSize)
{
byte[] DestinationArray = new byte[DesiredSize];
Array.Copy(OriginalArray, DestinationArray, DesiredSize-1);
return DestinationArray;
}
Couldnt that be modified to work with all arrays...
eg..
Code:
private static Array Redim(Array OriginalArray, int DesiredSize)
{
Array DestinationArray = new Array[DesiredSize];
Array.Copy(OriginalArray, DestinationArray, DesiredSize-1);
return DestinationArray;
}