none
Using the same variable in different methods. RRS feed

  • Question

  • VERY new here, so be nice please ^^"
    Essentially, my code is incomplete overall right now, I know, but I want to be able to use one variable in different methods. I aim to make a lil RPG ish thing to help me learn how to use C#

    Variable Turn set to 1 <|[This is the variable that I want to be able to use across different methods]

    Switch statement that considers the variable Turn
    Turn 1 - Pass it on to the method I am creating for turn 1
    Turn 2 - Pass it on to the method I am creating for turn 2
    so on and so fourth.

    Each turn should separately end with Turn += 1 so that each turn's personal method can increase the turn count. I can probably do it through the main turn command, true, but then I am not learning as much (and besides I can't mess about with time travel and messing with people's turns as easily, and what is an RPG without confusing time travel?)

    The current incomplete code is:

    using System;
    using TurnAction;
    
    
    namespace lilGame
    {
        class Program
        {
            public static void Main()
            {
                if(Turn.turns == 0)
                {
    
                }
    
            }
        }
    }
    
    
    
    
    
    
    
    

    for one of the programs and the other

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace TurnAction
    {
        //  public class Turn
        public class Turn
        {
    
            /*      
                    private int Turns { get; set; }
                    {
                        get
                        {
                            return turns; 
                       }
                    }
            */
    
            public double SetVariableTurns(double turns)
            {
                float turns = 1;
            }
    
            public void Left1(Turn turns)
            {
                Console.WriteLine("There's a big monster. A really big one. It eats you.");
                turns = 1;
                string Direction = Console.ReadLine();
            }
    
            public void Right1()
            {
                Console.WriteLine("You find a single, lonely gold coin. A bit like you, but it's actually worth something.");
                string Direction = Console.ReadLine();
            }
    
            public void Turn1()
            {
                string Direction = Console.ReadLine();
                 
    
                {
                    Console.WriteLine("You stand there contemplating how one might spell Left or Right, you are pretty sure it is not spelled {0}.", Direction);
                    string Wait = Console.ReadLine();
                    ;
                }
            }
        }
        

    (( Someone who was trying to help me lead me halfway through using two separate programs, one for managing the turn count and one for the separate turns, and led me to finish researching how to finish it myself, but it's already confusing me so I am probably going to undo it and move it all back to one program and just go with my original idea of a switch at the start and the separate turn methods after. ))

    Well, thank you very much for just looking at this mess. I intend to clean it up and move it all back to one program, but as I said I can't use one variable in multiple methods even when it is public, any help?

    Tuesday, June 26, 2018 10:57 AM

All replies

  • Currently you have variable local in your method. What you need is a class level variable so that it can be accessible in any method in that class. So you can write your class like:

    public class Turn
    {
       // class level variable
       public int turns = 0;
    
       public double SetVariableTurns(double turns)
       {
          // now set value in any method like below
          turns = 1;
       }

    and now in your main method you can use it :

    public static void Main()
    {
       Turn objTurn = new Turn();
       if(objTurn.turns == 0)
       {
    
       }
    
    }

    Hope it gives you idea.



    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Dolen Zhang Wednesday, June 27, 2018 8:17 AM
    Tuesday, June 26, 2018 11:10 AM
  • This seems to have fixed my issue I think. Now time to see if I can actually still use both different programs, since from the looks of it I seem to be able to. That would certainly make it cleaner overall. 

    Thank you!

    Tuesday, June 26, 2018 12:39 PM
  • Glad that it helped, you can mark the post as answer then.

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, June 27, 2018 11:59 AM