473,326 Members | 2,111 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,326 software developers and data experts.

virtuality and user-defined conversions

Are user-defined conversions chosen at compile time, or are there ways to
make sure that they are chosen at run-time, based on the actual type of the
object?

Here is a simplified example of what I'm trying to accomplish:

I have a method that has a parameter of type object. If the parameter is a
boxed uint, it should unbox it. So, the method looks like this:

void f(object o)
{
uint u = (uint)o;
}

I don't have access to this method's source code, and can't change it.

Now, I would like to pass it other types of objects besides boxed uints, and
have it convert them to uints. For example, I've created the following
class:

class WrappedUInt
{
private uint m_wrappedUInt;

public WrappedUInt(uint u)
{
m_wrappedUInt = u;
}

public static explicit operator uint(WrappedUInt w)
{
return w.m_wrappedUInt;
}
}

If I try the following, the conversion is not invoked and I get a class cast
exception:

WrappedUInt wu = new WrappedUInt(37);
f(wu);

However, if f() is modified as followed:

void f(WrappedUInt o)
{
uint u = (uint)o;
}

the conversion is invoked and everything works fine.

This seems to imply that conversion operators are only selected at compile
time based on static type information, and don't make use of runtime type
information. Is there any way I can define a conversion that's based on the
actual type of the object at run time, so that the first version of f() will
work?

Thanks for any help you can provide.

Wayne
May 15 '07 #1
3 1321
Wayne,

There isn't a way do to this in the way that you would like, that is, to
have the operator determined at runtime based on the actual type of the
instance.

What I don't get is why the signature isn't typed with a uint for the
parameter, unless you have a switch based on the type in the method itself.
If that's the case, then the best I think you can do is to create a wrapper
method which does some type checking beforehand and performs an appropriate
conversion based on whatever the most appropriate type is.

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

"Wayne" <wc*****@yahoo.comwrote in message
news:13*************@corp.supernews.com...
Are user-defined conversions chosen at compile time, or are there ways to
make sure that they are chosen at run-time, based on the actual type of
the object?

Here is a simplified example of what I'm trying to accomplish:

I have a method that has a parameter of type object. If the parameter is
a boxed uint, it should unbox it. So, the method looks like this:

void f(object o)
{
uint u = (uint)o;
}

I don't have access to this method's source code, and can't change it.

Now, I would like to pass it other types of objects besides boxed uints,
and have it convert them to uints. For example, I've created the
following class:

class WrappedUInt
{
private uint m_wrappedUInt;

public WrappedUInt(uint u)
{
m_wrappedUInt = u;
}

public static explicit operator uint(WrappedUInt w)
{
return w.m_wrappedUInt;
}
}

If I try the following, the conversion is not invoked and I get a class
cast exception:

WrappedUInt wu = new WrappedUInt(37);
f(wu);

However, if f() is modified as followed:

void f(WrappedUInt o)
{
uint u = (uint)o;
}

the conversion is invoked and everything works fine.

This seems to imply that conversion operators are only selected at compile
time based on static type information, and don't make use of runtime type
information. Is there any way I can define a conversion that's based on
the actual type of the object at run time, so that the first version of
f() will work?

Thanks for any help you can provide.

Wayne
May 15 '07 #2
Hi Nick --

Thanks for the info. I was afraid of that. The example was a
simplification, but I assume there's a switch statement in there. In any
case, it's a black box to me.

I thought about using a wrapper method, but it's not a practical solution
for a number of reasons that I can't go into. I'll need to take a different
approach to address this problem.

It's unfortunate that conversions don't depend on the actual runtime type --
it would essentially allow developers to define their own boxing and
unboxing operations.

Wayne

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:96**********************************@microsof t.com...
Wayne,

There isn't a way do to this in the way that you would like, that is,
to have the operator determined at runtime based on the actual type of the
instance.

What I don't get is why the signature isn't typed with a uint for the
parameter, unless you have a switch based on the type in the method
itself. If that's the case, then the best I think you can do is to create
a wrapper method which does some type checking beforehand and performs an
appropriate conversion based on whatever the most appropriate type is.

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

"Wayne" <wc*****@yahoo.comwrote in message
news:13*************@corp.supernews.com...
>Are user-defined conversions chosen at compile time, or are there ways to
make sure that they are chosen at run-time, based on the actual type of
the object?

Here is a simplified example of what I'm trying to accomplish:

I have a method that has a parameter of type object. If the parameter is
a boxed uint, it should unbox it. So, the method looks like this:

void f(object o)
{
uint u = (uint)o;
}

I don't have access to this method's source code, and can't change it.

Now, I would like to pass it other types of objects besides boxed uints,
and have it convert them to uints. For example, I've created the
following class:

class WrappedUInt
{
private uint m_wrappedUInt;

public WrappedUInt(uint u)
{
m_wrappedUInt = u;
}

public static explicit operator uint(WrappedUInt w)
{
return w.m_wrappedUInt;
}
}

If I try the following, the conversion is not invoked and I get a class
cast exception:

WrappedUInt wu = new WrappedUInt(37);
f(wu);

However, if f() is modified as followed:

void f(WrappedUInt o)
{
uint u = (uint)o;
}

the conversion is invoked and everything works fine.

This seems to imply that conversion operators are only selected at
compile time based on static type information, and don't make use of
runtime type information. Is there any way I can define a conversion
that's based on the actual type of the object at run time, so that the
first version of f() will work?

Thanks for any help you can provide.

Wayne

May 15 '07 #3

"Wayne" <wc*****@yahoo.comwrote in message
news:13*************@corp.supernews.com...
Hi Nick --

Thanks for the info. I was afraid of that. The example was a
simplification, but I assume there's a switch statement in there. In any
case, it's a black box to me.

I thought about using a wrapper method, but it's not a practical solution
for a number of reasons that I can't go into. I'll need to take a
different approach to address this problem.

It's unfortunate that conversions don't depend on the actual runtime
type -- it would essentially allow developers to define their own boxing
and unboxing operations.
I'd suggest that you implement IConvertible<uintinstead of an implicit
operator uint.... however you're at the mercy of the black box
implementation and whether it allows for IConvertible.
>
Wayne

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:96**********************************@microsof t.com...
>Wayne,

There isn't a way do to this in the way that you would like, that is,
to have the operator determined at runtime based on the actual type of
the instance.

What I don't get is why the signature isn't typed with a uint for the
parameter, unless you have a switch based on the type in the method
itself. If that's the case, then the best I think you can do is to create
a wrapper method which does some type checking beforehand and performs an
appropriate conversion based on whatever the most appropriate type is.

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

"Wayne" <wc*****@yahoo.comwrote in message
news:13*************@corp.supernews.com...
>>Are user-defined conversions chosen at compile time, or are there ways
to make sure that they are chosen at run-time, based on the actual type
of the object?

Here is a simplified example of what I'm trying to accomplish:

I have a method that has a parameter of type object. If the parameter
is a boxed uint, it should unbox it. So, the method looks like this:

void f(object o)
{
uint u = (uint)o;
}

I don't have access to this method's source code, and can't change it.

Now, I would like to pass it other types of objects besides boxed uints,
and have it convert them to uints. For example, I've created the
following class:

class WrappedUInt
{
private uint m_wrappedUInt;

public WrappedUInt(uint u)
{
m_wrappedUInt = u;
}

public static explicit operator uint(WrappedUInt w)
{
return w.m_wrappedUInt;
}
}

If I try the following, the conversion is not invoked and I get a class
cast exception:

WrappedUInt wu = new WrappedUInt(37);
f(wu);

However, if f() is modified as followed:

void f(WrappedUInt o)
{
uint u = (uint)o;
}

the conversion is invoked and everything works fine.

This seems to imply that conversion operators are only selected at
compile time based on static type information, and don't make use of
runtime type information. Is there any way I can define a conversion
that's based on the actual type of the object at run time, so that the
first version of f() will work?

Thanks for any help you can provide.

Wayne


May 15 '07 #4

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

Similar topics

11
by: Laszlo Zsolt Nagy | last post by:
My problem is about properties and the virtuality of the methods. I would like to create a property whose get and set methods are virtual. I had the same problems in Delphi before and the solution...
60
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm ...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
6
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"...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
7
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the...
0
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
2
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be...
1
by: Carlettus | last post by:
Dear All, sorry but I'm not sure if this is the right place to post my problem. I was using the following asp code to create users in Active Directory. Suddenly, and I don't know the reason, users...
0
by: rbukkara | last post by:
Hi, I have got the following error while trying to add a user in the LDAP Directory. javax.naming.NameNotFoundException: ; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu' I have...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.