type cast question

G

Guest

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
 
D

Daniel O'Connell [C# MVP]

Richard Lee said:
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?
 
J

Jay B. Harlow [MVP - Outlook]

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
 
J

Jon Skeet [C# MVP]

Richard Lee said:
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);
 
J

Jay B. Harlow [MVP - Outlook]

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
 

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