Question Accessing variable value from another class?

Dragnorian

New member
Joined
Aug 14, 2017
Messages
3
Programming Experience
1-3
Hello! I am very new to C# and new to these forums so I apologize if this is the wrong area to post this. The reason I come to the forums is simply: I have no idea how to word this properly into Google. Anyways, I have created a separate class and created a sub method in that class that will take care of creating random integers. Here is the code for this part:
C#:
    class search
    {
        public static Int16 RandomEventModifier;
       public static string RandomEventName;
        //###########################################
        public static void RandomEvents()
        {
            Random eventmod = new Random();
            RandomEventModifier = 10;
        }
    }
}
Now, when I am in my main form, I can call up the RandomEventModifier variable but I am unable to get the correct value. The correct value of the variable is 10 as stated in the code at the near bottom. In the main form, I get a returned value of 0. Here is the code from where I call it:
C#:
        private void btnSearch_Click(object sender, EventArgs e)
        {
            MessageBox.Show(search.RandomEventModifier.ToString());
        }
Any suggestions?
 
I have created a separate class and created a sub method in that class
You've defined a class and declared a method in that class. "separate" is superfluous and "sub" is nonsensical.

Anyway, with regards to the issue, you're getting the value of that variable. If you get zero then the value is zero. There's no evidence in what you've posted that you ever call that RandomEvents method. If you haven't called the method then the code to set the variable to 10 has not been executed, so it's going to have its default value, which is zero.
 
jmcilhinney;8099 There's no evidence in what you've posted that you ever call that RandomEvents method. If you haven't called the method then the code to set the variable to 10 has not been executed said:
I know I never called the method but I didn't think I was suppose to call the method due to when I tried, I couldn't get the variable to go in(declaring inside method) without getting an error. I am completely new with classes and never dealt with them in VB so I apologize for my ignorance.
 
What JC is telling you is that the way your code is setup, your variable is not set to 10 unless you call search.RandomEvents(); so your code would need to look like this instead:

C#:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void buttonSearch_Click(object sender, EventArgs e)
        {
            search.RandomEvents();
            MessageBox.Show(search.RandomEventModifier.ToString());
        }
    }


    class search
    {
        public static Int16 RandomEventModifier;
        public static string RandomEventName;
        //###########################################
        public static void RandomEvents()
        {
            Random eventmod = new Random();
            RandomEventModifier = 10;
        }
    }

With that said, it's generally not great practice to do it the way you are. My assumption is you hope to eventually generate a random number rather than returning 10 all the time. A property is a better option for you. Below is a quick example.

C#:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void buttonSearch_Click(object sender, EventArgs e)
        {
            MessageBox.Show(search.RandomEventModifier.ToString());
        }
    }


    class search
    {        
        public static int RandomEventModifier
        {
            get
            {
                Random random = new Random();
                // Generate a random number between 1-10
                return random.Next(1, 10);
            }
        }
    }

Also, make sure you fully grasp what the "static" keyword really means as this can quickly get new developers into trouble. Good luck.
 
I couldn't get the variable to go in(declaring inside method) without getting an error.
Then you probably should have posted here about that and told us what the error message was.
I am completely new with classes and never dealt with them in VB
That's false. String is a class. Form is a class. TextBox and Button are classes. Almost everything is a class, besides the basic inbuilt data types like numbers. You use classes all the time in both VB and C#.
 
That's false. String is a class. Form is a class. TextBox and Button are classes. Almost everything is a class, besides the basic inbuilt data types like numbers. You use classes all the time in both VB and C#.
Oh really? I never actually knew this. The thing I was trying to do is when I right click my project name, I wanted to create a separate class file to keep the code clean. Thanks for telling me those were all classes as well.

Thanks man! Also, I am not getting a returned value of 10, I am getting a returned value of 0. If I was getting a returned value of 10 then I wouldn't be having an issue lol
 

Latest posts

Back
Top Bottom