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;
} 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; }
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; }
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; } This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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"...
|
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...
|
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)
|
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:...
|
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...
|
by: Harris |
last post by:
Dear all,
I have the following codes:
======
public enum Enum_Value
{
Value0 = 0,
Value1 = 10,
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |