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

using Type

I have a function, which accept two parameters, one is a object,
another is a Type. I need to cast the object to the type define by
Type. is it possible?

void MyFunction(Object O, Type T)
{
// this is what I want to do
T MyObject=(T)O;
}
Jun 21 '06 #1
3 1362
Jinsong,

You can't do this without Generics, you could do something like this:

void MyFunction<T>(object o)
{
T MyObject = (T) o;
}

However, that's kind of pointless, because you could just do this then:

void MyFunction<T>(T o)
{
// Use o here as T.
}

This is better, since it will throw a compiler error if the object you
pass is not of type T (or derived from T).

In .NET 1.1 you should define a common interface that the instances
implement and then make your parameter that.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jinsong Liu" <Ji*****@mssolution.com.nospam> wrote in message
news:h6********************************@4ax.com...
I have a function, which accept two parameters, one is a object,
another is a Type. I need to cast the object to the type define by
Type. is it possible?

void MyFunction(Object O, Type T)
{
// this is what I want to do
T MyObject=(T)O;
}

Jun 21 '06 #2
Hi,

No, you cannot do that.

What is what you want to do?

You could use reflection to call members of T though.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jinsong Liu" <Ji*****@mssolution.com.nospam> wrote in message
news:h6********************************@4ax.com...
I have a function, which accept two parameters, one is a object,
another is a Type. I need to cast the object to the type define by
Type. is it possible?

void MyFunction(Object O, Type T)
{
// this is what I want to do
T MyObject=(T)O;
}

Jun 21 '06 #3
Nicholas Paldino, why wont this work?

String strA;
int i = (int) strA;

// gives the error can't cast string to integer (or something like
that)

Obinna.

Nicholas Paldino [.NET/C# MVP] wrote:
Jinsong,

You can't do this without Generics, you could do something like this:

void MyFunction<T>(object o)
{
T MyObject = (T) o;
}

However, that's kind of pointless, because you could just do this then:

void MyFunction<T>(T o)
{
// Use o here as T.
}

This is better, since it will throw a compiler error if the object you
pass is not of type T (or derived from T).

In .NET 1.1 you should define a common interface that the instances
implement and then make your parameter that.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jinsong Liu" <Ji*****@mssolution.com.nospam> wrote in message
news:h6********************************@4ax.com...
I have a function, which accept two parameters, one is a object,
another is a Type. I need to cast the object to the type define by
Type. is it possible?

void MyFunction(Object O, Type T)
{
// this is what I want to do
T MyObject=(T)O;
}


Jun 21 '06 #4

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

Similar topics

11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
7
by: | last post by:
Hello, I would like to do the following from a asp.net button click: <form method="POST" action="https://www.1234.com/trans_center/gateway/direct.cgi"> <input type="hidden" name="Merchant"...
0
by: Peter Conrey | last post by:
I have a perl web service (using SOAP::Lite) with a method called "Detail" that returns a strucure (hash reference to be exact). It works fine when consumed by a Perl client, but when I try to...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
2
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
39
by: Mark Odell | last post by:
I've always declared variables used as indexes into arrays to be of type 'size_t'. I have had it brought to my attention, recently, that size_t is used to indicate "a count of bytes" and that using...
7
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
8
by: =?Utf-8?B?Q2hyaXMgSGFsY3Jvdw==?= | last post by:
Hi there I've successfully added some .NET validation controls to a page (using <asp:RequiredFieldValidator ...), however when I try to set the 'display' property to 'dynamic', my page then...
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: 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?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...

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.