Regarding Class

Hi,

I'm facing a small problem and it would be great if someone could help me. Imagine I have a class which has a property called say "setvalue". Now the class should be designed in such a way that when I create an object of this class, lets say that the object is objClass, there should be a value set for the property "setvalue". For example like objClass.setvalue=1; .If no value is set I should get an error during compile time itself. Can someone tell me how I can do this. It would be great if you can provide me the code too.

Thanx & Regards,

Frenzy



Answer this question

Regarding Class

  • CorySmith

    Hi PJ,

    Thanx a lot for the code. It works great! Thanks once again.

    Regards,

    Ken.


  • brianmrush

    You are welcome, when you have any problem with implementing this, please lett us know!


  • Randalin

    Just take the value as a argument within the ctor:


    public class MyClass
    {
    private string _url;

    public int Url
    {
    get
    {
    return _url;
    }
    set
    {
    // Check the new value before setting.
    if( value == null || value.Lenght == 0 )
    {
    throw new ArgumentNullException( "Url" );
    }

    _url = value;
    }
    }

    public MyClass( string url )
    {
    // Check the argument value before setting.
    if( url == null || url.Lenght == 0 )
    {
    throw new ArgumentNullException( "url" );
    }

    _url = url;
    }
    }




  • Regarding Class