Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Properties. Multiple SET input type

I have a property that looks like this

private int clientID;

    public int ClientID
    {
        get { return clientID; }
        set { clientID = value; }
    }

I would like to be able to pass in a string to this property so that the setter will convert it for me. How do I do that?

I know how to do the conversion, I just need to know how to pass in a string without it throwing a type error.

Thanks,

Tom

like image 791
Tomas Beblar Avatar asked Dec 04 '22 07:12

Tomas Beblar


1 Answers

You can't, basically. Options:

  • Have a second property for the ClientID as text (backed by the same field)
  • Have a separate method (e.g. SetClientID(string))
  • Change the property type to object and do different things based on the value passed in (urgh - please don't do this!)
like image 133
Jon Skeet Avatar answered Dec 20 '22 14:12

Jon Skeet