473,804 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to cast in c# (Just kidding). But I do have a non-trival casting question.

G'Day.

Anybodey got an idea on this problem.

Say I have a function object doCast(object obj, Type t);
It's job is to cast the obect (obj) to a new type (t) and return it.

Sounds so simple, but I cant seem to find a way to do it without some
prety extravigant hacks.

Can anybodey think of a simple solution that I am overlooking. (um for
..net 1.0)

-dm

Mar 17 '06 #1
16 1550
Opps there is a .net has a function that does this.....
Convert.ChangeT ype(...).

My bad.

-dm

Mar 17 '06 #2
<th*********@gm ail.com> wrote:
Anybodey got an idea on this problem.

Say I have a function object doCast(object obj, Type t);
It's job is to cast the obect (obj) to a new type (t) and return it.

Sounds so simple, but I cant seem to find a way to do it without some
prety extravigant hacks.

Can anybodey think of a simple solution that I am overlooking. (um for
.net 1.0)


Well, normal casting (leaving aside actual conversions) wouldn't do you
any good, because the method signature would still have to be "object".
Are you looking to perform the various implicit and explicit
conversions?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 17 '06 #3
Convert.ChangeT ype performs convertion, not simple cast, and requires
the object to implement IConvertible interface. If you simply want to
cast, the doCast function would make no sense since it returns object
and you have to cast the return value again.
Sounds so simple, but I cant seem to find a way to do it without some
prety extravigant hacks

I am curious what are those hacks?

Mar 17 '06 #4
> G'Day.

Anybodey got an idea on this problem.

Say I have a function object doCast(object obj, Type t); It's job is
to cast the obect (obj) to a new type (t) and return it.

Sounds so simple, but I cant seem to find a way to do it without some
prety extravigant hacks.

Can anybodey think of a simple solution that I am overlooking. (um for
.net 1.0)

-dm


What would be the difference from just doing:

SomeObject x;
SomeOtherObject y = (SomeOtherObjec t)x;

?

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vk arlsen.no
PGP KeyID: 0x2A42A1C2
Mar 17 '06 #5
Hi,

Say I have a function object doCast(object obj, Type t);
It's job is to cast the obect (obj) to a new type (t) and return it.

Sounds so simple, but I cant seem to find a way to do it without some
prety extravigant hacks.

Can anybodey think of a simple solution that I am overlooking. (um for
.net 1.0)


You will have to return an object reference, so I find the method itself
pretty useless as it's right now. What are you trying to do?


Mar 17 '06 #6
Hi,

What would be the difference from just doing:

SomeObject x;
SomeOtherObject y = (SomeOtherObjec t)x;


The OP do not have SomeOtherObject nor he knows what it's , all he has is a
Type instance representing it.
Mar 17 '06 #7
Hello again, I thought is had solved the problem but as pointed out by
Truong it only works for classes implementing IConvetable.

Basicly I need to convert an object to a new type where the "new type"
is determined at runtime.

I think this is actuly a challange now...

basicly complete this stub:
object doCast(object obj, Type t)
{
object obj;

///magic code goes here to conver obj to type t

return obj;
}

The person with the most elegant solution will win.... "New Zealand".
Dont miss your chance to own your own pacific island nation!

-dm

Mar 22 '06 #8
opps (bad stub, lets correct),

object doCast(object obj, Type t)
{
object newObj;

///magic code goes here to conver obj to type t

return newObj;
}
Idealy this would work....

newObj = obj as t;

-dm

Mar 22 '06 #9
<th*********@gm ail.com> wrote:
Hello again, I thought is had solved the problem but as pointed out by
Truong it only works for classes implementing IConvetable.

Basicly I need to convert an object to a new type where the "new type"
is determined at runtime.


Well, how do you expect that conversion to happen? What has the
knowledge to convert one type to another in your code?

For instance, suppose I have two classes:

FirstClass
{
int x;
}

SecondClass
{
string y;
}

How would you expect it to convert an instance of FirstClass into an
instance of SecondClass?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 22 '06 #10

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

Similar topics

3
6579
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? Here's what I'm generally trying to achieve: I'm building (trying to anyway) a serialization library. Here's my design:
5
1345
by: Basil | last post by:
Hello. Compiler BCB60. There is a simple example: class a1 { int v; public: a1 ():v (0) {} a1 (int v1):v (v1) {}
5
3007
by: Luke Dalessandro | last post by:
Code: Thread -> U -> T public class Thread { protected: thread_t _tid; virtual void foo() = 0; public: // Static entry function for the internal thread
36
6698
by: MSG | last post by:
The answer is neither. Use macros. #define ALLOC(size, type) ((type) *) malloc((size) * sizeof(type)) #define NEW(type, name, size) (type) * (name) = ALLOC((size), (type)) They are both type-safe and concise. Compare: NEW(int, x, 1000);
16
16007
by: Amarendra GODBOLE | last post by:
Hi, I am a bit confused over the correct usage of memcpy(). Kindly help me clear the confusion. The linux manpage for memcpy(3) gives me the following prototype of memcpy(3): #include <string.h> void *memcpy(void *dest, const void *src, size_t n);
16
11289
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
9
2729
by: Frederick Gotham | last post by:
Let's assume that we're working on the following system: CHAR_BIT == 8 sizeof( char* ) == 4 (i.e. 32-Bit) Furthermore, lets assume that the memory addresses are distributed as follows: 0x00000000 through 0xFFFFFFFE : Valid byte addresses
6
527
by: Jack | last post by:
Is it possible to cast a 4-byte data type (e.g. an unsigned int) to a 4-byte struct? Sometimes the HIWORD and LOWORD of a 4-byte value contain information independent of each other. Is there a direct way to perform this cast? If not, is there way in C++ to do it? For example, I would like my function test() to work the way it is coded below. It produces a compiler error though: #include <pshpack2.h// 16-bit padding
10
2542
by: mypetrock | last post by:
Has anyone run into this error message? Unable to cast object of type 'Foo.Bar' to type 'Foo.Bar'. I'm trying to cast an object of type Foo.Bar that I got out of a hash table into a variable of type Foo.Bar. Foo.Bar data = (For.Bar)input; Thanks,
15
2394
by: Lloyd Dupont | last post by:
Don't mistake generic type for what you would like them to be!! IFoo<Ahas nothing in common with IFoo<B>! They are completely different type create dynamically at runtime. What you ask is a bit akin to ask: "the System.Web.UI and System.Windows.Controls namespace both contains a Control class, could I use one in place of the other? common they have the same name!" If you want to use a method common to both you should do as Alun...
0
9715
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
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10353
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
10356
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
10099
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...
0
9176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
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.