3

I was able to create an array of a struct I created, but I'm having trouble doing the same for an array of a class. I'm (faintly) aware that this probably isn't the best way to do this, but I'd appreciate help in figuring out what's going on.

I'm about 2 days into learning C#, and I'm navigating away from MS Office-VBA, if that gives you an idea of what I'm into. Anyway, I'm following an online reference, and along the way trying to play with what I've learned so far. This problem has come about as a result of my playing.

First, let me describe what I've done with the struct, and the array of that struct, with some code snippets.

I've been able to create a struct, called Machines...

// play with structs
struct Machines
{
    // vars for struct
    private string model, SN;
    private int hours;

    // assign values
    public void AssignValues(string model_in, string SN_in, int hours_in)
    {
        model = model_in;
        SN = SN_in;
        hours = hours_in;
    }

    // display values
    public void DisplayValues()
    {
        Console.WriteLine("Model: {0}", model);
        Console.WriteLine("SN: {0}", SN);
        Console.WriteLine("Hours: {0}", hours);
    }
};

... things seem to work just fine:

public static void Main()
{
    // play with structures
    Machines machine1 = new Machines();
    machine1.AssignValues("AA", "ABC01234", 34760);
    machine1.DisplayValues();

Output is:

Model: AA
SN: ABC01234
Hours: 34760

Then, I can create an array of the struct, and things continue to go well:

// play with structures and arrays
// declare, create new instance
Machines [] MyArr = new Machines[10];
MyArr[0].AssignValues("AA", "ABC01235", 43000);
MyArr[0].DisplayValues();

But, when I attempt to do the same with a class, it's a different story. What's going on?

public class ArmstrongMachine
{
    // vars for struct
    private string model, SN;
    private int hours;

    // assign values
    public void AssignValues(string model_in, string SN_in, int hours_in)
    {
        model = model_in;
        SN = SN_in;
        hours = hours_in;
    }

    // display values
    public void DisplayValues()
    {
        Console.WriteLine("Model: {0}", model);
        Console.WriteLine("SN: {0}", SN);
        Console.WriteLine("Hours: {0}", hours);
    }
};

...

    // play with classes
    ArmstrongMachine [] MyMachines = new ArmstrongMachine[10];
    MyMachines[0].AssignValues("AA", "ABC01236", 51000);
    MyMachines[0].DisplayValues();

The issue seems to begin with MyMachines[0].AssignValues.... If I comment out that line and the following, there are no problems (other than the warning that I've created a variable I'm not using).

Any ideas?

Also, please be aware that this is being compiled online.

6
  • 1
    The problem is that the default value for elements in a class array is null - so you need MyMachines[0] = new ArmstrongMachine();. Commented Mar 5, 2014 at 20:35
  • 1
    What is the problem? Does it explode? Also, don't use mutable structs. Commented Mar 5, 2014 at 20:36
  • Since you are new to C# read about value types vs reference types as well. Commented Mar 5, 2014 at 20:38
  • Sorry, maybe I should have been more clear on the actual problem - it won't compile. Commented Mar 5, 2014 at 20:38
  • What is the compiler error you're getting? Commented Mar 5, 2014 at 20:46

2 Answers 2

7

A class gives you a reference type in C#.

The array thus holds references to objects, and not the objects themselves.

The array initially contains nothing, all zeroes, which means all the references will be null.

You need to initialize each element of the array to hold an object reference:

// play with classes
ArmstrongMachine [] MyMachines = new ArmstrongMachine[10];
MyMachines[0] = new ArmstrongMachine();
MyMachines[0].AssignValues("AA", "ABC01236", 51000);
MyMachines[0].DisplayValues();
MyMachines[1] = new ArmstrongMachine();
MyMachines[2] = new ArmstrongMachine();
...
MyMachines[9] = new ArmstrongMachine();

If the array holds value types, like the structs, then the array holds the struct values themselves, thus it works with the structs, and not with the objects.

Also note that you should emphatically not use mutable structs (structs you can change). There's tons of things that can go wrong and bite you in ... so you should not use them. Go with classes in this case.

Here's a video on the subject of mutable structs: Evil Structs, by Jon Skeet.

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

1 Comment

Thank you! I had read about the need to initialize arrays, but unfortunately forgot about it as I progressed on. One lesson learned.
2

In simple words: A struct is only a way the memory is structured whereas a class is a real object. For the second example, you need to create instances of the class before you can make calls to it, because it only points to a memory location (which may be unassigned = a null reference):

ArmstrongMachine [] MyMachines = new ArmstrongMachine[10];
MyMachines[0] = new ArmstrongMachine();
MyMachines[0].AssignValues("AA", "ABC01236", 51000);
MyMachines[0].DisplayValues();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.