473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Generic Conversion Of Types

I am trying to figure out how to write a generic "format" routine to return
a string type variable. I want to pass this routine an object and based on
the type of the object format according to the type. I tried something
like this, but of course it does not like this... How can I accomplish
this?

Here is a snipit of code...

private string ConvertType(object item )
{
string val;
switch (item.GetType())
{
case System.Int32:
val = item.ToString();
break;
case System.Guid:
val = item.ToString();
break;
case System.Single:
FormatTheNumber((single)item)
break;
case System.Double:
FormatTheNumber((Double)item)
break;
}
}

Thanks in advance for your assistance!!!
Nov 15 '05 #1
3 2718
Hi,

The switch statement will not work in this case, if I am not mistaken
you will get a compile error along the lines of the switch expression
not being an integral type.

You could use the 'is' operator as follows

if ( item is System.Int32 )
...
else if ( item is System.Single )
...
else it ( item is System.Double )
...

Hope this helps

Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #2

"Jim Heavey" <Ji*******@nospam.com> wrote in message
news:Xn*********************************@207.46.24 8.16...
I am trying to figure out how to write a generic "format" routine to return a string type variable. I want to pass this routine an object and based on the type of the object format according to the type. I tried something
like this, but of course it does not like this... How can I accomplish
this?

Here is a snipit of code...

private string ConvertType(object item )
{
string val;
switch (item.GetType())
{
case System.Int32:
val = item.ToString();
break;
case System.Guid:
val = item.ToString();
break;
case System.Single:
FormatTheNumber((single)item)
break;
case System.Double:
FormatTheNumber((Double)item)
break;
}
}

Thanks in advance for your assistance!!!

To get the switch to work, add a "ToString()" to the "item.GetType()"
so you get " switch (item.GetType().ToString()) ".
Also change the case-arguments to strings: case "System.Int32" and so on.

You could build cases for the types that require special formatting (like
your
System.Single) and handle all simple ToString() cases in a "default".
Hans Kesting
Nov 15 '05 #3
Hi Jim,

I was just reading an article on c# generics (msdn - csharp
homepage)... It might be something worth looking into for a future
version of this routine.. But in the interim try this.

private string ConvertType(object item )
{
Type t = item.GetType();
string val = t.ToString();
string retval="";

switch (val)
{
case "System.Double":
retval = item.ToString("d");
break;
default:
//handle all cases except
really unusual types
retval = (string) item.ToString("r");
break;

}
return retval;
}
--
~~~~~~~~~~~~~
Tommie Carter
tcarternyc(at)hotmail(dot)com
--
Jim Heavey <Ji*******@nospam.com> wrote in message news:<Xn*********************************@207.46.2 48.16>...
I am trying to figure out how to write a generic "format" routine to return
a string type variable. I want to pass this routine an object and based on
the type of the object format according to the type. I tried something
like this, but of course it does not like this... How can I accomplish
this?

Here is a snipit of code...

Thanks in advance for your assistance!!! private string ConvertType(object item )
{
string val;
switch (item.GetType())
{
case System.Int32:
val = item.ToString();
break;
case System.Guid:
val = item.ToString();
break;
case System.Single:
FormatTheNumber((single)item)
break;
case System.Double:
FormatTheNumber((Double)item)
break;
}
}

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Master of C++ | last post by:
Hello Folks, I have a programming dilemma with templates and "genericity". I can think of a few solutions, but I am not sure which one is the best in terms of genetic C++ style. Any suggestions...
4
by: Tom Jastrzebski | last post by:
Hello everybody, Here is the problem I came across experimenting with Generics. I would like to write a class or a struct adding integer to any other, initially undefined *numeric type*. So, my...
6
by: Charles Law | last post by:
I want to do something like this: obj = CType(value, Value.Type) Well, not exactly, but I think that captures the essence. I realise it won't work as I have written it, and it looks a bit like...
6
by: Urs Eichmann | last post by:
While experimenting with the Feb CTP Edition of VB 2005, I came across "generic procedures". You can write: Public Class Foo Public Sub MySub(Of tDisp As IDisposable)(ByVal vMyParm As Integer)...
8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
22
by: Adam Clauss | last post by:
OK, I have class A defined as follows: class A { A(Queue<B> queue) { ... } } Now, I then have a subclass of both classes A and B. The subclass of A (SubA), more specifically is passed a...
2
by: Harold Howe | last post by:
Howdy all, I am getting a compiler error regarding a consrained conversion. It complains that it can't make the type conversion, even though the generic type argument inherits from the target of...
16
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one...
6
by: Rennie deGraaf | last post by:
Hello, I would like to write a function that reads a sequence of unsigned shorts from /any/ container, converts them to pairs of unsigned chars, and writes them to /any/ container. In other...
32
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...

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.