Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C# y u do dis to me? (C# Arrays)

Name: sadimir 2011-12-02 14:15

Switching from c++ to c# and derping hard.

The error:
"An unhandled exception of type 'System.NullReferenceException' occurred in sharp_TEST.exe

Additional information: Object reference not set to an instance of an object."

The masterpiece:

    class Program {
        static void Main() {

            Polygon4[] x = new Polygon4[20];

            foreach (Polygon4 face in x)
            {
                face.print();
            }

        }
    }
//////////////////////////////
    class Polygon4
    {
        Vertex3D a, b, c, d;
        public Polygon4()
        {
            a = new Vertex3D();
            b = new Vertex3D();
            c = new Vertex3D();
            d = new Vertex3D();

        }
        public void print()
        {
            a.print();
            b.print();
            c.print();
            d.print();

        }
    }
/////////////////////////////
    class Vertex3D
    {
        float x, y, z;
        public void print()
        {
            System.Console.Write(x+" "+y+" "+z+"\n");
        }
        public Vertex3D()
        {
            x = y = z = 0;
        }
    }

Explain c# object arrays pls

Name: Anonymous 2011-12-02 14:28

class Polygon4You got to be seriously brain damaged by OOP to make 4-vertices polygons into a class.

Name: sadimir 2011-12-02 14:33

This is a training program asshat, the names of the classes are irrelevant and don't represent anything.
If you like it better this way rename Polygon4 into Poop and Vertex3D into Corn

Name: Anonymous 2011-12-02 14:50

No code tags, seriously filling in the name field, insults and imageboard memes? Go back to whence you came from already.

Name: sadimir 2011-12-02 16:05

I found out that all 20 of the array members are null pointers =/
Am I supposed to do something extra to initialize the members?

Name: Anonymous 2011-12-02 16:08

Don't help >>1.

Name: sadimir 2011-12-02 16:18

Alright /prog/ you've been as helpful as ever.
I found out the answer myself.

Obj[] a = new Obj[20]; will not populate the array. That'll take manual labor.

Also, >>6, actual programmers on /prog/ are rare and it's mostly populated by tards who think their "Hello World!" will get them into EA Games.

Case closed.

Name: Anonymous 2011-12-02 16:22

>>1
When you create a new array, its entries get initialized with nulls. Therefore, you must initialize them yourself, as such:

Polygon4 e = new Polygon4();
for (int i = 0; i < x.Length; i++)
  x[i] = e;

Name: Anonymous 2011-12-02 20:49

Girls' Generation 9 GET

Name: Anonymous 2011-12-02 23:55

>>5
in C# an array of objects is basically an array of pointers to Objects. Just code assuming objects are never on the stack and it will make sense.

Name: Anonymous 2011-12-03 2:03

In C# or Java:

Polygon4[] x = new Polygon4[20];


is similar to this


Polygon4** x = new Polygon4*[20];

Name: Anonymous 2011-12-03 2:45

>>7
EA Games

DO NOT WANT

Duke Crunch'em Forever

Name: Anonymous 2011-12-03 5:22

you should be using generics(C++ template faggotry) anyway
just sayin'.jpg

Don't change these.
Name: Email:
Entire Thread Thread List