Click here to Skip to main content
15,896,063 members
Home / Discussions / C#
   

C#

 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 0:26
mveOriginalGriff14-Mar-19 0:26 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Eddy Vluggen14-Mar-19 3:38
professionalEddy Vluggen14-Mar-19 3:38 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Richard MacCutchan13-Mar-19 23:52
mveRichard MacCutchan13-Mar-19 23:52 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion13-Mar-19 23:59
Brian_TheLion13-Mar-19 23:59 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 0:28
mveOriginalGriff14-Mar-19 0:28 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Richard MacCutchan14-Mar-19 1:51
mveRichard MacCutchan14-Mar-19 1:51 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 0:28
Brian_TheLion14-Mar-19 0:28 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 0:44
mveOriginalGriff14-Mar-19 0:44 
That'll work, but it can be improved.
But before we get to that, please do us all a favour, and start formatting your code!
It's easy to do, just highlight it and click the "code" widget above the text box.
Then this:
public class Program
{
public static void Main(string[] args)
{
Program prog = new Program();


prog.ReadHeader();
prog.ReadVerbs();
prog.ReadNouns();
prog.ReadObjects();
prog.ReadStartRoom();

Retains its formatting and engages the syntax highlighter:
C#
public class Program
    {
        public static void Main(string[] args)
         {
            Program prog = new Program();
    

           prog.ReadHeader();
           prog.ReadVerbs();
           prog.ReadNouns();
           prog.ReadObjects();
           prog.ReadStartRoom();
           prog.ReadRooms();
           prog.ReadMessages();


Now to improvements Laugh | :laugh:
Does Main have anything to do with your game (I assume it's a game?)?
Probably not, so leave it in it's own class, and add a new Game class:
C#
public class Program
   {
   public static void Main(string[] args)
      {
      Game theGame = new Game();
      theGame.Start();
      }

C#
public class Game
    {                   
    public void Start()
        {
        ReadHeader();
        ReadVerbs();
        ReadNouns();
        ReadObjects();
        ReadStartRoom();
        ReadRooms();
        ReadMessages();
        ...
        }
    public void ReadHeader()
        {
        Console.WriteLine("ReadHeader");
        }
    ...
    }
Now Game can start to take care of itself, and nothing in Main or program can be affected. You can change the whole game without changing Main at all!

Now start thinking about your methods like ReadHeader. What is it supposed to do? It reads something, but it either needs to store it as part of the instance in a class level variable:
C#
public class Game
    {                  
    private Word[] words;
    public void Start()
        {
        ...
Or it needs to return a value:
C#
public class Game
    {                   
    public void Start()
        {
        Word[] words = ReadWords();
        ...

    public Word[] ReadWords()
        {
        ...
        return words;
        }
...
Think carefully about the data your program will use, and how it needs to "flow" around the application - you need to work it out fairly well before you start coding or you will have massive changes to make later on!

Oh, and I'd totally agree with the Petzold recommendation - you seem to be trying to run before you can walk, and that ends up just confusing most people.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!

GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 1:09
Brian_TheLion14-Mar-19 1:09 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 1:19
Brian_TheLion14-Mar-19 1:19 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Richard MacCutchan14-Mar-19 1:54
mveRichard MacCutchan14-Mar-19 1:54 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 1:57
Brian_TheLion14-Mar-19 1:57 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Richard MacCutchan14-Mar-19 2:16
mveRichard MacCutchan14-Mar-19 2:16 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 2:22
Brian_TheLion14-Mar-19 2:22 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Richard MacCutchan14-Mar-19 2:25
mveRichard MacCutchan14-Mar-19 2:25 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 2:01
mveOriginalGriff14-Mar-19 2:01 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 2:13
Brian_TheLion14-Mar-19 2:13 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 2:31
mveOriginalGriff14-Mar-19 2:31 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 12:27
Brian_TheLion14-Mar-19 12:27 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 21:59
mveOriginalGriff14-Mar-19 21:59 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 23:43
Brian_TheLion14-Mar-19 23:43 
GeneralRe: Trying to find the problem in this simple C$ program Pin
OriginalGriff14-Mar-19 23:47
mveOriginalGriff14-Mar-19 23:47 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion15-Mar-19 0:53
Brian_TheLion15-Mar-19 0:53 
GeneralRe: Trying to find the problem in this simple C$ program Pin
Brian_TheLion14-Mar-19 1:40
Brian_TheLion14-Mar-19 1:40 
QuestionRe: Trying to find the problem in this simple C$ program Pin
Luc Pattyn14-Mar-19 7:09
sitebuilderLuc Pattyn14-Mar-19 7:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.