Problem with array initaialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bonghi

    Problem with array initaialization

    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 VertexPositionC oloredNormal[ { value1, value2,
    value3 };

    whereas if I initialize it like this:

    _Rendervertices = new VertexPositionC oloredNormal[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(La youtKind.Sequen tial)] 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
  • Jon Skeet [C# MVP]

    #2
    Re: Problem with array initaialization

    On Jun 3, 4:07 pm, Bonghi <claudio.ben... @gmail.comwrote :
    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 VertexPositionC oloredNormal[ { value1, value2,
    value3 };
    >
    whereas if I initialize it like this:
    >
    _Rendervertices = new VertexPositionC oloredNormal[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

    Comment

    • Bonghi

      #3
      Re: Problem with array initaialization

      On Jun 3, 5:05 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
      On Jun 3, 4:07 pm, Bonghi <claudio.ben... @gmail.comwrote :
      >
      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 VertexPositionC oloredNormal[ { value1, value2,
      value3 };
      >
      whereas if I initialize it like this:
      >
      _Rendervertices = new VertexPositionC oloredNormal[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.htmlfo r 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

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Problem with array initaialization

        On Jun 3, 6:11 pm, Bonghi <claudio.ben... @gmail.comwrote :
        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.

        <snip>
        Many thanks again and apologies for wasting your time.
        No problem at all - just glad it was all sorted out :)

        Jon

        Comment

        Working...