Creating a dynamic array...

M

Matthew Wells

I am creating an array of a specific type. If I use Array.CreateInstance
and then try to assign as Object into an element I get

Error 2 Cannot apply indexing with [] to an expression of type
'System.Array'

Remember that I still can't cast becaue I don't know the type ahead of time.

You would think that the array is of type t becaust that's what it is
supposed to do. I believe that Array.CreateInstance is creating a generic
Array that can hold the type t, not creating an array of t types. I'm open
to suggestions.



Jon Skeet said:
Matthew Wells said:
I did say that's what I'm using (this is a little corrected from my last
post - I checked the actual line)

Object objList = Activator.CreateInstance(t, new Object[1] {5}); \\ for
five
elements

You'll notice that I'm now using the signature for CreatteInstance that
is
needed for an array of type t.

But you're still using *Activator*.CreateInstance rather than
*Array*.CreateInstance.

The call to Array.CreateInstance is simpler and more obviously
understandable, IMO.
But like I said before, if you use this, you can't assign a value to an
element of the array like:

objList[0] = 2;

You'll get an error. You have to declare another array, cast it, and
then
assign this array to that.

Object[] ObjListNew = (Object[])objList;

After you're done using ObjListNew, you can jsut refer to the original
objList since they refer to the same object.

Nice and neat.

All of that is still true with Array.CreateInstance, but you don't have
to go through as many hoops.
 
J

Jon Skeet [C# MVP]

Matthew Wells said:
I am creating an array of a specific type. If I use Array.CreateInstance
and then try to assign as Object into an element I get

Error 2 Cannot apply indexing with [] to an expression of type
'System.Array'

Remember that I still can't cast becaue I don't know the type ahead of time.

You would think that the array is of type t becaust that's what it is
supposed to do. I believe that Array.CreateInstance is creating a generic
Array that can hold the type t, not creating an array of t types. I'm open
to suggestions.

I'm not suggesting you remove *all* your code and replace it with a
call to Array.CreateInstance - just the bit that's calling
Activator.CreateInstance.

Activator.CreateInstance is declared to return Object, so you're
already having to cope with that - just cope with Array.CreateInstance
returning Array in exactly the same way. Cast it to object[] (for
reference types - that won't work for value type arrays) or whatever
you currently do.

Note that if you *just* need indexing, you can do:

IList array = Array.CreateInstance(...);

IList provides an indexer.
 
M

Matthew Wells

Now I've got it all down. It failed the first time I tried
Array.CreateInstance because I was still using my original

Type t = Type.CreateType(sEntityName + "[]");

so I was really creating an array of arrays. I set it back to just the type
so the Array.CreateInstance was working, but I still couln't assign to it.
Your seggestion of IList worked!!! Now I'm down to these two lines of code
and everything works perfectly.
Type t = Type.GetType(sEntityName);

IList objList = Array.CreateInstance(t, dt.Rows.Count);

Now here's where I ask too much. I don't need this, but it would be great.
If I create a single instance of t:

Object objNew = Activator.CreateInstance(t);

....how can I get the intellisense to work? For eexample, I create a
Customer object so when I type

objNew.

I see
..Name
..Address
.... etc.

This one must be impossible.

Thanks to all of you for your help!!!!!




Jon Skeet said:
Matthew Wells said:
I am creating an array of a specific type. If I use Array.CreateInstance
and then try to assign as Object into an element I get

Error 2 Cannot apply indexing with [] to an expression of type
'System.Array'

Remember that I still can't cast becaue I don't know the type ahead of
time.

You would think that the array is of type t becaust that's what it is
supposed to do. I believe that Array.CreateInstance is creating a
generic
Array that can hold the type t, not creating an array of t types. I'm
open
to suggestions.

I'm not suggesting you remove *all* your code and replace it with a
call to Array.CreateInstance - just the bit that's calling
Activator.CreateInstance.

Activator.CreateInstance is declared to return Object, so you're
already having to cope with that - just cope with Array.CreateInstance
returning Array in exactly the same way. Cast it to object[] (for
reference types - that won't work for value type arrays) or whatever
you currently do.

Note that if you *just* need indexing, you can do:

IList array = Array.CreateInstance(...);

IList provides an indexer.
 
J

Jon Skeet [C# MVP]

Matthew Wells said:
Now I've got it all down. It failed the first time I tried
Array.CreateInstance because I was still using my original

Type t = Type.CreateType(sEntityName + "[]");

so I was really creating an array of arrays. I set it back to just the type
so the Array.CreateInstance was working, but I still couln't assign to it.
Your seggestion of IList worked!!! Now I'm down to these two lines of code
and everything works perfectly.
Type t = Type.GetType(sEntityName);

IList objList = Array.CreateInstance(t, dt.Rows.Count);

Now here's where I ask too much. I don't need this, but it would be great.
If I create a single instance of t:

Object objNew = Activator.CreateInstance(t);

...how can I get the intellisense to work?

You can't. That would be asking the IDE to know that you're talking
about Customer even though you've already said that you don't know
which type you *are* using!

As you say, it's impossible.
 
M

Matthew Wells

Bummer.

Thanks again!!!

BTW, this is the longest thread I've vver been involved with.

Jon Skeet said:
Matthew Wells said:
Now I've got it all down. It failed the first time I tried
Array.CreateInstance because I was still using my original

Type t = Type.CreateType(sEntityName + "[]");

so I was really creating an array of arrays. I set it back to just the
type
so the Array.CreateInstance was working, but I still couln't assign to
it.
Your seggestion of IList worked!!! Now I'm down to these two lines of
code
and everything works perfectly.
Type t = Type.GetType(sEntityName);

IList objList = Array.CreateInstance(t, dt.Rows.Count);

Now here's where I ask too much. I don't need this, but it would be
great.
If I create a single instance of t:

Object objNew = Activator.CreateInstance(t);

...how can I get the intellisense to work?

You can't. That would be asking the IDE to know that you're talking
about Customer even though you've already said that you don't know
which type you *are* using!

As you say, it's impossible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top