0

I've been trying to access an instance lykkesHest of my class horse from a third class game. But it tells me that the instance does not exist in the current context which makes sence because when lykkesHest hest is made it is not made as a public class. But when i try and make lykkesHest a public class instead it gives me a lot of weird error. I apologize for the text outputted to the user as it is danish.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Lykkes_Hest { // The class with the main game hidden inside it! class game { public static int money; public static double fodder; public game() { money = 1000; fodder = 1; } public static void help() { Console.WriteLine("#######################"); Console.WriteLine("###### - HJÆLP - ######"); Console.WriteLine("#######################"); Console.WriteLine(); Console.WriteLine("Kommandoer du kan bruge:"); Console.WriteLine("Hjælp - viser dig denne skærm"); Console.WriteLine("fodre hesten - giver din hest mad hvis du har noget"); } public static void exitSafely() { Console.WriteLine("Er du sikker på at du vil forlade spillet?"); Console.WriteLine("(Ja eller nej)"); if(Program.getBool()) { Environment.Exit(0); } } public void RegisterCommand() { string command = Console.ReadLine(); switch(command) { case "exit": exitSafely(); break; case "vent": lykkesHest.PassDay(); break; default: Console.WriteLine("Ikke genkendt kommando."); break; } } } // A class for making a horse class horse { public string name; public double age = 0; public string color; public double hunger = 50; public double boredom = 50; public horse() { Console.WriteLine("Hvad skal din hest hedde?"); name = Console.ReadLine(); Console.WriteLine("Hvor mange år er din hest?"); while (age == 0) { try { age = Convert.ToInt32(Console.ReadLine()); } catch { Console.WriteLine("Do skal skrive et tal."); } } Console.WriteLine("Hvilken farve har din hest?"); color = Console.ReadLine(); } public void Print() { Console.WriteLine("Din hest heder {0} er {1} år gammel og har farven {2}", name, age, color); } public static void Die(string cause) { Console.WriteLine("Din hest døde af {0}........", cause); Console.WriteLine("Du burde passe bedre på den en anden gang!"); Console.WriteLine(); Console.WriteLine("Klik på en knap for at afslutte."); Console.ReadKey(); Environment.Exit(0); } public void PassDay() { hunger =+ 10; boredom =+ 10; if(hunger >= 100 || boredom >= 100) { horse.Die("sult"); } else if(boredom >= 100) { horse.Die("Kedsomhed"); } } } class Program { public static bool getBool() { while (true) { string answer = Console.ReadLine(); if (answer.ToLower() == "ja") { return true; } else if (answer.ToLower() == "nej") { return false; } Console.WriteLine("Du må kun skrive ja eller nej"); } } static void Main(string[] args) { // Get passowrd Console.WriteLine("HVAD ER KODEORDET FOR AT MÅ SPILLE DET HER AMAZING SPIL?"); if (Console.ReadLine().ToLower() != "what is password?") { Environment.Exit(0); } // Initialize the game game Spil = new game(); Console.WriteLine("###############################"); Console.WriteLine("######## -Lykkes Hest- ########"); Console.WriteLine("###############################"); Console.WriteLine(); Console.WriteLine("Velommen, du spiller version 001"); Console.WriteLine(); // Create a basic horse horse lykkesHest = new horse(); lykkesHest.Print(); Console.WriteLine("Er dette korrekt?"); while(getBool() == false) { lykkesHest = new horse(); } // Optional tutorial Console.WriteLine(); Console.WriteLine("Vil du gerne lære de kommandoer du kan bruge?"); Console.WriteLine("(Ja eller nej)"); if (getBool()) { Console.WriteLine(); Console.WriteLine("Lykkes hest er et interaktivt sandbox spil hvor du styrer EN FUCKING HEST!!"); Console.WriteLine("Du bestemmer selv om du vil have din hest til at en flotter præmie hest eller en sy' hurtig ræcer!"); Console.WriteLine(); Console.WriteLine("Du spiller ved at skrive nogle få kommandoer ind i kommando linjen (Den du læser fra lige nu)."); Console.WriteLine(); Console.WriteLine("Hvis du nogensinde glemmer en kommando eller vil lære nogle nye kan du altid skrive 'Hjælp' for at få dem vist."); Console.WriteLine("Og hvis du nogensinde vil stoppe spillet kan du gøre det ved at skrive 'exit'"); } Console.WriteLine(); Console.WriteLine(); // Loop the rest of the game while (true) { Console.WriteLine("#####################################################"); Console.WriteLine("Penge: {0} - Foder: {1}", game.money, game.fodder); Console.WriteLine("Hestens sult: {0} - Hestens kedsomhed: {1}", lykkesHest.hunger, lykkesHest.boredom); Console.WriteLine("#####################################################"); game.RegisterCommand(); Console.WriteLine(); } } } } 
1
  • 1
    Can you mark the line where you get an error? Commented May 19, 2016 at 9:01

1 Answer 1

3

If the game is about managing a horse, then the game class (which you should be calling Game to follow usual naming conventions) should contain a field of type horse:

class Horse { public int Age{ get; private set }; public int Hunger { get; private set; } public string Name { get; private set; } public Horse(string name) { Name = name; Age = 0; Hunger = 0; } public bool IsDead() { return Hunger >= 100 || Age > 3650; } void PassDay() { Age = Age + 1; Hunger = Hunger + 1; } void Feed() { Hunger = 0; } // etc. public override string ToString() { return string.Format("{0} Age {1}", Name, Age); } } 

Either the game can create it's own horse instance, or you have to pass the horse instance into the Game's constructor. If the game manages one horse, and the game always has exactly one horse, it's a good practice to assign it in the constructor so that you can't even have a game without a horse.

I translated (and changed) your game somewhat to make the example clearer.

class Game { private readonly Horse horse; private int money; // more things public Game(Horse horse, int money) { this.horse = horse; this.money = money; } public bool IsOver { get { return horse.IsDead(); } } } 

The main program class is very different. It's a static class and contains only static methods. The other classes (horse, game) are classes of which you create objects. You have one game, which has one horse and so on.

public static class Program { private static Game game; public static void Main() { game = CreateNewGame(); do { // get input // update game state } while (!game.IsOver); } private static Game CreateNewGame() { Horse horse; do { horse = CreateRandomHorse(); Console.WriteLine("Is this ok? : " + horse); // Calls horse.ToString() } while (!GetBoolInput()); // have a horse, can make a game return new Game(horse, 100); } } 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! This was really usefull.
Having someone correct and give tips is the most helpfull thing when you are just starting out on coding.
You are declaring horse in the do block, it should not be available outside this block. I think this has been the original authors issue, too.
Thanks, horse moved. It still doesn't work of course, there are lots of things missing from the loop, such as displaying the horse, etc. but now at least the scopes should be ok, which was one of the original issues.
Added a ToString() and a display. This is evolving to a mini C# tutorial , there are likely better places for those :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.