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

c# List<int> in class

Name: tt 2011-03-05 12:18

So i made a class and put a public List<int> in there. Seems like every element in that class is using the same list. Even though i have new list() in the constructor.

Can anybody help?

Name: Anonymous 2011-03-05 12:19

>>1
Can I put my arm in your distended anus?

Name: Anonymous 2011-03-05 12:19

>>1
Or my penis?

Name: Anonymous 2011-03-05 12:19

>>1
My hard throbbing niggercock?

Name: Anonymous 2011-03-05 12:19

>>1
Please let me ravage you in the anus.

Name: Anonymous 2011-03-05 12:20

>>1
I NEED DICK

Name: Anonymous 2011-03-05 12:20

and i though /b/ was shit

Name: Anonymous 2011-03-05 12:21

show your code, well-enclosed in [code][/code] tags.

Name: Anonymous 2011-03-05 12:21

>>7
The autism has consumed /proggles~/. Feel free to stay and spam /b/ memes. ;(

Name: Anonymous 2011-03-05 12:21

>>7
disregard the fuckheads, please.

Name: Anonymous 2011-03-05 12:22

>>10
ICE BURN MOTHERFUCKER

Name: Anonymous 2011-03-05 12:22

public struct notacija : IComparable<notacija>
        {
            public int f;
            public int g;
            public int h;
            public List<int> pot;

            public int CompareTo(notacija other)
            {
                return f.CompareTo(other.f);
            }

            public notacija(int b, int c, int d)
            {
                f = b + c;
                g = b;
                h = c;
                pot=new List<int>();
                pot.Add(d);
            }

            public notacija(int b, int c)
            {
                f = b + c;
                g = b;
                h = c;
                pot = new List<int>();
            }

            public void dodaj(int d, List<int> e)
            {
                pot=e;
                pot.Add(d);
            }
        }

Name: Anonymous 2011-03-05 12:23

you should of been here when that autist xarn stipp posted

Name: Anonymous 2011-03-05 12:24

>>12
Why would you write code in your third-world-country language?

Name: Anonymous 2011-03-05 12:24

>>12
Could you also show me how you use that class? A minimalistic test case that shows the problems is always great.

Name: Anonymous 2011-03-05 12:25

Once again, please don't reply to the fuckheads.

Name: Anonymous 2011-03-05 12:25

>>14
Becuase the retards at my uni think you are copying from the web if you write in english

Name: Anonymous 2011-03-05 12:28

>>15
notacija temp = new notacija(0, 1, 2);
            Notacija.Add(temp);
            Notacija.Add(temp);
            Notacija[0].pot.Add(1);
            Notacija[1].pot.Add(2);
            int stevec = 0;
            string ss = "";
            for (int b = 0; b < Notacija.Count; b++)
            {
                ss = "";
                for (int a = 0; a < Notacija[b].pot.Count; a++)
                {
                    ss += Notacija[b].pot[a] + " ";
                }
                MessageBox.Show(stevec + " " + ss);
            }


You can paste the class and use this if you want to see the problem if not let me explain:
Notacija is a list of notacija, so i add 2 object to it. Then i add to the List pot in the first object the number 0 and in the second object the number 2.

When i write out the List pot of every object it is like i added both numbers to both lists

Name: Anonymous 2011-03-05 12:30

>>18
>number 0 and in the second object the number 2.
i meant number 1 and number 2 as you can see in the code

also nvm the "stevec" variable you can delete it

Name: Anonymous 2011-03-05 12:31

>>18
Just a stylistic thing: I'm pretty sure variable names should start with lower case letter and class names with an upper case letter.

Name: Anonymous 2011-03-05 12:35

>>18
You are correct in your assumption that a C# struct is a value type. However, this means that all fields are copied during an assignment (struct1 = struct2;), and when a field contains a reference, the reference is simply copied (a new object is not created!).

Name: Anonymous 2011-03-05 12:37

>>21
So how can i make it so that it wont be copied but it will make new ones for each element?

Name: >>21 2011-03-05 12:40

Example:
notacija X = new notacija(1, 2, 3);
notacija Y;

X = Y;

X.f = 2;
Y.f = 3;

At this point, X.f and Y.f contain different values as they are fields of two different structs. However, X.pot and Y.pot point to the exact same reference, that is, the one created in the construct at X's initialization.

Name: Anonymous 2011-03-05 12:41

op here

Niggers

Name: Anonymous 2011-03-05 12:41

>>23
I did actualy figured that out what i want to know is this:
>>22

Name: Anonymous 2011-03-05 12:49

>>22
In C++, you could overload the assignment operator (which is almost never a good idea). In C#, you cannot. The closet you can get is to add a constructor public notacija(notacija other) that initializes its own fields from other's. For value fields, you should simply do x.field=y.field;. For reference fields, you are going to have to create a new object of the same type, and copy over the values inside it manually.

The resulting code should look like:

MyClass X = new MyClass(1,3,4);
MyClass Y;

Y = new MyClass(X);

Now, X and Y should be completely independent and should share no references in their fields.

Name: Anonymous 2011-03-05 12:56

>>26
i need to add this in the constructor?

MyClass X = new MyClass(1,3,4);
MyClass Y;

Y = new MyClass(X);

Name: Anonymous 2011-03-05 13:00

>>27
I can't tell if you're trolling.

Name: Anonymous 2011-03-05 13:02

>>28
Yea sorry i understood now.

Forgot that references are showing to a location and that what i was doing was just copying the reference.

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