0

Just tried to create ArrayList and to populate it with objects, but hit this error:

Cannot create an instance of the static class 'diamondmine.MyObject'

The code so far:

public static class MySettings
{
  //some lines
  public static ArrayList myObjects { get; set; }

  static MySettings()
  {
    //some lines
    myObjects = new ArrayList();
  }
}

public static class MyObject
{
  public static int a { get; set; }
  public static int b { get; set; }

  static MyObject()
  {
    a = 1;
    b = 2;
  }
}

And this is how I am trying to add a new object:

//some code
MySettings.myObjecs.Add(new MyObject());
//more code

Rly don't have a clue where's the problem, I am doing the things written in MSDN :(

captainsac
  • 2,484
  • 3
  • 27
  • 48
1000Gbps
  • 1,455
  • 1
  • 29
  • 34
  • 1
    Your can not create instance of `static` class, replace `public static class MyObject` with `public class MyObject` – General-Doomer May 28 '15 at 06:30
  • 1
    The error message is quite clear. You cannot directly instantiate a static class. Instead, you reference it's member through the name of the class, i.e, `MySettings.myObjects`. – Tim May 28 '15 at 06:31
  • @1000Gbps - you might be confused with the `static constructor`. It's only called when you try to access a `static` member of `MyObject` class for the fiorst time. – Amit Kumar Ghosh May 28 '15 at 06:35
  • Is there any reason why all your classes and properties are static? My approach is to check if this is really necesseray and if not - which I assume - remove those modifiers. – MakePeaceGreatAgain May 28 '15 at 06:42

2 Answers2

5

As the error says, you can't create instances of static classes.

Your MyObject class should not be static, which will let you create instances of it.

See here for some more info on static and non-static: Static vs non-static class members

To answer the questions in the comments your code will end up something like this:

public static class MySettings
{
  //some lines
  public List<MyObject> myObjects { get; set; }

  static MySettings()
  {
    //some lines
    myObjects = new List<MyObject>();
  }
}

public class MyObject
{
  public int a { get; set; }
  public int b { get; set; }

  public MyObject()
  {
    a = 1;
    b = 2;
  }
}

Then the usage is:

MySettings.myObjects.Add(new MyObject());
MySettings.myObjects[0].a; // returns 1
Community
  • 1
  • 1
Steve
  • 9,335
  • 10
  • 49
  • 81
  • Ok, but I still cannot access properties of the objects in the elements. `'object' does not contain a definition for 'a' and no extension method 'a' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?` `MySettings.myObjects[i].a` for example – 1000Gbps May 28 '15 at 06:35
  • This error drops in an `if` check against two properties of an object added in the array – 1000Gbps May 28 '15 at 06:38
  • That's because myObject's type is an ArrayList, try using a `List` instead. – Steve May 28 '15 at 06:38
  • Well, how ... doing this in last 10 minutes w/o success. And why should I use `List` when it should be ``? – 1000Gbps May 28 '15 at 06:44
  • 1
    It shouldn't be object, you're adding an instantiated type to a collection, then trying to access it's properties. You can only access the properties of a concrete type. – Steve May 28 '15 at 06:45
  • Sorry, but still got the same error :( Whoa ... so its `myObjects = new List():` Now it's working ... – 1000Gbps May 28 '15 at 06:50
  • Can you update the original question with the code you have now? – Steve May 28 '15 at 06:51
  • It's working ... Rly weird way to set an array with objects :\ Weird language – 1000Gbps May 28 '15 at 06:56
1

Replace your code with comethig like this:

public static class MySettings
{
  //some lines
  public static ArrayList MyObjects { get; private set; }

  static MySettings()
  {
    //some lines
    MyObjects = new ArrayList();
  }
}

public class MyObject
{
  public int a { get; set; }
  public int b { get; set; }

  public MyObject()
  {
    a = 1;
    b = 2;
  }
}

Take note, that MyObjects is private set.

General-Doomer
  • 2,681
  • 13
  • 13