Problem with array initaialization

B

Bonghi

Hi all,
I have a problem initializing an array of custom structures.
It all occurs trying to visualize solids in XNA (ie new version of
managed directx) but I think the problem resides in something I do
wrong in c#. I really hope not to be off topic, furthermore I'm not a
frequent reader of this newsgroup.

Let's get to the point.

Everything works if I initialize the array in the form:

_Rendervertices = new VertexPositionColoredNormal[ { value1, value2,
value3 };

whereas if I initialize it like this:

_Rendervertices = new VertexPositionColoredNormal[3];
_Rendervertices[0] = value1;
_Rendervertices[1] = value2;
_Rendervertices[2] = value2;

I have problems when using the array to fill a vertexbuffer

I believe vertexbuffers read the memory offsets of specified
information and in fact the structure is defined with an attribute
[StructLayout(LayoutKind.Sequential)] should the array be defined with
some similar attribute as well?

I'm really lost with this. I thought the two methods would result in
the same memory allocation.

Many thanks for any hint you could provide.
Claudio
 
J

Jon Skeet [C# MVP]

I have a problem initializing an array of custom structures.
It all occurs trying to visualize solids in XNA (ie new version of
managed directx) but I think the problem resides in something I do
wrong in c#. I really hope not to be off topic, furthermore I'm not a
frequent reader of this newsgroup.

No problem - sounds like you've got the right group.
Let's get to the point.

Everything works if I initialize the array in the form:

_Rendervertices = new VertexPositionColoredNormal[ { value1, value2,
value3 };

whereas if I initialize it like this:

_Rendervertices = new VertexPositionColoredNormal[3];
_Rendervertices[0] = value1;
_Rendervertices[1] = value2;
_Rendervertices[2] = value2;

Note that you've got value2 twice there - is that in the original
code?

Otherwise, the two snippets should be the same.
I have problems when using the array to fill a vertexbuffer

What kind of problems? Can you post a short but complete program which
demonstrates the problem? See
http://pobox.com/~skeet/csharp/complete.html for what I mean by that.

Jon
 
B

Bonghi

I have a problem initializing an array of custom structures.
It all occurs trying to visualize solids in XNA (ie new version of
managed directx) but I think the problem resides in something I do
wrong in c#. I really hope not to be off topic, furthermore I'm not a
frequent reader of this newsgroup.

No problem - sounds like you've got the right group.
Let's get to the point.
Everything works if I initialize the array in the form:
_Rendervertices = new VertexPositionColoredNormal[ { value1, value2,
value3 };
whereas if I initialize it like this:
_Rendervertices = new VertexPositionColoredNormal[3];
_Rendervertices[0] = value1;
_Rendervertices[1] = value2;
_Rendervertices[2] = value2;

Note that you've got value2 twice there - is that in the original
code?

Otherwise, the two snippets should be the same.
I have problems when using the array to fill a vertexbuffer

What kind of problems? Can you post a short but complete program which
demonstrates the problem? Seehttp://pobox.com/~skeet/csharp/complete.htmlfor what I mean by that.

Jon

Jon, thanks a lot for replying...
The problem lied somewhere else... I've found the error in a
completely different piece of code and in fact
the two methods I mentioned are perfectly equivalent.

The double value2 was just a typo on this post but the code was
different but too complex for a post so I
modified it to help people have a better understanding... of what
turned out to be a false problem.

:)

Many thanks again and apologies for wasting your time.

Claudio
 
J

Jon Skeet [C# MVP]

Jon, thanks a lot for replying...
The problem lied somewhere else... I've found the error in a
completely different piece of code and in fact
the two methods I mentioned are perfectly equivalent.

Excellent. It would have been very strange otherwise.

Many thanks again and apologies for wasting your time.

No problem at all - just glad it was all sorted out :)

Jon
 

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