Type Conversion

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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

now, when i want to get the content(value) of a field,
i have to write:

string myValue = (string)myField.Value;

There is nothing in your code that assure the value is a string so the
above code may throw an exception, a safer (will not throw an exception but
probably will not give you the expected result) is to use the ToString()
method.

BTW, IMO you should not use "value" as a name of a member, it could create
confusion reading the code and errors.
but,

string myValue = myField.Value;

wouldn't work of course, because Value is an object and not a string.

just to be 100% clear (as jon would say :) ) it won't work cause Value is
an instance of Object and not an instance of String
so, is there a possibility to say, whenever
a STRING or INT type is requested, a automatical
convert(boxing/unboxing) would be executed?

not at all, beside a string is not a value type as int, char , it's a
completely different beast , take a look at (jon' s article)
http://www.yoda.arachsys.com/csharp/strings.html


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.

VB does automatic casting if you do not use strict , forget that in c#

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 ))
{

}

}


I think you should read a book about c# , you will get a much better
explanation of how the types works in a strong typed language.


cheers,
 
J

James Curran

public class Field
{
private object value;
public object Value
{
get { return this.value; }
set { this.value = value; }
}

public int AsInt
{
get { return (int) this.value; }
}

public string AsString
{
get {return this.value as string; }
}
}

string myValue = myField.AsString;
void WriteContent ( object content )
{

// right align on ints
if ( content == typeof ( int ))
{

EEEKK!!!

void WriteContent(int content)
{
// right align on ints
}

void WriteContent(DateTime content)
{
// MM/DD/YYYY
}

WriteContent ( myObject.field1 ); // Error at compile time!

WriteContent ( myObject.field1.AsInt); // does the write thing
WriteContent ( myObject.field1.AsDateTime); // does the write thing

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
M

Michael S

Hi.

You could write an implicit cast operator like this:

public class Field
{
private object value;
public object Value
{
get { return this.value; }
set { this.value = value; }
}

public static implicit operator string(Field f)
{
return (string)f.value;
}
}


and then this would work.

Field myField = new Field();
myField.Value = "test";

string myValue = myField;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top