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

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


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.


Nov 16 '05 #1
6 19178
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
"Jim Bancroft" wrote:

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.


Nov 16 '05 #2
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]
- mv*@spam.guard.caspershouse.com

"Jim Bancroft" <bo********@nowhere.com> wrote in message
news:e4**************@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.

Nov 16 '05 #3
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/<e4**************@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

[microsoft.public.dotnet.framework.clr]
Nov 16 '05 #4
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

"Jim Bancroft" wrote:

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.


Nov 16 '05 #5
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

Nov 16 '05 #6

"Jim Bancroft" <bo********@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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



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
Nov 16 '05 #7

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

Similar topics

2
by: Angelos Karantzalis | last post by:
Hi guys, I'm trying to load a class instance dynamically, and then cast it to it's base type so I can use it in my app. More specifically, I'm dynamically instantiating a...
3
by: Steve Teeples | last post by:
Can someone explain how to cast an object to a specific type during runtime? // This line of code tells me the objects type. System.Type type = System.Type.GetType("string of the type"); //...
3
by: Steve Teeples | last post by:
I have a method that passes in two string objects (both numerical numbers) and a string identifying their type. public bool DoCompare(string num1, string num2, string theirType) { System.Type...
12
by: Steve Teeples | last post by:
Can someone tell me the correct method of casting and object at run time. Here is a sample of what I'm trying to do. classA acts as a base for classes that execute code in classes derived from...
61
by: Ken Allen | last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years. One of the drawbacks with VB6 and earlier was the difficulty in casting a 'record' to a different 'shape' so one could...
2
by: Giulio Petrucci | last post by:
Hi everybody, here's my problem: I have to dymanically build (and compile, of course) some code, from some ECMAScript function. ECMAScript variables I get are not typezed, so I should have...
3
by: LongBow | last post by:
Hello all, First of all, sorry for multiple question per one thread. I have two questions. First what I think might be the easier problem. I am capturing data from an embedded device which I...
2
by: NullQwerty | last post by:
Hey folks, So, I've got three enum types: enum enum1 enum enum2 enum enum3 And then I've got a function overloaded three times to accept each enum type: private void func(enum1 myenum){}
11
by: Dan Tallent | last post by:
I have code here that checks the type of an object and then cast it so I can access its properties. In the simple example below I am accessing the ReadOnly property that is unique to textbox...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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,...

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.