Question

I'm making a class that will read information from resource files (doesn't matter the type of resource file, just for clarification) and pass the information on a Form for easy manipulation of information.

My questions is should I use a struct to easily organize the information and then pass a List to a Form looking to utilize the information?

A typical set of information I will be scraping is for example:

[Hero Name]
[Hero Type]
[Starting HP]
[Starting Mana]

[Portrait]
[Backstory]

[Spell 1]
[Spell 1 Description]

[Spell 2]
[Spell 2 Description]

[Spell 3]
[Spell 3 Description]

[Spell 4]
[Spell 4 Description

]

Should I use a struct for this case? Hero name is a string, hero type as well. But for example, the [Portrait] will be an image. Can a struct hold multiple Data Types?

Was it helpful?

Solution

I would prefer a class, in this case.

Structs should be used when all of the following are true:

  1. Act like primitive types
  2. Have an instance size under 16 bytes
  3. Are immutable
  4. Value semantics are desirable.

In your case, none of the above are really true, since you're making a (probably mutable), large group of properties.

You'd want to use a struct for something that was a single, immutable (ie: not changing) value, at least in a logical sense. A complex number, or a vector, etc, are the types of scenarios where structs are good candidates.

Also, in general, figure any time you're going to include a reference type (such as a string) inside of your object, you'll want your object to be a class. That, right away, is a good sign that you don't want struct.

For details, see the design guidelines page for "Choosing Between Classes and Structures".

OTHER TIPS

You certainly can use a struct for this case, but that doesn't necessarily mean you should.

You should use structs when you want value semantics as opposed to reference semantics. MSDN has more guidelines including (these are when not to use structs)

  • It logically represents a single value, similar to primitive types (integer, double, and so on).
  • It has an instance size smaller than 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.

In this case, I would use a class.

Yes, a struct can hold multiple data types.

However, using a struct vs using a class depends wholly on how your going to use the object. Bear in mind that when you assign the object to a new variable, the containgn values will be copied in a new object and no be reflected in the original one, ie:

Dim a As New Character("Willow", 12) ' Starting up the name and the Hp, as an example.
Dim b As Character = a
b.HP = 24
Console.WriteLine(a.HP)

The code will write '12', as that is the hit points saved in 'a'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top