Zhenia
Zhenia

Reputation: 1

Calling a function in a class with the class as a parameter in c#

class Fraction
    {
        public double Num { get; set; }
        public double Denom { get; set; }
        public void show(Fraction f)
        {
            Console.Write($"({f.Num}/{f.Denom})");
        }
     }

this bit of code shows no errors, but when I try to actually call the show(Fraction x) function it gives the "The name 'show' does not exist in the current context" error. I assume I can't set a class as a parameter, then what workaround could you suggest?

Upvotes: 0

Views: 151

Answers (1)

qTzz
qTzz

Reputation: 126

This is the solution youre looking for

class Fraction
    {
        public double Num { get; set; }
        public double Denom { get; set; }
        public void show()
        {
            Console.Write($"({Num}/{Denom})");
        }
     }

You would call it this way.

var fraction = new Fraction{Num = 10, Denom = 10};
fraction.show();

Upvotes: 1

Related Questions