|
-
Apr 25th, 2005, 08:40 PM
#1
Thread Starter
Lively Member
migrating from VB.NET
hi all,
i migrated from VB.NET to C# about a week ago, and there are two things i cannot figure out how to do in C#. If anyone could help me out this would be much appreciated.
1. How to create a property (besides "this") with indexers. For example in VB:
VB Code:
Property MyProp(ByVal param1 As Integer, ByVal param2 As Integer) As Object
...
End Property
2. How to do a 'ReDim Preserve' in C# code.
VB Code:
Dim asdf(1023) As Byte
ReDim asdf(2047)
Thx in advance for any help.
-
Apr 25th, 2005, 10:08 PM
#2
Hyperactive Member
Re: migrating from VB.NET
dunno much this but try this one, because i'm not a good coder also.
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());
-
Apr 26th, 2005, 12:43 AM
#3
Frenzied Member
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
-
Apr 26th, 2005, 01:10 AM
#4
Sleep mode
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 ) just read the MSDN Help .
-
Apr 27th, 2005, 03:23 PM
#5
Thread Starter
Lively Member
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 ?
-
Apr 27th, 2005, 04:28 PM
#6
I wonder how many charact
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;
}
-
May 12th, 2005, 07:08 PM
#7
Thread Starter
Lively Member
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
-
May 20th, 2005, 12:37 AM
#8
Re: migrating from VB.NET
 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;
}
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|