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

Why is CS0553 a compiler error?

I tried to create a class which basically acts like union.
I have a string and an double member in my class but only
one is "active" and I want the use the "implicit" keyword
to get the "active" object. Here's the code:

public class StringDoubleVal
{
string strval;
double dblval;
bool string_active;

public void SetVal(string val)
{
strval = val; string_active = true;
}

public void SetVal(double dblval)
{
dblval = val; string_active = false;
}

public static implicit operator object(StringDoubleVal
val)
{
return val.string_active ? val.strval : val.dblval;
}
}

The problem is that since "object" is a base class of
StringDoubleVal, the compiler won't let me define an
implicit operator for the "object" type since the compiler
can perform such an operation by its self.

The problem is that the semantics in my program of the
value of a StringDoubleVal object is not the object it
contains but the currently active object contained by the
StringDoubleVal object. Why is this just not a warning
instead of an error?
Nov 15 '05 #1
3 1823
Martin Hallerdal <an*******@discussions.microsoft.com> wrote:

<snip>
The problem is that the semantics in my program of the
value of a StringDoubleVal object is not the object it
contains but the currently active object contained by the
StringDoubleVal object. Why is this just not a warning
instead of an error?


How would you expect your operator to ever be invoked? Because there's
already an implicit conversion from StringDoubleVal to object, that
would always be used.

Why not just define a method GetVal which returns the appropriate
object?

(You'll then need to fix up the other problems in your code, but I'm
assuming you can do that.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Hi Martin,

I think that you need to implement a string or double operator , also you
should redefine ToString() . I think that with this 3 methods you will get
the functionality you want.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Martin Hallerdal" <an*******@discussions.microsoft.com> wrote in message
news:0c****************************@phx.gbl...
I tried to create a class which basically acts like union.
I have a string and an double member in my class but only
one is "active" and I want the use the "implicit" keyword
to get the "active" object. Here's the code:

public class StringDoubleVal
{
string strval;
double dblval;
bool string_active;

public void SetVal(string val)
{
strval = val; string_active = true;
}

public void SetVal(double dblval)
{
dblval = val; string_active = false;
}

public static implicit operator object(StringDoubleVal
val)
{
return val.string_active ? val.strval : val.dblval;
}
}

The problem is that since "object" is a base class of
StringDoubleVal, the compiler won't let me define an
implicit operator for the "object" type since the compiler
can perform such an operation by its self.

The problem is that the semantics in my program of the
value of a StringDoubleVal object is not the object it
contains but the currently active object contained by the
StringDoubleVal object. Why is this just not a warning
instead of an error?

Nov 15 '05 #3
Martin,

I don't know what you want to use this class for, but I think you get the
same basic behavior from just using a variable of type "object". You can
assign either a double or a string to it, and when you reference it, it will
give you the current value as an object.

Perhaps you could explain more what you're trying to do.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Martin Hallerdal" <an*******@discussions.microsoft.com> wrote in message
news:0c****************************@phx.gbl...
I tried to create a class which basically acts like union.
I have a string and an double member in my class but only
one is "active" and I want the use the "implicit" keyword
to get the "active" object. Here's the code:

public class StringDoubleVal
{
string strval;
double dblval;
bool string_active;

public void SetVal(string val)
{
strval = val; string_active = true;
}

public void SetVal(double dblval)
{
dblval = val; string_active = false;
}

public static implicit operator object(StringDoubleVal
val)
{
return val.string_active ? val.strval : val.dblval;
}
}

The problem is that since "object" is a base class of
StringDoubleVal, the compiler won't let me define an
implicit operator for the "object" type since the compiler
can perform such an operation by its self.

The problem is that the semantics in my program of the
value of a StringDoubleVal object is not the object it
contains but the currently active object contained by the
StringDoubleVal object. Why is this just not a warning
instead of an error?

Nov 15 '05 #4

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

Similar topics

0
by: rollasoc | last post by:
Hi, I seem to be getting a compiler error Internal Compiler Error (0xc0000005 at address 535DB439): likely culprit is 'BIND'. An internal error has occurred in the compiler. To work around...
1
by: Ayende Rahien | last post by:
reparing resources... Updating references... Performing main compilation... error CS0583: Internal Compiler Error (0xc0000005 at address 53168B12): likely culprit is 'BIND'. An internal...
3
by: Mark Rockman | last post by:
------ Build started: Project: USDAver2, Configuration: Debug .NET ------ Preparing resources... Updating references... Performing main compilation... error CS0583: Internal Compiler Error...
4
by: David Sworder | last post by:
Consider the following line of code (it's not important what it does): resp.DocItem=Relations.SelectDocItems_BySearchString(req.SearchPhrase); It turns out that this line is in error. The...
3
by: bsa | last post by:
Hi, This maybe a stupid question for which I apologies, but I couldn't find a solutions so I'll ask. I'm a newbie and I'm trying to do the following: A have a base class called space that...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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.