Results 1 to 9 of 9

Thread: [RESOLVED] generic ReDim function in C#

  1. #1

    Thread Starter
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Resolved [RESOLVED] generic ReDim function in C#

    is it possible to create a generic ReDim function in C# that will take in any data type? creating a ReDim function for a single data type is easy, but handling any data type seems to be confusing.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: generic ReDim function in C#

    I assume you are concerned about the "Preserve" keyword, because you don't need a ReDim function if you don't want to preserve. Just assign a new Array object to your variable:
    Code:
    int[] myArray = new int[10];
    
    myArray = new int[20];
    Also, the Array.Copy method can achive what ReDim Preserve does in just a few lines anyway:
    Code:
    int[] myArray = new int[5];
    
    int[] tempArray = new int[10];
    Array.Copy(myArray, 0, tempArray, 0, Math.Min(myArray.Length, tempArray.Length));
    myArray = tempArray;
    I'm not sure you can do it any other way, because you cannot convert back and forth between an Array object and an array of a particular type of objects.

  3. #3

    Thread Starter
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: generic ReDim function in C#

    i guess it would have to be a separate function for each type of array.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  4. #4
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: generic ReDim function in C#

    It sounds like you want templates. Something I don't think C# has. But you might be able to pull it off using Interface, but it depends on what you want to do. But templates would be the best.


    - ØØ -

  5. #5
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: generic ReDim function in C#

    Or you also might be able to do it with the Object class, since after all, in C# EVERY object is derived somehow from Object.


    Code:
    using System;
    
    class MainClass{
    
    	public static void Main(string[] args){
    	
            Object[] array = new Object[5];
            
            for (int i = 0; i < 5; i++){
                array[i] = i;       
            }	
            
            Object[] tempArray = new Object[10];
    
            Array.Copy(array, 0, tempArray, 0, Math.Min(array.Length, tempArray.Length));
            array = tempArray;
    		
            for (int i = 0; i < 10; i++){
                Console.WriteLine("Array[i]: " + array[i]);       
            }	
    	
    	
    	}
    	
    }

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: generic ReDim function in C#

    Visual Studio 2005 Has the Generic namespace wthich is pretty much exactly like C++ template. So you'll be able to have a single Redim function in C# that can handle any array type.

    2005 rules.
    I don't live here any more.

  7. #7
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: generic ReDim function in C#

    Yeah, make him wait untill C# 2.0 is out..


    - ØØ -

  8. #8
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: generic ReDim function in C#

    I'm sure it's not as efficient as a standard Array but how about using an ArrayList that automatically resizes as needed?

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  9. #9

    Thread Starter
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: generic ReDim function in C#

    an arraylist sounds like the way to go.

    thanks.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

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