GK28
GK28

Reputation: 81

Confused about how C# struct work

I have a struct

public struct card
{
    PictureBox picture;
    double value;
}

I want to make an array of that, and Add/remove pictures and value as I go on. I'm not able to do this

card[] c = new card[13];
c[1].value = 4; 

How do assign, read, and chance values of the those?

Upvotes: 3

Views: 139

Answers (3)

supercat
supercat

Reputation: 81115

A structure is a bunch of variables stuck together with duct tape. If you want a bunch of independent-but-related variables stuck together with duct tape, it is often better to use a struct that exposes a bunch of variables stuck together with duct tape than to design a class which tries to serve that purpose, or a struct which pretends to be an object of a class which tries to serve that purpose. If, however, you want something that behaves as an object, then you should use a class.

Given card defined as shown, card[] foo = new card[10] will make foo identify an array holding ten references of type PictureBox and ten double values. The declaration card bar; will define space for another PictureBox reference and another double which are independent of anything else in the universe. Saying bar = foo[3]; will be equivalent to bar.picture = foo[3].picture; bar.value = foo[3].value;, and will not establish any lasting relationship between the fields of bar and those of foo[3].

Duct-taped-variable structures can be very useful for some purposes, such as holding the coordinates of a point, but it is important to recognize them for what they are. If you want a type to represent an object, use a class. Only if you want it to hold a bunch of independent but related variables duct-taped together should you use a struct.

Upvotes: 0

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149518

A struct in C# is not equivalent to a C struct. In C#, a struct has a by value copy semantic. This can be a bit confusing.

Consider the following:

Card[] cards = new Card[13];
Card card = cards[1];
card.Value = 42;

You would probably expect cards[1].Value to be 42, but you'll be surprised when you find out it isn't. This is partially the reason why mutable structs are evil.

Go with a class instead, which is closer to a C struct in the way that a copy of the reference to the class is passed, instead of copying the value itself:

public class Card
{
    public PictureBox Picture { get; set; }
    public double Value { get; set; }
}

Upvotes: 1

Cyral
Cyral

Reputation: 14153

Make value public.

public double value;

By default, class/struct level elements are private, which makes it inaccessible.

It is recommended to capitalize public elements, and to use properties instead of fields, so using the following would be better:

public double Value { get; set; }

You may want to consider making your struct a class, as is not a very good fit. (See When to use struct?, most of the time you will be working with classes in C#) You could also use a dictionary of picture's and their values:

public Dictionary<Picture, double> Cards;

Upvotes: 6

Related Questions