473,382 Members | 1,238 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

CreateInstance question

Array.CreateInstance( typeof(Int32), 5 );

What is the advantage of creating an array this way?

Adrian.
Nov 18 '06 #1
5 1693
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.

--
Dave Sexton

"Adrian <" <no*@all.accessiblewrote in message
news:45********************@dreader2.news.tiscali. nl...
Array.CreateInstance( typeof(Int32), 5 );

What is the advantage of creating an array this way?

Adrian.


Nov 18 '06 #2
Array provides the CreateInstance method, instead of public constructors, to
allow for late bound access.

"Adrian <" <no*@all.accessiblewrote in message
news:45********************@dreader2.news.tiscali. nl...
Array.CreateInstance( typeof(Int32), 5 );

What is the advantage of creating an array this way?

Adrian.


Nov 18 '06 #3

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:u7**************@TK2MSFTNGP03.phx.gbl...
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
Nov 18 '06 #4
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);
}

--
Dave Sexton

"Adrian <" <no*@all.accessiblewrote in message
news:45********************@dreader2.news.tiscali. nl...
>
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:u7**************@TK2MSFTNGP03.phx.gbl...
>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


Nov 18 '06 #5
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();
}
}
}


Nov 18 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David Elliott | last post by:
I am trying to load an assembly and execute a method from a class. I have listed 3 parts to the code. 1) The Driver to load and execute 2) The Interface 3) The assembly I have a valid referece...
1
by: sean | last post by:
Hello Is it possible to call Activator.CreateInstance if the type is not compiled into an assembly. For instance if I drop a DLL into the same folder where executable is running, will the...
18
by: David Sworder | last post by:
Hi, I need to design a method that creates and returns a large array of objects. The problem is that the *type* of object to create isn't know until runtime. As a result, a parameter of type...
7
by: Osvaldo Bisignano | last post by:
I'm using Assembly.CreateInstance (3rd overload) to create an instance of one of the types included on that dll. But what the hell are the bindingflags? I get this exception: ...
7
by: hazz | last post by:
this is a repost with more concise code (well, for me) and better questions (I hope....) . given the following two classes, my intent is to use either Activator.CreateInstance or InvokeMember pass...
1
by: John Jenkins | last post by:
Hi, I have a fairly simeple question. What are the differences between Assembly.CreateInstance and System.Activator.CreateInstance? I had read that one maps to the other, however when I use...
1
by: hazz | last post by:
this is a repost with a hopefully more clearly stated scenario and more concise questions at the end. given the following two classes, my intent is to use pass a token into the instantiated class...
8
by: Dan Holmes | last post by:
Isn't CreateInstance(typeof(int)) the same as CreateInstance<int>()? i don't understand how this method helps anything. dan
1
by: Johnny R | last post by:
Hello, I'm loading a Class from Assemly DLL using Activator.CreateInstance. That loaded Class is executed in a worker Thread with no loop. What actually happends when class is loaded using...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.