I am currently studying C# and am trying to prepare for next weeks lessons which will be the introduction of classes and methods. To that end i have attempted to build a class called MaxBox which is meant to be a general utility class that I can store some general functions in like 'Displaying a String' or 'Play Again'. I've built my main file (Program) and my class file (MaxBox) and lines #23, #28 and #59 return the same general error 'An object reference is required for the non-static field, method, or property 'program.MaxBox.DisplayStr(string)'. #57 returns a similar error 'An object reference is required for the non-static field, method, or property 'program.MaxBox.PlayAgain()'
I'm a total newb really, and i'm wrestling with objects, I've done some research to get myself this far but I don't understand the language enough yet to be able to understand what the resources I've read are saying I guess to solve this error. Help and guidance is greatly appreciated. I'm still in my first weeks and really I know nothing.
Program:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; // needed for close using System.Threading.Tasks; namespace a020_Breakcase_MeaningOfNames_C { class Program { public void Play() { /* * Conditionals - Use switch/Case statement too: * Evaluate user data (name) * Return meaning of name evaluated * OR * Return 'Name not found' error message * Say goodbye */ MaxBox.DisplayStr("What's in a name? Let's find out!"); Console.Write("\n\n"); do { MaxBox.DisplayStr("Enter Name: "); string uName = Console.ReadLine().ToLower(); switch (uName) { case "doyle": Console.WriteLine("Doyle means descendant of Dubhghalle"); break; case "fiona": Console.WriteLine("Fiona is considered to be a Latinised form of the Gaelic word fionn, meaning \"white\", \"fair\"."); break; case "hunter": Console.WriteLine("Hunter means to search with purpose"); break; case "liam": Console.WriteLine("This name is a short form of the Irish name Uilliam (William) which is now use independently as a given name. As a Hebrew name, Liam means \"my people; I have a nation\"."); break; case "max": Console.WriteLine("Short for of Maximilian, Maxwell, and the various name using it as a first syllable."); break; case "portia": Console.WriteLine("It is of Latin origin. Feminine form of a Roman clan name. Portia was used by Shakespeare as the name of a clever, determined young heroine in \"The Merchant of Venice\" who disguises herself as a lawyer to save her husband's life."); break; default: Console.WriteLine("I'm sorry but I don't know the meaning of the name " + uName + "."); break; } } while (MaxBox.PlayAgain()); MaxBox.DisplayStr("C#eers!"); } static void Main(string[] args) { Program myProgram = new Program(); myProgram.Play(); Console.Read(); } } } My Class:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace a020_Breakcase_MeaningOfNames_C { class MaxBox { /* * MaxBox is my general functions class * Contains * DisplayStr() - Display the string given * PlayAgain() - If True, runs program again */ public String uName; public String command; public void DisplayStr(String StrTxt) { Console.Write(StrTxt); } public Boolean PlayAgain() { Console.Write("\n\nDo you want to play again? (y)es or (n)o: "); String command = Console.ReadLine().ToLower().Trim(); if (command == "y" || command == "yes") return true; return false; } } }
MaxBoxclass, or change it to be static