By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,636 Members | 1,693 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,636 IT Pros & Developers. It's quick & easy.

type cast question

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.