Jump to content

c# basic out of scope problem

Go to solution Solved by noisebomb44,
37 minutes ago, Nineshadow said:

Well yes , I was writing in C++ , my bad. I'm just used to writing everything in C++ and forgot this was about C#. Anyway, I think it would be like this :


int[] test = new int[9];
public static void scoreCreate()
        {
            test[0] = 0;
			//etc

            string[] name = new string[9];
            name[0] = "noname";
			//etc

            updateScores()

        }
static void updateScores()
{
		if (5 > test[0])
            {
				etc
			}
}

 

Didnt work, BUT, i found the answer on annother site, by using classes

 

working code (even simpler example this time):

		public static class GlobalValues
        {
            public static int testvalue = 1;

        }

        public static void test()
        {
            Console.WriteLine("numer " + GlobalValues.testvalue);
            Console.ReadLine();
        }

 

Thanks for all the help though!

So i have run into this really basic problem, but i cant see anything wrong with it, i declare a few arrays in public, and then when i try to acces them again i cant, and instead get the "out of scope error" (excact error: The name "blabla" doesnt excist in the current context)

public static void scoreCreate()
        {

            int[] test = new int[9];
            test[0] = 0;
			//etc

            string[] name = new string[9];
            name[0] = "noname";
			//etc

            updateScores()

        }
static void updateScores()
{
		if (5 > test[0])
            {
				etc
			}
}

Ignore any normal misstakes i made, i remade my code to make it easier to read

BTW im using console in visual studio

Long live Stalin, he loves you; sing these words, or you know what he’ll do!

Link to comment
https://linustechtips.com/topic/541216-c-basic-out-of-scope-problem/
Share on other sites

Link to post
Share on other sites

It's because the array test doesn't exist in the global scope. Instead , you just declared it inside the scoreCreate() function. You can either make it a global variable or pass it via the argument list to the updateScores() function , which would be pretty much unnecessary. Just make it a global variable.

Something like this :

int *test;
void scoreCreate()
{
test = new int[9];
/*
rest of the code here
*/
updateScores();
}
void updateScores()
{
/*
code here
/*
test[0]
/*
code here
*/
}

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

21 minutes ago, Nineshadow said:

It's because the array test doesn't exist in the global scope. Instead , you just declared it inside the scoreCreate() function. You can either make it a global variable or pass it via the argument list to the updateScores() function , which would be pretty much unnecessary.

Not sure what i did wrong, but 

 

int test;
public static void scoreCreate()

int[] test = new int[9];
test[0] = 1

static void updateScores()
{
 if (5 > test[0])

}
}

(i simplified it a little bit more)

Doesnt work, still the out of scope error

 

Im guessing the "int *test;" was a misstake, since adding the " * " only made it worse, and gave me this error: "Pointers and fixed size buffers may only be used in an unsafe context"

Long live Stalin, he loves you; sing these words, or you know what he’ll do!

Link to post
Share on other sites

Just now, noisebomb44 said:

Not sure what i did wrong, but 

 


int test;
public static void scoreCreate()

int[] test = new int[9];
test[0] = 1

static void updateScores()
{
 if (5 > test[0])

}
}

Doesnt work

 

Im guessing the "int *test;" was a misstake, since adding the " * " only made it worse, and gave me this error: "Pointers and fixed size buffers may only be used in an unsafe context"

Well yes , I was writing in C++ , my bad. I'm just used to writing everything in C++ and forgot this was about C#. Anyway, I think it would be like this :

int[] test = new int[9];
public static void scoreCreate()
        {
            test[0] = 0;
			//etc

            string[] name = new string[9];
            name[0] = "noname";
			//etc

            updateScores()

        }
static void updateScores()
{
		if (5 > test[0])
            {
				etc
			}
}

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

37 minutes ago, Nineshadow said:

Well yes , I was writing in C++ , my bad. I'm just used to writing everything in C++ and forgot this was about C#. Anyway, I think it would be like this :


int[] test = new int[9];
public static void scoreCreate()
        {
            test[0] = 0;
			//etc

            string[] name = new string[9];
            name[0] = "noname";
			//etc

            updateScores()

        }
static void updateScores()
{
		if (5 > test[0])
            {
				etc
			}
}

 

Didnt work, BUT, i found the answer on annother site, by using classes

 

working code (even simpler example this time):

		public static class GlobalValues
        {
            public static int testvalue = 1;

        }

        public static void test()
        {
            Console.WriteLine("numer " + GlobalValues.testvalue);
            Console.ReadLine();
        }

 

Thanks for all the help though!

Long live Stalin, he loves you; sing these words, or you know what he’ll do!

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×