开发者

Is there any way to set what is returned when a variable is requested as is?

Hullo,

For example if I had:

public class Fu
{
    int _bar;


    public Fu(){
        _bar = 12;
    }

    public void Square(){
   开发者_开发百科     _bar *= _bar;
    }
}

(This is just an example by the way, I know this is the worst possible way of doing what I'm doing.)

Is there anyway that I could go:

Fu _fu = new Fu();
_fu.Square();
Console.WriteLine(_fu);

and _fu returns 144 instead of the class Fu? Or is that just a ridiculous question?

Regards, Harold

Edit: Console.WriteLine(_fu); was a bad example. What if I wanted to do int twelveCubed = 12 * _fu?


Console.WriteLine has to convert the object to a string to print it; the default implementation uses the type's name, but you can override it:

public override string ToString() { return _bar.ToString(); }

or if (comment) you want a conversion operator:

public static implicit operator int(Fu fu) { return fu._bar; }


Add this method in your Fu class

 public override string ToString()
 {
     return _bar.ToString();
 }


You need to override the ToString method

public class Fu
{
    int _bar;


    public Fu()
    {
        _bar = 12;
    }

    public void Square()
    {
        _bar *= _bar;
    }

    public override string ToString()
    {
        return _bar.ToString();
    }
}

If you want to automatically convert to int values you can create an cast operator

public class Fu
{
    int _bar;


    public Fu()
    {
        _bar = 12;
    }

    public void Square()
    {
        _bar *= _bar;
    }

    public static implicit operator int(Fu f)
    {
        return f._bar;
    }
}

And use it like this

Fu _fu = new Fu();
_fu.Square();
int intVal = _fu;
Console.WriteLine(intVal);


I suspect that you're looking for implicit operator overloading, which allows you to implement an operator that provides a user-defined type conversion. In this case, you could define an implicit conversion between the Fu class and type int, where an instance of the class Fu would behave exactly like a value of type int.

For example, adding the following method to your class will make your code work exactly as suggested:

public static implicit operator int(Fu fu)
{
   return fu._bar;
}

I advise you to be very careful when implementing features like this! They can often be confusing to consumers of your class, and should only be implemented when the conversion is guaranteed not to cause a loss of data.


C# is not like old Visual Basic where controls had the concept of DefaultProperty so

MsgBox(TextBox1)

was exactly the same as:

MsgBox(TextBox1.Text)

in my opinion what you are asking does not make much sense and if it did and it worked, I still would not use it. How can we specify what should be returned and what if we change the class implementation and then we break everything without compile time errors?!


Quetion must be clear, give some more details?

anyways

why not you trying the Properties of class.

 public class Fu
{
   private int _bar;
    public Fu()
    {
        _bar = 12;
    }
    public void Square()
    {
       _bar *=_bar;
    }
    public override string ToString()
    {
        return _bar.ToString();
    }
}

And call them as

       Fu fu = new Fu();
        fu.Square();
        Console.WriteLine(fu);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜