cmbrooks
cmbrooks

Reputation: 47

Using constructor to create properties

I am very new to C#, but have to use it for a project at work, so I apologize if this is a duplicate, use the wrong vocabulary or am asking a simple question (it is hard to research a question when you don't understand what the question should be).

I have a class with multiple constructors. I want the properties of that class to be based on the constructor that I call. This is the code I have now:

public class MyClass
{
    public object Property1;
    public object Property2;
    public object Property3;

    public MyClass(object newProperty1, object newProperty2, object newProperty3)
    {
        Property1 = newProperty1;
        Property2 = newProperty2;
        Property3 = newProperty3;
    }

    public MyClass(object newProperty1, object newProperty2)
    {
        Property1 = newProperty1;
        Property2 = newProperty2;
    }

}

What happens is when I call the second constructor is I get an empty Property3 object. What I want to have happen is that there is no Property3 object property included in MyClass at all when the second constructor is called.

Is this possible? Thanks in advance for the help.

Upvotes: 0

Views: 83

Answers (4)

Ron Beyer
Ron Beyer

Reputation: 11273

It is kind of possible to do what you are asking, but you lose the ability to use intellisense (at least as of VS2013):

public class MyClass : DynamicObject
{
    private Dictionary<string, object> _dynamicMembers = new Dictionary<string, object>();

    public MyClass(object newProperty1, object newProperty2, object newProperty3)
       : this(newProperty1, newProperty2)
    {
        ((dynamic)this).NewProperty3 = newProperty3;
    }

    public MyClass(object newProperty1, object newProperty2)
    {
        ((dynamic)this).NewProperty1 = newProperty1;
        ((dynamic)this).NewProperty2 = newProperty2;
    }

    public override IEnumerable<string> GetDynamicMemberNames()
    {
        return _dynamicMembers.Keys.ToArray();
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        return _dynamicMembers.TryGetValue(binder.Name, out result); 
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        _dynamicMembers[binder.Name] = value;

        return true;
    }
}

Which is then used like:

dynamic threePropertyClass = new MyClass(10, "something", 1.6f);
dynamic twoPropertyClass = new MyClass(10, "something");

However there is a lot of boxing/unboxing going on here and I'd avoid it as much as possible. The MyClass object really doesn't have the properties you are trying to reference, it loads them from a Dictionary<string, object>, but it does work like you are wanting, only containing the properties you want. You can also add new properties by doing something like:

threePropertyClass.NewProperty = 15.2;

And you could add additional logic in the TrySetMember to keep the user from doing that if you didn't want that.

Upvotes: 1

Graffito
Graffito

Reputation: 1718

Your declaration is equivalent to :

public class MyClass
{
    public object Property1 = null ;
    public object Property2 = null ;
    public object Property3 = null ;

As you can see, Property3 is null by default. Then no need to dispose Property3.

Remark: you can refactor your second constructor like this:

 public MyClass(object newProperty1, object newProperty2) : this(newProperty1,newProperty2,null) {}

Upvotes: 0

Andres Castro
Andres Castro

Reputation: 1858

You should use inheritance for what you are trying to accomplish.

public class MyClass
{
    public object Property1;
    public object Property2;

    public MyClass(object newProperty1, object newProperty2)
    {
        Property1 = newProperty1;
        Property2 = newProperty2;
    }

}

public class MyClass2 : MyClass
{
    public object Property3;

    public MyClass2(object newProperty1, object newProperty2, object newProperty3)
               :base(newProperty1, newProperty2)
    {
        Property3 = newProperty3;
    }
}

Upvotes: 2

Jashaszun
Jashaszun

Reputation: 9270

No, this isn't possible.

Fields are compile-time properties of a class, not runtime. During runtime, you can't change how many and what types of variables a class stores (although you can certainly change their values).

Upvotes: 2

Related Questions