473,659 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Referencing System.Type?

8 New Member
I'm trying to make a change to an object depending contextually on it's type. The following code, even though I pass the Int32 into the function as an Object, still knows it's an Int32.
Expand|Select|Wrap|Line Numbers
  1. public String FunctionName(Object obj)
  2. {
  3. return obj.GetType().ToString();
  4. }
  5.  
  6. Int32 testObj = 0;
  7. FunctionName(testObj);
  8. //returns "System.Int32"
  9.  
However, the following gives me a compile-time exception:
Expand|Select|Wrap|Line Numbers
  1. public void FunctionName(ref Object obj)
  2. {
  3. Type typ = obj.GetType();
  4. Object x = 0;
  5. obj = (typ)x;
  6. }
  7.  
Even if I replace the typ object with System.Int32, I still get an error saying I can't cast Object to Int32. Maybe someone out there knows:

Why won't it let me compile this and just give me an exception at runtime if the cast isn't valid?

Is there a way for me to get a handle on a specific System.Type? For instance, I can pass "System.Int 32" into a generic function, but the following won't work:
Expand|Select|Wrap|Line Numbers
  1. public T SomeFunction<T>()
  2. {...}
  3. Type typ = Type.GetType(Int32);
  4. SomeFunction<typ>();
  5.  
Apr 8 '08 #1
1 1345
Plater
7,872 Recognized Expert Expert
I think if you use the "ref" keyword you cannot convert it?
I would not expect you to be able to type cast to a variable type like that, but you should be able to like switch on the object's actual type.
Apr 8 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
1957
by: Mark Broadbent | last post by:
Hi guys, just going through remoting at the moment and a couple of questions relating to .net in general has surfaced. Firstly I have seen in the designer that for the namespace and many of its associated classes of System.Runtime.Remoting are available, but certain ones are not. A reference to System.Runtime.Remoting needs to be added to make available all the unavailable ones. Now although I (think) understand the concept that an...
9
2926
by: MDoyle | last post by:
Here is the easiest question you’ll get all day from a complete newbie. I installed Visual Studio on my laptop last night and have been tryin to write my very first C# programs this morning. I’m running into few issues that I’m sure someone on this forum will be able to help m understand quickly. I started out with a traditional “Hello World” program, again, my firs C# programs. I was surprised to see how the System namespace wa...
6
11276
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header" Src="WebControls/Header.ascx" %> The web user control contains the following server controls
6
2470
by: Mikey_Doc | last post by:
Hi We are running cms 2002, Framework 1.0 with Visual studio 2002. We have just upgraded to Framework 1.1 and visual studio 2003. All of our database connection strings are stored within the machine config, this was necessary as our web site has 4 environments and the database server has a different name in each. Since the upgrade the applications can't read the strings in the
0
1527
by: N. Demos | last post by:
Hello, I have a custom usercontrol, of which I have two instances of in my aspx page. Both the usercontrol and page have codebehind. In the page codebehind, I want a member variable for each usercontrol. My problem is that I keep getting a compiler error on the usercontrol variables in my page codebehind (BC30002: Type 'CityGeoPosSelectClass' is not defined.) For whatever reason, my usercontrol class is not being declared in the page...
9
1589
by: Brett Romero | last post by:
Say I have a library (A.dll) with a method that accepts a collection of a specific type. The type is defined in B.dll. In A.dll, I need to loop through this collection and reference fields of the type. For example, a Person type. It may have arm, leg, feet, and hands. Hands is a collection containing types of fingers. The only way to get at fingers is by accepting the parameter as its true type. This means I'll need a file...
9
1551
by: Greg Smith | last post by:
I have a page that has a code behind file that is looking very messy. I would like to put some of the code in a class library to clean up the page but I can't get the syntax correct for referring to controls like buttons on the from functions in the class library. What would be the correct syntax to do this? Any help is greatly appreciated.
0
1354
by: sansie | last post by:
Hi, I have a page which is basically a details view(in inset mode) inside a datalist. The problem is I want to set the field "invoiceID" (of the detailsview) to an invoiceID coming through the query string of the page. Now, I have sucessfully done this with a detailsview that is not enclosed in a datalist, using the following page load script: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles...
2
6919
by: Andrus | last post by:
I need compile in-memory assembly which references to other in-memory assembly. Compiling second assembly fails with error Line: 0 - Metadata file 'eed7li9m, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' could not be found Saving assembly to Windows temp directory for referencing only creates huge amout of temporary
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8535
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8629
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.