473,706 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1498
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.AsStrin g;
void WriteContent ( object content )
{

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

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

void WriteContent(Da teTime 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.goo glegroups.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.goo glegroups.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1682
by: Mark Oliver | last post by:
Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense. class cData { string s; public cData(string s) { this.s=s;
7
2241
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the collection. Thus, my interface now looks like this public interface IMyCollection : ICollection { void Add (string toBeAdded);
27
5632
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
3
1878
by: pgconnolly | last post by:
/* foreach does implicit type conversion on elements of a params argument or Generic.List. * This is not good. * Examples of evil follow... */ using System; // I love it when C# is strict with me... using System.Collections.Generic;
16
12788
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
2
1880
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version: $Revision: 42333 $
1
3281
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming -- Deep Secrets, Perter van der Linden'. But I wonder why line 9 is ok but line 10 is illegal? Is what Keith Thompson said in another post also helpful to understand this question: "... The implicit conversion rule applies only to void*, not...
669
25996
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
4
2106
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) : x0(a), y0(b),
8
3149
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the developer. x = someObject as MySpecificType; x = (MySpecificType) someObject; Thanks.
0
8783
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9056
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7912
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5939
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2096
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.