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?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
GK28
  • 81
  • 3
  • 13
  • 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](https://www.bing.com/search?q=C%23+class+vs.+struct)... – Alexei Levenkov Jun 19 '15 at 02:36
  • Also see [When to use struct?](http://stackoverflow.com/questions/521298/when-to-use-struct) – Cyral Jun 19 '15 at 02:37

3 Answers3

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;

Community
  • 1
  • 1
Cyral
  • 13,999
  • 6
  • 50
  • 90
  • 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 Cards. Since That might be useful to me. – GK28 Jun 19 '15 at 02:44
  • 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. – Cyral Jun 19 '15 at 02:54
  • 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? – GK28 Jun 19 '15 at 06:58
  • 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. – supercat Jun 19 '15 at 17:54
  • 1
    @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. – Servy Jun 19 '15 at 18:05
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; }
}
Community
  • 1
  • 1
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
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.

supercat
  • 77,689
  • 9
  • 166
  • 211