C# syntax question

Slapshot136

Divide et impera
Reaction score
471
I want to create a multi-dimension array of a struct and initialize it to a certain value, but I can't seem to find the right syntax..
Code:
    struct test
    {
        int store;
        void test(int i = 0, int j = 0)
        {
            store = i + j;
        }
    }

Code:
            test[ , ] arr =
            {
                { new test(1,2)} { new test(2,3)},
                { new test(4,5)} { new test(9,1)}
            };

how would I do this?
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
They say something about the [ , ] arrays in that article.
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,675
not particularly.. I want a uniform array, hence the [ , ] as opposed to [][]

You have to rethink about this. Do you want a matrix (rectangular arrays) of Test structs, or a jagged arrays (arrays of possibly-different-length array) of Test structs?

A matrix is a 2D uniform array, while a jagged arrays is a 1D uniform array.
 

Slapshot136

Divide et impera
Reaction score
471
well I half figured it out.. I wanted a matrix tom, and the syntax is

Code:
         test[ , ] arr =
            {
                { new test(1,2), new test(2,3)},
                { new test(4,5), new test(9,1)}
            };

problem is c# doesn't seem to support default constructors on structs..
 

Slapshot136

Divide et impera
Reaction score
471
more c# questions

I used the GUI (*gasp*.. but that kinda is the point of c#, otherwise I would have used c++) to create a board for a game - now I have 64 PictureBox objects that I would like to reference via an array, something along the lines of
Code:
PictureBox* [,] gameGrid =
{
      {PictureBox00, PictureBox01},
      {PictureBox10, PictureBox11}
}

however I seem unable to reference an instance of PictureBox with a pointer.. I see only a horrible alternative:

create a custom class that contains a 64-long switch case and implements [,], and returns accordingly
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,675
Well, you're going to have to ask that in Stack Overflow. Maybe the C# overlords will help you with that problem.

Or maybe this.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
If C# pointer syntax is anything like C++, you'd have to dereference the objects:

Code:
PictureBox* [,] gameGrid =
{
      {&PictureBox00, &PictureBox01},
      {&PictureBox10, &PictureBox11}
}

But what is wrong with using an array of simple PictureBox objects? iirc C# behaves like Java in that it captures a reference to the object rather than dublicate it. So isn't that what you want/need?
 

Slapshot136

Divide et impera
Reaction score
471
But what is wrong with using an array of simple PictureBox objects? iirc C# behaves like Java in that it captures a reference to the object rather than dublicate it.

that would be exactly what I need - thanks (P.S. - I tried dereferencing it as well, but I don't think it's possible in C#.. atleast not on certain system functions like PictureBox)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top