473,329 Members | 1,585 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,329 software developers and data experts.

type cast question

Hi,

I have a question when I do a data type cast.

the common way when we do a cast, is we know the type we want to cast to,
i.e. we want to cast object to string,
object xyz = "question";
(string)xyz;

now we only have a type object of System.String type
Type type = Type.GetType("System.String");

how do we use this "type" object to do "(string)xyz"?

Thanks.

Richard

Nov 22 '05 #1
4 2339

"Richard Lee" <Richard Le*@discussions.microsoft.com> wrote in message
news:99**********************************@microsof t.com...
Hi,

I have a question when I do a data type cast.

the common way when we do a cast, is we know the type we want to cast to,
i.e. we want to cast object to string,
object xyz = "question";
(string)xyz;

now we only have a type object of System.String type
Type type = Type.GetType("System.String");

how do we use this "type" object to do "(string)xyz"?


You can't.
What purpose would a cast serve if you don't know the type before hand?
Casting is a language feature that changes the variable type moreso than the
concrete type. What are you trying to achieve?
Nov 22 '05 #2
Richard,
In addition to Daniel's comments that you can't do a cast.

You might be able to use System.Convert.ChangeType to change the type of an
object variable to the Type of the type variable.

I find Convert.ChangeType useful where I have dynamic input or dynamic
output. For example a generalized routine to transform one data source to a
different data source... (ala MS Access Import Wizard or SQL DTS).

Hope this helps
Jay

"Richard Lee" <Richard Le*@discussions.microsoft.com> wrote in message
news:99**********************************@microsof t.com...
Hi,

I have a question when I do a data type cast.

the common way when we do a cast, is we know the type we want to cast to,
i.e. we want to cast object to string,
object xyz = "question";
(string)xyz;

now we only have a type object of System.String type
Type type = Type.GetType("System.String");

how do we use this "type" object to do "(string)xyz"?

Thanks.

Richard

Nov 22 '05 #3
Richard,
I have a service here to sort an array of DataRow object by certain
DataColumn, but the type of DataColumn is object which we can't use compareto method. Why have a service to do that, DataTable.Select & DataView already do it for
you?
What is ICompare, do you mean IComparer?

Reading the IComparer.Compare help, should give you a clue as to how to
implement the function.
int Compare(object a, object b)
{
IComparable ac = a as IComparable;
return ac.compareto (b);
}
You may need special case code in the above for special cases such as
byte[], otherwise the above will handle all the types normally found in a
DataSet.

Hope this helps
Jay

"Richard Lee" <Ri********@discussions.microsoft.com> wrote in message
news:A1**********************************@microsof t.com... The thing I am trying to do is, I also want to dynamicly cast a datasource.
I have a service here to sort an array of DataRow object by certain
DataColumn, but the type of DataColumn is object which we can't use compareto method.

Sample here.

Class DataRowCompare: ICompare
{
private Type type;
private int index;

DataRowCompare(Type type, int index)
{
this.type = type;
this.index = index;
}

compare(object a, object b)
{

// I don't want to use switch here to enumerate all the type
// if I can only use type information passed in via constructor // pheudo code like below
(type)a.compareto ((type)b);
}
}

so at client code, I get DataColumn type and index I want to compare, then
create an instance of this comparer.

Convert.ChangeType also return an object type, it's not we want here.

Thanks

"Daniel O'Connell [C# MVP]" wrote:

"Richard Lee" <Richard Le*@discussions.microsoft.com> wrote in message
news:99**********************************@microsof t.com...
Hi,

I have a question when I do a data type cast.

the common way when we do a cast, is we know the type we want to cast to, i.e. we want to cast object to string,
object xyz = "question";
(string)xyz;

now we only have a type object of System.String type
Type type = Type.GetType("System.String");

how do we use this "type" object to do "(string)xyz"?


You can't.
What purpose would a cast serve if you don't know the type before hand?
Casting is a language feature that changes the variable type moreso than the concrete type. What are you trying to achieve?

Nov 22 '05 #4
Richard Lee <Ri********@discussions.microsoft.com> wrote:
The thing I am trying to do is, I also want to dynamicly cast a datasource.

I have a service here to sort an array of DataRow object by certain
DataColumn, but the type of DataColumn is object which we can't use compareto
method.

Sample here.

Class DataRowCompare: ICompare
{
private Type type;
private int index;

DataRowCompare(Type type, int index)
{
this.type = type;
this.index = index;
}

compare(object a, object b)
{

// I don't want to use switch here to enumerate all the type
// if I can only use type information passed in via constructor
// pheudo code like below
(type)a.compareto ((type)b);
}
}

so at client code, I get DataColumn type and index I want to compare, then
create an instance of this comparer.

Convert.ChangeType also return an object type, it's not we want here.


If you know that you'll be able to call CompareTo, presumably that's
because you know that the object implements IComparable, right? In
which case, just use:

((IComparable)a).CompareTo(b);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #5

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

Similar topics

2
by: seia0106 | last post by:
Hello I am writing a function to read a binary file. Here is a part of code #include <fstream> .. .. BYTE *pData; long lDataLen; pms->GetPointer(&pData); lDataLen = pms->GetSize(); // Read...
4
by: Richard Lee | last post by:
Hi, I have a question when I do a data type cast. the common way when we do a cast, is we know the type we want to cast to, i.e. we want to cast object to string, object xyz = "question";...
6
by: Programmer | last post by:
I am making a project in which i have one interface ITest, and a class which is implementing that interface. I am making object of that class using object...
4
by: T!LT3D | last post by:
Hi I'm trying to convert data automatically from a SqlDataReader into the correct type for its class property, it goes something like this but is reported syntactilly incorrect.... MyField =...
6
by: Jim Bancroft | last post by:
Hi everyone, I'm having some trouble with the code below. I receive a compile-time error on the second line saying "; expected": private static void myTestFunction(long myLong) { ...
7
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
16
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
14
by: Alex | last post by:
I saw the topic of "wired code " about "point to array" and know a little about it. But I am still confused about the question below: I define a point to array "b" int (*b); then I locate the...
8
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...
7
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.