Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / March 2008

Tip: Looking for answers? Try searching our database.

Array.CreateInstance failure

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Edgile - 06 Mar 2008 16:58 GMT
Hello,

Bellow is a little except that creates a static array dynamically. However
it creates
a two dimensional one instead of a single dimensional. Why????

public static void Main(string[] args)
{
    int[] intArray = new int[]{2,3};
    Type type = intArray.GetType();
    Array array = Array.CreateInstance(type, 0);
    // array is a two dimensional array. Why?
    intArray = (int[]) array; // Throws InvalidCastException, as expected
    Console.WriteLine();
}
Jon Skeet [C# MVP] - 06 Mar 2008 17:35 GMT
> Bellow is a little except that creates a static array dynamically. However
> it creates
> a two dimensional one instead of a single dimensional. Why????

Because you've said you want an array where each element is of type
int[]. If you want the overall array to be int[], use
Array.CreateInstance(typeof(int), 0)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Ben Voigt [C++ MVP] - 07 Mar 2008 00:16 GMT
> Hello,
>
[quoted text clipped - 11 lines]
> Console.WriteLine();
> }

It isn't a two dimensional array.  You can test that

int[,] array2d = (int[,]) array;    // InvalidCastException

and

array.Rank will be 1, not 2.

In fact it is a one dimensional array where the element type is
intArray.GetType() as you requested, that is each element is an array.

This array-of-arrays is sometimes called a jagged array.

If you want an array of the same type as the first array, use
intArray.GetType().MemberType
Edgile - 17 Mar 2008 10:45 GMT
Thanx for the answers to both Ben and Jon, you were absolutely correct.

> > Hello,
> >
[quoted text clipped - 27 lines]
> If you want an array of the same type as the first array, use
> intArray.GetType().MemberType

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.