471,337 Members | 879 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Type Conversion

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.

Dec 9 '05 #1
4 1422
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Dec 9 '05 #2
ok thanks :-)

steven.

Dec 9 '05 #3
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
<ap****@gmx.net> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com... 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.

Dec 9 '05 #4
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;


<ap****@gmx.net> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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.

Dec 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Mark Oliver | last post: by
7 posts views Thread by Madhu Gopinathan | last post: by
27 posts views Thread by Yuriy Solodkyy | last post: by
3 posts views Thread by pgconnolly | last post: by
16 posts views Thread by Enekajmer | last post: by
2 posts views Thread by Martin v. Lwis | last post: by
669 posts views Thread by Xah Lee | last post: by
4 posts views Thread by zaeminkr | last post: by
8 posts views Thread by Smithers | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.