3

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?

2
  • 1
    If you are just starting to learn programming with C# consider sticking with class for some time... If coming from C/C++ - struct is really close to what they are in C/C++ except not public buy default (with the same similar issues about default value semantics). In any case make sure to carefully read some of the discussions on C# class vs. struct... Commented Jun 19, 2015 at 2:36
  • Also see When to use struct? Commented Jun 19, 2015 at 2:37

3 Answers 3

6

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;

Sign up to request clarification or add additional context in comments.

5 Comments

I went with struct since I found them easy to use in C, also I'm not comfortable using class, and not clear on how to make one. Also could you elaborate on the public Dictionary<Picture, double> Cards. Since That might be useful to me.
A dictionary is a collection that uses a key and a value (Also known as a HashMap in some languages) to store data. To make a class, you just change struct to class, and I would strongly recommend doing so. I use classes 99% of the time as structs are for very special cases.
So I'm trying to make a class, that contains a picture box and double value. However, i'm not able to able to make a variable of type "PictureBox" inside the class, any idea why?
If a struct is going to be used as a bunch of variables stuck together with duct tape, rather than an object, it should be a bunch of variables stuck together with duct tape. Using autoproperties with classes leaves open the possibility that a later version of the class might do something different (e.g. lazily computing them, providing a change-notification mechanism, etc.) but since structs can't usefully do such things there's little advantage to using auto-properties.
@GK28 The difference between a struct and a class in C has almost nothing to do with the difference between a struct and a class in C#. In C the difference is just the default accessibility of members. In C# a struct has value semantics and a class has reference semantics, and both classes and structs have the same default accessibility modifiers on members. You should be using a class here, not a struct.
1

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; }
}

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.