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 / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

CreateInstance question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
> Adrian < - 18 Nov 2006 20:24 GMT
Array.CreateInstance( typeof(Int32), 5 );

What is the advantage of creating an array this way?

Adrian.
Dave Sexton - 18 Nov 2006 20:41 GMT
Hi Adrian,

It takes a Type as a parameter.  This allows you to define an Array variable at design-time, and at
runtime create an instance of any given Type:

Array array = Array.CreateInstance(typeof(int), 5);

Since I hard-coded typeof(int) its value may not be as apparent, but imagine if a Type was passed
into a method instead, for instance, and used as the argument to the CreateInstance method.

Signature

Dave Sexton

> Array.CreateInstance( typeof(Int32), 5 );
>
> What is the advantage of creating an array this way?
>
> Adrian.
> Adrian < - 18 Nov 2006 21:03 GMT
> Hi Adrian,
><snipped>
> Since I hard-coded typeof(int) its value may not be as apparent, but imagine if a Type was passed
> into a method instead, for instance, and used as the argument to the CreateInstance method.

Apologies for being so slow: could you please give an example? I don't
follow.

Adriam
Dave Sexton - 18 Nov 2006 21:24 GMT
Hi Adrian,

public void DoStuff(Type arrayType)
{
   // Here, Int32 is hard-coded
   int[] intArray = null;

   // Here, an Array of an unknown Type is created (late-bound)
   Array unknownTypeArray = Array.CreateInstance(arrayType, 10);

   // if we knew that arrayType was, for example, typeof(int)
   // then we could cast our Array into an int[]:
   if (unknownTypeArray is int[])
   {
       intArray = (int[]) unknownTypeArray;

       Console.WriteLine("int[] length: " + intArray.Length);
   }
   else
       Console.WriteLine("unknown Array Type: " + unknownTypeArray.GetType().FullName);
}

Signature

Dave Sexton

>> Hi Adrian,
>><snipped>
[quoted text clipped - 7 lines]
>
> Adriam
> Adrian < - 18 Nov 2006 22:22 GMT
Dave, I played about with your example. Very interesting.
The point is clear now. Thank you,
Adrian.

using System;
public class SamplesArray
{
public static void Main()
{
 Type arrayType = Type.GetType("System.String");
 new SamplesArray().DoStuff(arrayType);
}

//public void DoStuff(Type arrayType)
public void DoStuff(Type arrayType)
{
 // Here, Int32 is hard-coded
 int[] intArray = null;

 // Here, an Array of an unknown Type is created (late-bound)
 Array unknownTypeArray = Array.CreateInstance(arrayType,5, 10);

 // if we knew that arrayType was, for example, typeof(int)
 // then we could cast our Array into an int[]:
 if (unknownTypeArray is int[])
 {
  intArray = (int[]) unknownTypeArray;

  Console.WriteLine("int[] length: " + intArray.Length);
  Console.Read();
 }
 else
 {
  Console.WriteLine("unknown Array Type: " +
unknownTypeArray.GetType().FullName);
  Console.Read();
 }
}
}
Sharon - 18 Nov 2006 20:47 GMT
Array provides the CreateInstance method, instead of public constructors, to
allow for late bound access.

> Array.CreateInstance( typeof(Int32), 5 );
>
> What is the advantage of creating an array this way?
>
> Adrian.

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.