Results 1 to 8 of 8

Thread: migrating from VB.NET

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    84

    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:
    1. Property MyProp(ByVal param1 As Integer, ByVal param2 As Integer) As Object
    2.     ...
    3. End Property

    2. How to do a 'ReDim Preserve' in C# code.
    VB Code:
    1. Dim asdf(1023) As Byte
    2. ReDim asdf(2047)

    Thx in advance for any help.

  2. #2
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: migrating from VB.NET

    dunno much this but try this one, because i'm not a good coder also.
    VB Code:
    1. int[] tempArray = new int[]{1,2,3};
    2.             int newsize = tempArray.Length;
    3.             int[] i = new int[newsize];
    4.             'redim preserve the value of tempArray to i.
    5.             System.Array.Copy(tempArray,i,tempArray.Length);
    6.             for(int j=0;j<i.Length;j++)
    7.                 MessageBox.Show(i[j].ToString());

  3. #3
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    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

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    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 .

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    84

    Question 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 ?

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    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; 
    }

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    84

    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

  8. #8
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    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; 
    }
    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
  •  



Click Here to Expand Forum to Full Width