C
CSharpNewbie
Hi
I would like (or a least think I would like to) define a wrapper class
for an int so as to make it type safe. I know this means converting a
value type to a reference type - its not that performance sensitive.
How do I write a class that wraps the integer nicely and is something
other than Object?. For example, I have written:
public class DBSchemaID
{
private int schemaID=0;
public N2KDBSchemaID(int schemaIDIn)
{
schemaID=schemaIDIn;
}
public int SchemaID
{
get{return schemaID;}
set{schemaID=value;}
}
}
So now how can I make it act as an int in the when used as an array
entry? For example:
DBSchemaID schemaID=new DBSchemaID(4);
schemaObj=SchemaArray[schemaID];
Is some sort of implict conversion to int (like ToString()) possible.
Also can one override the operators == and >= etc.
Can this be done? Am I crazy for wanting to do it? How else could you
make sure that the parameters of a method call are type safe. In other
words, if a method is GetSchemaDBName(int schemaID) any int could be
handed in. If the method is GetSchemaDBName(DBSchemaID schemaID) then
I am being a bit stricter and safer. Is it reasonable to want to do
this?
Just being allowed to derive from Int32 would do everything necessary.
What the best way of doing this sort of thing? Any advice appreciated
- I am new to this.
Cheers
OldNewbie
I would like (or a least think I would like to) define a wrapper class
for an int so as to make it type safe. I know this means converting a
value type to a reference type - its not that performance sensitive.
How do I write a class that wraps the integer nicely and is something
other than Object?. For example, I have written:
public class DBSchemaID
{
private int schemaID=0;
public N2KDBSchemaID(int schemaIDIn)
{
schemaID=schemaIDIn;
}
public int SchemaID
{
get{return schemaID;}
set{schemaID=value;}
}
}
So now how can I make it act as an int in the when used as an array
entry? For example:
DBSchemaID schemaID=new DBSchemaID(4);
schemaObj=SchemaArray[schemaID];
Is some sort of implict conversion to int (like ToString()) possible.
Also can one override the operators == and >= etc.
Can this be done? Am I crazy for wanting to do it? How else could you
make sure that the parameters of a method call are type safe. In other
words, if a method is GetSchemaDBName(int schemaID) any int could be
handed in. If the method is GetSchemaDBName(DBSchemaID schemaID) then
I am being a bit stricter and safer. Is it reasonable to want to do
this?
Just being allowed to derive from Int32 would do everything necessary.
What the best way of doing this sort of thing? Any advice appreciated
- I am new to this.
Cheers
OldNewbie