Jump to content

C# function varibales

MisterWhite
Go to solution Solved by Nineshadow,

Guess is not initalized.

Try

string guess = "";

Is there a way not to declare variables in another function?

Lets say we have this simple example

public static void Main (string[] args){  int m;  Function(m);  Console.Write(m);}public static void Function(int m)// this place{  m=2*3;}

Is there a special variable declaration method to that i would not need to rewrite variable names int function

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

I don't quite get it...

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 comment
Share on other sites

Link to post
Share on other sites

This doesn't work because when Main runs the second function might not have. You need to declare the variable as a foo value to begin with. Also, why are you giving Function the variable m when m isn't created until it runs?

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

Another point is that as soon as you pass m into that function it's going to be passed by value and not reference - which from the way you are attempting to assign to it looks to me like you think it will be a reference.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

I don't quite get it...

I want to use all variables from main without writing them in new functions header

public static void Function(int m)// what i want is without "int m"

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

I want to use all variables from main without writing them in new functions header

public static void Function(int m)// what i want is without "int m"

Why use functions then?

It kinda defeats the purpose.

You could use global variables maybe.

 

 

Aaah. I get what you're trying to do.

Try

public static void Function(ref int m)

in the function declaration.

I think that's how references work in C#. In C++ it would be

int &m

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 comment
Share on other sites

Link to post
Share on other sites

 

I want to use all variables from main without writing them in new functions header

public static void Function(int m)// what i want is without "int m"

 

Unless they are global to the scope then you can't. You could use a lambda and capture the variable implicitly but that's about as close as it gets.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Unless they are global to the scope then you can't. You could use a lambda and capture the variable implicitly but that's about as close as it gets.

 

so it would be 

global int m;

?

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

so it would be 

global int m;

?

 

No just declare it external to the scope of Main but within the same class.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

All non-static methods within the class can use this

public class Example{    private int someVariable;    // ...}

All static and non-static methds within the class can use this

public class Example{    private static int someVariable;    // ...}

Using public vs private vs protected will change what is allowed to access them.

Link to comment
Share on other sites

Link to post
Share on other sites

No just declare it external to the scope of Main but within the same class.

class MainClass{  string guess; public static void main()  {     guess="Like this?";// cause it doesn't work  }}

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Try this:

public static void Main (string[] args){  int m;  Function(m);  Console.Write(m);}public static void Function(ref int m){  m=2*3;}

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 comment
Share on other sites

Link to post
Share on other sites

class MainClass{  string guess; public static void main()  {     guess="Like this?";// cause it doesn't work  }}

 

See my answer. The variable needs to be static as well.

Link to comment
Share on other sites

Link to post
Share on other sites

See my answer. The variable needs to be static as well.

i've tryed but got many errors, so ill stick with declaring all variables :D

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

Try this:

public static void Main (string[] args){  int m;  Function(m);  Console.Write(m);}public static void Function(ref int m){  m=2*3;}

btw, when calling function in main, i had to add "ref" to the name also.

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

some error occured

public static main(){string guess;string[] word= new string[40];int mistakes=0;int length;char[] empty = new char[50];int generate;allStuff(ref mistakes,ref length,ref empty,ref guess,ref generate,ref word);// ERROR  Use of unassigned local variable 'guess'}public static void allStuff(ref int mistakes, ref int length, ref char[] empty, ref string guess , ref int generate,ref string[] word){  // some stuff here}

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

btw, when calling function in main, i had to add "ref" to the name also.

 

Yes you are quite right, the ref keyword will take the address of the variable. You won't need it for objects as those are passed by ref by default.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Yes you are quite right, the ref keyword will take the address of the variable. You won't need it for objects as those are passed by ref by default.

any solution to earlier error?

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

any solution to earlier error?

 

Well... what do you think? Consider the error message provided. Why not look up the ref keyword and the string class and have a think.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Well... what do you think? Consider the error message provided. Why not look up the ref keyword and the string class and have a think.

even without those "ref" i get that message

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Guess is not initalized.

Try

string guess = "";

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 comment
Share on other sites

Link to post
Share on other sites

 

Guess is not initalized.

Try

string guess = "";

Worked

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Guess is not initalized.

Try

string guess = "";

 

I was letting him work through things himself. In any event it's better to use the constant string.Empty.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

I was letting him work through things himself. In any event it's better to use the constant string.Empty.

We don't have that in C++ D:

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 comment
Share on other sites

Link to post
Share on other sites

I was letting him work through things himself. In any event it's better to use the constant string.Empty.

i didn't have this error until i put all code in the function, so never thought it could be the case

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

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

×