Groups | Blog | Home
all groups > dotnet clr > october 2004 >

dotnet clr : [C#] Question about casting and System.Type.GetType()


Jim Bancroft
10/7/2004 9:44:03 AM

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)

{

System.Data.SqlTypes.SqlInt32 myTestInt;
myTestInt= (System.Type.GetType("System.Int32")) myLong;

}



However, when I put the actual type in parens, it works fine:

private static void myTestFunction(long myLong)

{

System.Data.SqlTypes.SqlInt32 myTestInt;
myTestInt= (System.Int32) myLong;

}

Would anyone know offhand what I'm doing wrong? I thought the GetType
method would do the trick here, but I'm not an expert on casting and the
GetType method.



tom wisnowski
10/7/2004 9:55:03 AM
System.Type.GetType will return a Ssytem.Type object. What your first exampe
is trying to do is cast a long to a System.Type which cannot be done. When
doing a cast, you need to cast to the target type (Int32 in your case).

If you want to use the System.Type to do the cast, use Convert.ChangeType()

Hope that's clear!

Tom Wisnowski
Statera
MCP MCAD
[quoted text, click to view]
Richard Blewett [DevelopMentor]
10/7/2004 10:06:20 AM
Type.GetType returns an instance of the System.Type class - a view on the tyoe information (metadata) about the type passed in. So it will have information like what methods and fields are in the type and what interfaces it implements.

What you want to do is coerce one type to another and so you use the (<typename>) syntax or cast which says (in the case of integral types) "give me the equivelent of this integral type as this other one - I know I may lose information as the first is bigger than the second".

Paraphrasingthe line of code that blows up in your code would produce this;

Type t = typeof(int);
myTestInt = t myLong;

this is not going to compile as "t myLong" makes no syntactic sense in C#.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.framework.clr/<e4srrzIrEHA.1900@TK2MSFTNGP10.phx.gbl>


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)

{

System.Data.SqlTypes.SqlInt32 myTestInt;
myTestInt= (System.Type.GetType("System.Int32")) myLong;

}



However, when I put the actual type in parens, it works fine:

private static void myTestFunction(long myLong)

{

System.Data.SqlTypes.SqlInt32 myTestInt;
myTestInt= (System.Int32) myLong;

}

Would anyone know offhand what I'm doing wrong? I thought the GetType
method would do the trick here, but I'm not an expert on casting and the
GetType method.





---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



Rakesh Rajan
10/7/2004 10:31:03 AM
Hi Jim,

When you use System.Type.GetType("System.Int32"), the compiler doesn't have
idea whether there are any cast operators defined, as what GetType returns is
simply an instance of type 'Type' - not the original explicit System.Int32
type.

When you use a type name explicitly, the compiler can refer to this
information and proceed.

HTH,
Rakesh Rajan

[quoted text, click to view]
Jim Bancroft
10/7/2004 10:49:23 AM
I appreciate all your posts-- so it seems GetType returns a generic Type
object, and of course you can't cast to such an animal. Makes sense.

Nicolas read my intentions right in his post above: the reason I'm
experimenting is because I'm porting a VB 6 application to C#. One of the
VB application's functions takes an ADODB.DataTypeEnum and a variant as
parameters. In the function itself, a recordset is created with a column of
the DataTypeEnum (adInteger, for example) passed in, and the variant
parameter used as the value for that column.

I'm looking to do something similar in the port; that is, pass in (maybe) a
System.Data.SqlDbType parameter and an object as parameters. The function
would add a column to a DataTable using the SqlDbType passed in, then the
object would be "cast" to the SqlDbType and used as the value for the
column-- it's only a one-record DataTable.

I'm still feeling my way around all this, and if you have any "do or don't"
suggestions about what I'm aiming for, please feel free to chime in. And
thanks again.

-Jim




Nicholas Paldino [.NET/C# MVP]
10/7/2004 12:58:06 PM
Jim,

When you call System.Type.GetType, it returns an actual type. When you
perform a cast, the compiler doesn't have any concept if instances at that
point, it's just parsing code and creating assemblies from it (from a very
high level).

So, the compiler expects a token that is a type name, and you are
feeding it "System.Type.GetType("System.Int32"))", which is causing it to
complain.

I would guess that you are trying to perform some sort of dynamic type
casting, so you can have early bound calls (and intellisense possibly) when
using code that is anonymous. If this is the case, then you can not do
this. You would have to cast the object to a base type or interface that is
shared, and then make calls to that.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

[quoted text, click to view]

©tefan ©imek
10/8/2004 10:00:23 AM

[quoted text, click to view]

Well I guess you might just use the 'object' type, the DataTable itself
tries to convert the values passed in if it's possible.

HTH, Stefan

AddThis Social Bookmark Button