0

I have a pice of initialiser code that should run when a new instance of a class is created, without having to be called. How would I do that ?

Update:

class Pice {

  public int Type;

  public void init() {

   Type = random(sudo);

  }

}

Now I would like the init to run only once when an instance of the class is created. So where do I put it ?

Mercury
  • 594
  • 1
  • 12
  • 28

3 Answers3

10

Use the class constructor.

 public class MyClass
        {
            public MyClass() 
            {
                //Initialise
            }
        }
Wheels73
  • 2,850
  • 1
  • 11
  • 20
0

Format this to fit your class name, paste into your class.cs file, and add your initialization logic.

public ClassName() {
    // initialization logic goes here
}

See this page for additional information.

parameter
  • 614
  • 10
  • 17
-1
class Pice {

 public Pice(){
  this.init();
}

  public int Type;

  public void init() {

   Type = random(sudo);

  }

}
Giorgi_Mdivani
  • 373
  • 1
  • 5
  • 12