Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting non-0-based array created by c# Array.CreateInstance to collection T [duplicate]

I'm trying to convert vb6 code to c# code. My vb6 source code contains arrays of the most varied types and after conversion by Vs2005 tools these arrays have become 0-based array, but I need to reconvert to non-0-based array.So for example I try to use Array.CreateInstance with explicit cast to T[] for a generic use:

public void ResizeArrayBaseN<T>(ref T[] original, int firstLower, int firstCount)
{
    try
    {
        int[] myBoundsArray = new int[1] {firstLower };
        int[] myLengthsArray = new int[1] {firstCount - firstLower + 1 };
        original = (T[])Array.CreateInstance(typeof(T), myLengthsArray, myBoundsArray);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

But I'm catching cast from T[*] to T[] error. Can someone help me,please ? Thanks in advance

like image 247
Micheal Bolton Avatar asked Jan 18 '26 10:01

Micheal Bolton


1 Answers

A T[] is a vector array - a zero-based one-dimensional array. You cannot use T[] to represent an array with a non-zero base, because: it isn't a vector. This is what the * in T[*] means. I suspect it can only be referenced as an Array - making it quite inconvenient to use.

like image 104
Marc Gravell Avatar answered Jan 20 '26 01:01

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!