Ask Experts Questions for FREE Help !
Ask
    WolfHybrid23's Avatar
    WolfHybrid23 Posts: 2, Reputation: 1
    New Member
     
    #1

    Sep 11, 2017, 10:19 AM
    CSharp Help
    So I keep getting an error when I try to access a variable in a different class, Every time I try to set it the compiler thinks I'm trying to get its value then it throws the NullPointerException and says I need an object reference to be able to use this var. Well once I made an object reference it throws the error "This var cannot be accessed with an instance reference" so basically its telling me to do the opposite of what I'm doing every time no matter what.

    I would like a fast reply because this is part of a 7 day challenge, Here's my code:
    In GlobalVars.cs:
    Code:
    namespace Adventure{
        public class GlobalVars
        {
            public static bool[] Been;
            public static string[] Inv;
            public static int[] Inventory;
            public static string Name;
            public static int Money;
            public static int Exp;
            public static int Lvl;
            public static bool[] Items;
        }
    }
    In Rooms/MainMenu.cs:
    Code:
    namespace Adventure.Rooms{
        public class MainMenu
        {
            public static GlobalVars Variables = new GlobalVars();
            public static void Main()
            {
                for(int i=0; i<7; i+=1)
                {
                    Variables.Inventory[i] = 0; //Here's where the error occures
                    Variables.Inv[i] = "Nothing";
                }
                for(int i=0; i<40; i+=1)
                {
                    Variables.Been[i] = false;
                }
                for (int i = 0; i < 100; i += 1)
                {
                    Variables.Items[i] = true;
                }
                Console.WriteLine("The Adventure");
                Console.WriteLine("Made by Wolf Game Studios.");
                Console.WriteLine("Made in 7 days");
                Console.WriteLine("");
                Console.WriteLine("What is your first name?");
                GlobalVars.Name = Console.ReadLine();
                YHouse.Bedroom();
            }
        }
    }
    The Error Details (Provided by Visual Studio):
    Code:
    Severity	Code	Description	Project	File	Line	Suppression StateError	CS0176	Member 'GlobalVars.Inventory' cannot be accessed with an instance reference; qualify it with a type name instead	Adventure	C:\Users\WolfH\source\repos\Adventure\Adventure\Rooms\MainMenu.cs	16	Active
    Thank You in advance

Check out some similar questions!

Csharp and Asp.net [ 0 Answers ]

Connection string can be programmatically retrieved from web.config using configuration class? True or false


View more questions Search
 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.