C
C#•15mo ago
Nor

CS0120 - Object refrence required for the non static field, method, or property "object.Equals(obj)"

Hello! I am a self learning programmer beginning with c#. I am trying to code a randomized Math Quiz in a Console app, and i am currently trying to validate the user input and then have it print "Correct" or "False" depending on the answer. Now, im not sure as how to approach this, but i have a general idea of the code and i coded it, its just that i have no idea how to make it check whether the answer was correct, and it gives me the aforementioned error. Heres my code: // START HERE Console.WriteLine("Welcome to this Math Quiz!"); Console.WriteLine("This is your first question"); // RNG Random rnd = new Random(); float random1 = rnd.Next(-100, 100); float random2 = rnd.Next(-100, 100); Console.WriteLine("{0} + {1}?", random1, random2); // Checks if answer is correct float answer = random1 + random2; Convert.ToBoolean(answer); if (Console.ReadLine(Equals(answer))) { Console.WriteLine("Correct!"); } else { Console.WriteLine("False!"); }
26 Replies
SinFluxx
SinFluxx•15mo ago
if (Console.ReadLine(Equals(answer))) will be your problem
Nor
NorOP•15mo ago
yeah i know that, i just need a fix i know the cause i just dont know the solution
SinFluxx
SinFluxx•15mo ago
String.Equals Method (System)
Determines whether two String objects have the same value.
Nor
NorOP•15mo ago
so what would i use to detect the user input?
SinFluxx
SinFluxx•15mo ago
You can use Equals, check the link to see the correct syntax
Nor
NorOP•15mo ago
Equals(String)?
SinFluxx
SinFluxx•15mo ago
That's as a member of the string class, i.e. You'd have to call myStringOne.Equals(myStringTwo) not just Equals(myStringTwo) (how would it know what it's comparing with?) Check the code examples in the link and you'll see how it's being used
Nor
NorOP•15mo ago
// Checks if answer is correct int answer = random1 + random2; Convert.ToBoolean(answer); int answeruser = Console.ReadLine(); if (answeruser.Equals(answer)) { Console.WriteLine("Correct!"); } else { Console.WriteLine("False!"); } so this is the improved code, the issue now is that it cant convert "string" to "int", how do i fix this? @SinFluxx Convert.ToInt32(answeruser) should work right?
SinFluxx
SinFluxx•15mo ago
Sort of 🙂 TryParse is what you should use instead though: https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-7.0
Int32.TryParse Method (System)
Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.
Nor
NorOP•15mo ago
Tryparse isnt a command in the version i use 4.8 net c# 7.8? i think Convert works fine, now the issue seems to be that it cant make an "answer" it doesnt seem to detect the correct answer, it just prints false, even though the answer is correct
SinFluxx
SinFluxx•15mo ago
TryParse should be available, the issue with convert is if a non numeric value is entered you'll hit exceptions
Nor
NorOP•15mo ago
itellisense doesnt display it and its not registered as a command for some reason i think convert works fine for my purposes, i just cant figure out why the code doesnt seem to detect the correct answer as correct
SinFluxx
SinFluxx•15mo ago
Convert.ToBoolean(answer); What are you trying to achieve with that line? And why are you using float for the number to guess but converting the user's guess to an int?
Nor
NorOP•15mo ago
i wrote Convert.ToBoolean due to an error thats somehow fixxed now ´without it the second point is just a mistake
SinFluxx
SinFluxx•15mo ago
Correct those and try again then
Nor
NorOP•15mo ago
still doesnt detect it as a correct answer // START HERE Console.WriteLine("Welcome to this Math Quiz!"); Console.WriteLine("This is your first question"); // RNG Random rnd = new Random(); int random1 = rnd.Next(-100, 100); int random2 = rnd.Next(-100, 100); Console.WriteLine("{0} - {1}?", random1, random2); // Checks if answer is correct float answer = random1 - random2; string answeruser = Console.ReadLine(); Convert.ToInt32(answeruser); if (answeruser.Equals(answer)) { Console.WriteLine("Correct!"); } else { Console.WriteLine("False!"); } the code im using
SinFluxx
SinFluxx•15mo ago
float answer = random1 - random2;
Nor
NorOP•15mo ago
whats with that
SinFluxx
SinFluxx•15mo ago
It's a float
Nor
NorOP•15mo ago
so convert to int?
SinFluxx
SinFluxx•15mo ago
Just change float to int
Nor
NorOP•15mo ago
still doesnt work
SinFluxx
SinFluxx•15mo ago
sorry, it's because your Convert.ToInt32(answeruser); line isn't doing anything, it needs to assign it to a variable, so you could just get rid of:
string answeruser = Console.ReadLine();

Convert.ToInt32(answeruser);
string answeruser = Console.ReadLine();

Convert.ToInt32(answeruser);
And replace it with: int answeruser = Convert.ToInt32(Console.ReadLine());
Nor
NorOP•15mo ago
it works now, thanks man
SinFluxx
SinFluxx•15mo ago
no problem blobthumbsup
MODiX
MODiX•15mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server