0

I am an absolute beginner to c# and I need some help with creating an array of classes.

class container {
    public string name;
    string details;
    string destination;

    public void get_all(){
      Console.WriteLine(name + "\n" + details + "\n" + destination);
    }
    public void set_name() {
      Console.WriteLine("enter name : ");
      name = Console.ReadLine();

      Console.WriteLine("enter details : ");
      details = Console.ReadLine();

      Console.WriteLine("enter destination: ");
      destination = Console.ReadLine();
    }
  }

main :

class MainClass {
    public static void Main(string[] args) {
       
      for (int i=0 ; i<10 ; i++){
        var cont[i] = new container();
      }

      cont[1].set_name();
      cont[1].get_all();


    }

I want to declare an array of container and set the second container of the array (as you can see in the last 3 lines) but I can't do it, can you help me?

9
  • 1
    What is wrong with your original code? Did you get some exception? Commented Jan 4, 2021 at 23:37
  • @Fabio Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at ex.MainClass.Main (System.String[] args) [0x00008] in <a1c9c743864949bc89998476dd37fe89>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at ex.MainClass.Main (System.String[] args) [0x00008] in <a1c9c743864949bc89998476dd37fe89>:0 exit status 1 Commented Jan 4, 2021 at 23:39
  • 1
    What is ban? You probably just need var cont = new container[10]; before the loop. Then, you can use cont[i] inside the loop. Commented Jan 4, 2021 at 23:41
  • @Fabio this is the original code Commented Jan 4, 2021 at 23:41
  • 1
    Code in main class is not full, because current code wouldn't even compile, can you post complete example. Notice that cont array is not defined anywhere Commented Jan 4, 2021 at 23:42

2 Answers 2

1

To create array of "containers" and set values for second container.

public class MainClass 
{
  public static void Main(string[] args) 
  {
    var containers = new container[10];
    for (int i=0 ; i<10 ; i++)
    {
      containers[i] = new container();
    }

    containers[1].set_name();
    containers[1].get_all();
  }
}

Will be good if you start following c# coding conventions from the beginning. IT is very important that our code style is consistent, because now days we work in the teams.

For example following General Naming Conventions your code will look like below

public class Container 
{
  public string Name { get; set; }
  private string _details;
  private string _destination;

  public void GetAll()
  {
    Console.WriteLine(Name + "\n" + _details + "\n" + _destination);
  }

  public void SetName() 
  {
    Console.WriteLine("enter name : ");
    Name = Console.ReadLine();

    Console.WriteLine("enter details : ");
    _details = Console.ReadLine();

    Console.WriteLine("enter destination: ");
    _destination = Console.ReadLine();
  }
}

public class MainClass 
{
  public static void Main(string[] args) 
  {
    var containers = new Container[10];
    for (int i=0 ; i<10 ; i++)
    {
      containers[i] = new Container();
    }

    containers[1].SetName();
    containers[1].GetAll();
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

no, it does not work I get this error main.cs(22,14): error CS1525: Unexpected symbol void', expecting class', delegate', enum', interface', partial', ref', or struct' main.cs(22,31): error CS1514: Unexpected symbol [', expecting .' or {' main.cs(22,31): error CS1525: Unexpected symbol ]', expecting class', delegate', enum', interface', partial', ref', or struct' main.cs(24,22): error CS1530: Keyword new' is not allowed on namespace elements main.cs(24,25): error CS1525: Unexpected symbol `co ....
@Newdude, wrap Main method with the class
@Fabio while you're teaching, would you mind also giving some lessons on C# naming conventions? As is the container[sic] class makes me wanna stick pins in my eyes :)
can you(if you have time) tell me why you wrapped the "Main" class into "MainClass"?
@Newdude Main in public static void Main is not a class (a blueprint for an object instance), it is a method - code that performs an action
1

To declare an array you can write

Container[] cont = new Container[10];

You can use var as well

var cont = new Container[10];

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.