1

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; } } } 
2
  • Use str1.Equals(str2) to test string equality Commented Jan 20, 2014 at 15:12
  • 1
    You should instantiate MaxBox class, or change it to be static Commented Jan 20, 2014 at 15:12

2 Answers 2

6

MaxBox's methods are not static, you need an instance of it.

MaxBox maxBox = new MaxBox(); 

at the beginning of your main class. Then

maxBox.DisplayStr(....) 

also, just to get you thinking, you can replace:

if (command == "y" || command == "yes") return true; return false; 

with

return (command == "y" || command == "yes"); 
Sign up to request clarification or add additional context in comments.

4 Comments

May seem like a minor detail, but it doesn't matter whether MaxBox is static, just that the members he's trying to access aren't.
What's a static? Why would he need an instance if it's not a static? If you're wondering - see msdn.microsoft.com/en-us/library/98f28cdx.aspx
Thank you - I'm so so so new to classes that i completely forgot to 'introduce' (substantiate?) my class to my program. Thank you. And thanks to everyone for all the great tips here. Last week i built a super simple class for my prior project and i'm trying again so when we hit classes i'll be ready as i've heard they're confusing - and they are but not AS confusing as they were last week - again super thanks!
Also - thanks for the suggest on how to return my command, that is very boss and gave me a lot to think about. Thank you very much.
3

The methods PlayAgain and DisplayStr are instance methods on the type MaxBox. In order to call them you need an instance of MaxBox on which to call them. Right now you are trying to call them via the type name which only works for static methods

MaxBox.DisplayStr("hello world"); // wrong MaxBox mb = new MaxBox(); mb.DisplayStr("hello world"); // right 

It is possible to define methods such that you can invoke them from the type name. But doing so requires that they be marked as static

class MaxBox { public static void DisplayStr2(string str){ ... } } MaxBox.DisplayStr2("hello world"); // right 

1 Comment

Thank you for the help - very helpful comments all around and honestly very much appreciated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.