A
apoc69
hi folks,
is that possible in c#? :
i have for example a simple field class, which contains a value, like
this:
public class Field
{
private object value;
.......
.......
public object Value
{
get { return this.value; }
set { this.value = value; }
}
}
now, when i want to get the content(value) of a field,
i have to write:
string myValue = (string)myField.Value;
but,
string myValue = myField.Value;
wouldn't work of course, because Value is an object and not a string.
so, is there a possibility to say, whenever
a STRING or INT type is requested, a automatical
convert(boxing/unboxing) would be executed?
so, i would be able to write:
string myValue = myField;
or
string myValue = myField.Value;
This would auto convert from the value of the field-reference to an
string (because a string is requested). i think visual basic is doing
something similar,but in that case, I want to implement my code for
automical boxing/unboxing.
often, i get problems because i forget to call the .Value property,
for example a method requests some kind of content (int, string,
datetime), like this:
void WriteContent ( object content )
{
// right align on ints
if ( content == typeof ( int ))
{
}
// MM/DD/YYYY
if ( content == typeof ( DateTime ))
{
}
}
and i code:
WriteContent ( myObject.field1 ); // Wrong
INSTEAD
WriteContent ( myObject.field1.Value ); // Correct
thanks,
steven wolf.
is that possible in c#? :
i have for example a simple field class, which contains a value, like
this:
public class Field
{
private object value;
.......
.......
public object Value
{
get { return this.value; }
set { this.value = value; }
}
}
now, when i want to get the content(value) of a field,
i have to write:
string myValue = (string)myField.Value;
but,
string myValue = myField.Value;
wouldn't work of course, because Value is an object and not a string.
so, is there a possibility to say, whenever
a STRING or INT type is requested, a automatical
convert(boxing/unboxing) would be executed?
so, i would be able to write:
string myValue = myField;
or
string myValue = myField.Value;
This would auto convert from the value of the field-reference to an
string (because a string is requested). i think visual basic is doing
something similar,but in that case, I want to implement my code for
automical boxing/unboxing.
often, i get problems because i forget to call the .Value property,
for example a method requests some kind of content (int, string,
datetime), like this:
void WriteContent ( object content )
{
// right align on ints
if ( content == typeof ( int ))
{
}
// MM/DD/YYYY
if ( content == typeof ( DateTime ))
{
}
}
and i code:
WriteContent ( myObject.field1 ); // Wrong
INSTEAD
WriteContent ( myObject.field1.Value ); // Correct
thanks,
steven wolf.