3

I've learned to use implicit operators for my classes.

Now I want to do something like this:

public static implicit operator string(int val)
{
    return "The value is: " + val.ToString(); // just an nonsense example
}

However this is wrong. Compiler says:

User-defined conversion must convert to or from the enclosing type

How can I workaround this?

My goal is to be able to run code like this:

int x = 50;
textbox1.Text = x; // without using .ToString() or casting
5
  • Why is this wrong? In my opinion it's perfectly right! Commented Jul 22, 2013 at 10:44
  • @RomanoZumbé I updated my question. When I try this, compiler says "User-defined conversion must convert to or from the enclosing type" Commented Jul 22, 2013 at 10:47
  • You can't work around this because you must implement the implicit operator as a static method in the class being converted - and of course, you can't change System.Int32's implementation. Commented Jul 22, 2013 at 10:48
  • 3
    This is a bad goal. Don't try to write code like this. There's a reason that people cry foul over VB.NET coders who turn off option strict. Commented Jul 22, 2013 at 10:48
  • @J... I will think about it. Commented Jul 22, 2013 at 10:58

3 Answers 3

6

You can't add an operator outside of the class of either the parameter or the return type of the operator.
In other words: You can't add an operator that converts between two types that you both don't control.

The compiler error you get is very explicit about this:

User-defined conversion must convert to or from the enclosing type

So, what you are trying to achieve here is simply not possible.
Your best option probably is an extension method on int that performs the conversion and returns a string.

Sign up to request clarification or add additional context in comments.

3 Comments

I understand that. There is no way to workaround this to get same effect?
@Kamil: You can't. However, see the last sentence in my updated answer for an alternative approach.
I already did. I tried to minimize my code and avoid writing .MyExtension() everywhere. Maybe I will write own class and put that integer inside. Thanks.
2

You might want to consider an alternative approach.

You could write an extension method on Control like so:

public static class ControlExt
{
    public static void SetText(this Control self, object obj)
    {
        self.Text = obj.ToString();
    }
}

Then your code would become:

int x = 50;
textbox1.SetText(x);

It would of course work with any type, not just ints:

textbox1.SetText(DateTime.Now);

Comments

1

You need to add implicit operator as a member of the class. Since you do not have control over int class; it is not possible to add an implicit operator to int class.

You may either write a wrapper for int or create a new extension method to convert int to string directly (which has already been defined as ToString()).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.