C# syntax question

Discussion in 'General Programming Support' started by Slapshot136, Feb 8, 2014.

  1. Slapshot136

    Slapshot136 Divide et impera

    Ratings:
    +483 / 2 / -0
    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?
     
  2. Accname

    Accname 2D-Graphics enthusiast

    Ratings:
    +1,551 / 4 / -4
  3. Slapshot136

    Slapshot136 Divide et impera

    Ratings:
    +483 / 2 / -0
  4. Accname

    Accname 2D-Graphics enthusiast

    Ratings:
    +1,551 / 4 / -4
    They say something about the [ , ] arrays in that article.
     
  5. tom_mai78101

    tom_mai78101 The Helper Connoisseur / Ex-MineCraft Host Staff Member

    Ratings:
    +985 / 4 / -1
    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.
     
  6. Slapshot136

    Slapshot136 Divide et impera

    Ratings:
    +483 / 2 / -0
    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..
     
  7. tom_mai78101

    tom_mai78101 The Helper Connoisseur / Ex-MineCraft Host Staff Member

    Ratings:
    +985 / 4 / -1
    Just remember to initialize it upon creation. All is well.
     
    • Like Like x 1
  8. Slapshot136

    Slapshot136 Divide et impera

    Ratings:
    +483 / 2 / -0
    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
     
  9. tom_mai78101

    tom_mai78101 The Helper Connoisseur / Ex-MineCraft Host Staff Member

    Ratings:
    +985 / 4 / -1
    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.
     
  10. s3rius

    s3rius Linux is only free if your time is worthless.

    Ratings:
    +128 / 0 / -0
    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?
     
    • Like Like x 1
  11. Slapshot136

    Slapshot136 Divide et impera

    Ratings:
    +483 / 2 / -0
    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)
     

Share This Page