S
Steven Livingstone
Bit of advice here folks.
I am creating a default constructor that just initializes a new instance of
my object and a secon constructor that takes an ID and loads an object with
data from the database for the given ID. I could just leave two separate
constructors, or i could just have the default one call the second with a
"-1" as the ID. This means that the initialization of certain fields can be
based on whether my ID is -1 (for lazy loading and so on).
Here is my question.... i'd like to make use of a nullable int in this case,
so rather than passing -1 as my default value when calling the second
constructor from the first, i can pass null.
Is this good practice, especially in public constructors/interfaces to use
int? (realize how hard it is to end this sentence with a question mark now -
but it IS a question!!
Here is a rough example of what i'm harping on about (for a class "Element")
........
public Element() : this(null) { }
public Element(int? elementid)
{
InitializeObject();
if (elementid != null)
SetUpObject(elementid);
}
The alternative is to use -1 in public interfaces and use null internally,
but that's kinda daft. I'd rather be consistent with one or the other. In
other words, for a number of the constructors that i DO have, which all are
public, can i (or is it a good idea to) use int? and so on rather than the
traditional int??
At a more advanced level - is it part of the CLI? Are there limitations i am
going to run into?
What kind of support with web services, wsdl, schema (well xml schema doe
support null's) as generated from .Net is there for int?
thanks,
steven :: http://stevenR2.com
I am creating a default constructor that just initializes a new instance of
my object and a secon constructor that takes an ID and loads an object with
data from the database for the given ID. I could just leave two separate
constructors, or i could just have the default one call the second with a
"-1" as the ID. This means that the initialization of certain fields can be
based on whether my ID is -1 (for lazy loading and so on).
Here is my question.... i'd like to make use of a nullable int in this case,
so rather than passing -1 as my default value when calling the second
constructor from the first, i can pass null.
Is this good practice, especially in public constructors/interfaces to use
int? (realize how hard it is to end this sentence with a question mark now -
but it IS a question!!
Here is a rough example of what i'm harping on about (for a class "Element")
........
public Element() : this(null) { }
public Element(int? elementid)
{
InitializeObject();
if (elementid != null)
SetUpObject(elementid);
}
The alternative is to use -1 in public interfaces and use null internally,
but that's kinda daft. I'd rather be consistent with one or the other. In
other words, for a number of the constructors that i DO have, which all are
public, can i (or is it a good idea to) use int? and so on rather than the
traditional int??
At a more advanced level - is it part of the CLI? Are there limitations i am
going to run into?
What kind of support with web services, wsdl, schema (well xml schema doe
support null's) as generated from .Net is there for int?
thanks,
steven :: http://stevenR2.com