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

Hello,

For example, if I had:

public class Fu { int _bar; public Fu(){ _bar = 12; } public void Square(){ _bar *= _bar; } } 

(This is an example, by the way, I know that this is the worst way to do what I do.)

Anyway, I can go:

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

and _fu returns 144 instead of the class Fu? Or is this just a funny question?

Regards, Harold

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

+4
source share
6 answers

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

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

or if (comment) you want to use the conversion operator:

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

Add this method to the Fu class.

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

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 int values, you can create a translation 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); 
+1
source

I suspect you are looking for an implicit operator overload that allows you to implement an operator that provides custom type conversion. In this case, you can define an implicit conversion between the Fu class and the int type, where an instance of the Fu class will behave exactly like an int value.

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

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

I advise you to be very careful when implementing such functions! They can often be confusing to consumers in your class and should only be implemented when conversion is guaranteed not to result in data loss.

+1
source

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

 MsgBox(TextBox1) 

was exactly the same as:

 MsgBox(TextBox1.Text) 

in my opinion, what you ask does not make much sense, and if that is the case, and it worked, I still wonโ€™t use it. How can we indicate what should be returned, and what if we change the implementation of the class and then we break everything without compile-time errors ?!

0
source

Quetion should be clear, give more details?

anyway

why don't you try class properties.

  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 like

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

Source: https://habr.com/ru/post/1342274/


All Articles