+ 2

Help C# "this keyword"

The this Keyword The program you are given takes 2 numbers as input and should calculate and output their average. But something is wrong. Complete the Avg class by creating the constructor, where the 2 parameters will be assigned to members of the class. Sample Input 5.0 4.0 Sample Output 4.5 Inside the constructor, use this keyword with class members for assignation. my code: using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { double num1 = Convert.ToDouble(Console.ReadLine()); double num2 = Convert.ToDouble(Console.ReadLine()); Avg avg = new Avg(num1, num2); Console.WriteLine(avg.GetAvg()); } } class Avg { double num1; double num2; //create the constructor public Con(double num1, double num2) { this.num1 = num1; this.num2 = num2; } public double GetAvg() { return (num1 + num2)/2; } } } this code isn't working please help.

19th Oct 2021, 8:50 AM
Kongphop Sinsung
10 Answers
+ 3
The constructor should have the same name as the class. Rename Con() to Avg(). Then your program will compile and run correctly.
19th Oct 2021, 1:45 PM
Brian
Brian - avatar
+ 4
Kongpop Sinsung , if you expect to get a helpful answer from the community, please give some more information, but not only '... isn't working properly...' thanks!
19th Oct 2021, 10:45 AM
Lothar
Lothar - avatar
+ 3
Lothar , sorry for my bad English, my code output is "usercode/file0.cs(30,16): error CS1520: Method must have a return type". please help. thanks
19th Oct 2021, 11:10 AM
Kongphop Sinsung
+ 3
I think this is the best solution and the easiest one using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { double num1 = Convert.ToDouble(Console.ReadLine()); double num2 = Convert.ToDouble(Console.ReadLine()); Avg avg = new Avg(num1, num2); Console.WriteLine(avg.GetAvg()); } } class Avg { double num1; double num2; //create the constructor public Avg( double num1,double num2){ this.num1=num1; this.num2=num2; } public double GetAvg() { return (num1 + num2)/2; } } }
20th Dec 2022, 11:58 PM
Muhammad Tariq Muhammad Muhammad Almanzalawy
Muhammad Tariq Muhammad Muhammad Almanzalawy - avatar
+ 2
Kongpop Sinsung If this is Read Only & no question has been asked, please delete from Q&A and post in your personal feed. Q&A is reserved for coding related questions. Please respect this forum and help the mods maintain the standard
19th Oct 2021, 9:21 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Thanks! Brian, It's worked.
19th Oct 2021, 2:43 PM
Kongphop Sinsung
+ 2
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { double num1 = Convert.ToDouble(Console.ReadLine()); double num2 = Convert.ToDouble(Console.ReadLine()); Avg avg = new Avg(num1, num2); Console.WriteLine(avg.GetAvg()); } } class Avg { readonly double num1; readonly double num2; //create the constructor public Avg(double num1, double num2) { this.num1 = num1; this.num2 = num2; } public double GetAvg() { return (num1 + num2)/2; } }
22nd Feb 2023, 8:47 AM
Guy Martial KEYOU
+ 1
sorry, this is about "this & readonly" in C# and I don't know why this code isn't working properly, sorry again.
19th Oct 2021, 9:49 AM
Kongphop Sinsung
0
How it's working
15th Dec 2022, 3:16 AM
Aghingith GJ
Aghingith GJ - avatar
0
I got it
15th Dec 2022, 2:17 PM
Aghingith GJ
Aghingith GJ - avatar