Home > OS >  C# Windows forms app. Override toString question
C# Windows forms app. Override toString question

Time:04-03

Is it possible to give the Override ToString method the return value of another method? So in the screenshot you will see a override tostring, which displays the Radius * the PI value.

And I want to say:

Radius * PI value = return value

(return value = "oppervlakte")

enter image description here

CodePudding user response:

The below should work, call the function in the ToString method and insert it's value into the interpolated string.

public override float BerekenOppervlakte()
{
    float oppervlakte = (float)(Radius * Pi);
    return oppervlakte; 
}

public override string ToString()
{
    var oppervlakte = BerekenOppervlakte();
    return $"{Radius}m x {Pi} = {oppervlakte}";
}
  • Related