0

I have a class that contains some values and an array, and I need elements in this array to have default values.

Like

class Object
{
    public string OName { get; set;}
    public string OType { get; set; }
    public string Data { get; set; }

    Object[] RelationList = new Object[5];
    RelationList[0] = blabla;
    RelationList[1] = blabla;
    ......
}

I need to set RelationList to some default values. Any body knows how to do that? Thanks.

2
  • 7
    I think Object might have another meaning somewhere... you might want to rethink that class name. Commented Jan 13, 2015 at 20:56
  • Thanks for your suggestions. I renamed it. Commented Jan 13, 2015 at 21:00

5 Answers 5

4

Setting those defaults is the job of the constructor:

class SomeKindOfObject
{
    public string OName { get; set; }
    public string OType { get; set; }
    public string Data { get; set; }

    Object[] RelationList = new Object[5];

    // Constructor
    public SomeKindOfObject()
    {
        RelationList[0] = blabla;
    }
}

Or, you could also just use an array initializer if you already have the objects laying around:

Object[] RelationList = new Object[] { blahblah, blahblah, blahblah, ect };
Sign up to request clarification or add additional context in comments.

Comments

0

You'll have to iterate through them.

E.g.

class Foo
{
    private readonly object[] _relationList = new object[5];

    public Foo(object defaultValue)
    {
        for (var i = 0; i < _relationList.Length; ++i) {
            _relationList[i] = defaultValue;
        }
    }
}

Comments

0

Initialize your properties in the constructor:

class Class1
{
    public string OName { get; set;}
    public string OType { get; set; }
    public string Data { get; set; }
    public object RelationList { get; set; }

    public Class1()
    {
      // initialize your properties here
      RelationList = new object[5];
      RelationList[0] = blabla;
      RelationList[1] = blabla;
    }
}

Comments

0

Setting defaults can be done in a constructor for the object

class MyObject
{
   public string OName { get; set; }
   public string OType { get; set; }
   public string Data { get; set; }
   public Object[] RelationList = new Object[5];

   public MyObject()
   {
      RelationList[0] = 1;
      RelationList[1] = 2;
   }
}

Depending on the circumstances, there are other mechanisms for setting these defaults (e.g. creating a new array in the constructor instead of at the declaration, passing them in on the constructor, etc.)

Comments

0

You can use object initialisers

Object[] RelationList = new[] { 
     new Object { OName = "abc", OType = "xyz", Data = "tetst" }, 
     new Object { OName = "abc", OType = "xyz", Data = "tetst" },
     new Object { OName = "abc", OType = "xyz", Data = "tetst" },
     new Object { OName = "abc", OType = "xyz", Data = "tetst" }
 };

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.