Jump to content

Can I use a String variable to Chose which Int I want to effect? C#

RainColt

Im really new to coding and I've got like 40 int variables that I want to be able to manipulate on a button press but I don't want to write like 40 if statements to change them, I've simplified it down to 3 in the example but,

Can I use the CurrentInt String within the Maths() method to chose what int variable I want to affect without like 100 if statements? 

 

It is code running in UnityEngine but I feel like this problem is more or less general coding than anything Unity specific. 

 

public Button ClassOption1, ClassOption2, ClassOption3;
int Comp, Eleg, Pres;
public void ButtonOptions()
{
switch (CurrentButton)
        {
         //Sends a string to Maths() method on button press
            case "BUTTON1":
                ClassOption1.onClick.AddListener(() => Maths("Comp"));
                ClassOption2.onClick.AddListener(() => Maths("Eleg"));
                ClassOption3.onClick.AddListener(() => Maths("Pres"));
                break;
}
 
public void Maths(string CurrentInt)
    { 
    int [insert Variable name here] +=1;
   ///Summary
   ///can I use the CurrentInt String to chose what variable to affect without using like 40 if
   ///statements?
   ///Summary
   
    }

 

Why do you always die right after I fix you?

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, CUDAcores89 said:

-Snip-

I'll take that as a Probably not.

But I got on the Unity Forums was 

 

int i;

}

if(int.TryParse (String, out i)) 

{

so Imma look into that and see what I can work with. Don't have great hopes.

Why do you always die right after I fix you?

 

Link to comment
Share on other sites

Link to post
Share on other sites

There are a whole lot of different ways, but using a reference to the variable might be the easiest for a beginner to grasp and play with:

public Button ClassOption1, ClassOption2, ClassOption3;
int Comp, Eleg, Pres;
public void ButtonOptions()
{
switch (CurrentButton)
        {
         //Sends a string to Maths() method on button press
            case "BUTTON1":
            	//Instead of giving Maths() the value within the variable, we instead
                //give the "address" to the variable, so Maths() can directly modify
                //the variable itself
                ClassOption1.onClick.AddListener(() => Maths(ref Comp));
                ClassOption2.onClick.AddListener(() => Maths(ref Eleg));
                ClassOption3.onClick.AddListener(() => Maths(ref Pres));
                break;
}

//Don't forget the "ref"-keyword here!
public void Maths(ref int intVariable)
    { 
    intVariable += 1;
    }

 

I recommend reading https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

You can use a design pattern such as the state pattern. That will eliminate if and else but still, you need to code for each neccesary cases. However, unlike a hundred if and Else or switch statements, state pattern produces much cleaner, coherent, and comprehensible code which is easy to maintain. 

https://en.m.wikipedia.org/wiki/State_pattern

 

I actually made a reference guide on software design patterns based on materials I learn from a recent college programming course. You can take a look

https://github.com/GAO23/CSE_219_Final_Project/blob/master/README.md

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

I think I had a similar situation when building an app as an exercise. The problem was I had 26 buttons that needed to manipulate a specific element on a GUI. However, rather than using the editor to create the default handler for each button, I named the buttons in a specific way so I could derive some unique information about it and use a single event handler.

 

So if it helps, I have a blog about it at

Remember to take the principle of it and see if it applies

 

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

×