❔ How to do global variable?

I would like to use the number that generates in Form2_Load() and work with it somewhere else. Is there any solution?
No description
26 Replies
_apix
_apix13mo ago
My approach to this would be to use the out parameter to reference the number when calling Form2_Load(), but this might not be applicable in your case
ZacharyPatten
ZacharyPatten13mo ago
1. you should avoid using static especially in winforms code 2. you already made the fields public so they are accessible from other scopes outside of just class Form2's code since they fields are already public... you just need to access them 3. in general you should abstract game/business logic from UI code. you would likely benefit from these fields exist in another class and not inside the code of a Form
♡Mr. L0stík♡
♡Mr. L0stík♡OP13mo ago
so what should I do?
ZacharyPatten
ZacharyPatten13mo ago
where are you calling new Form2()?
♡Mr. L0stík♡
♡Mr. L0stík♡OP13mo ago
I'm not because I'm programming in framework oh wait
ZacharyPatten
ZacharyPatten13mo ago
yes you are If you are not using git source control, I highly recommend you start using it. It will help you learn how to code faster, expecially for Winforms projects
♡Mr. L0stík♡
♡Mr. L0stík♡OP13mo ago
In form1
No description
ZacharyPatten
ZacharyPatten13mo ago
and what do you see when you write simulator. after simulator.Show()?
♡Mr. L0stík♡
♡Mr. L0stík♡OP13mo ago
nothing more. Just to open new window
ZacharyPatten
ZacharyPatten13mo ago
share a screenshot o wait you made them static as mentioned... they likely should not be static but since you made them static, what do you see when you write Form2. after simulator.Show() you seem to be struggling with the difference between static and non-static
♡Mr. L0stík♡
♡Mr. L0stík♡OP13mo ago
I already deleted the static As you said
ZacharyPatten
ZacharyPatten13mo ago
if you deleted the static then you should see simulator.speed for example
♡Mr. L0stík♡
♡Mr. L0stík♡OP13mo ago
yes I can But I need the speed variable in Form2. I need the value of speed further
ZacharyPatten
ZacharyPatten13mo ago
rephrase your question. that doesn't make much sense
♡Mr. L0stík♡
♡Mr. L0stík♡OP13mo ago
I need the value of speed = random.Next(600, 1400); out of the Form_Load()
ZacharyPatten
ZacharyPatten13mo ago
yes. simulator.speed will have that value. you need to make sure you access it at the appropriate time this is where isolating UI and business logic can be a benefit
♡Mr. L0stík♡
♡Mr. L0stík♡OP13mo ago
No. That's not I mean something else. I'll send you code public string[] listpar; public string[] speedlist; public static int speed; public static int azimute; public static int attitude; public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { breefing(); speed = random.Next(600, 1400); azimute = random.Next(0, 365); attitude = random.Next(100, 6000); string xd = speed.ToString(); specbreef.Text = speed + "km/h, heading " + azimute + ", with attitude " + attitude; return; } List<string> listpick = new List<string>(); listpick.Add(speed + "km/h " + azimute + "az " + attitude + "m ");
ZacharyPatten
ZacharyPatten13mo ago
please $codegif when sending code
ZacharyPatten
ZacharyPatten13mo ago
but I still don't understand what your current issue is. you will need to elaborate
♡Mr. L0stík♡
♡Mr. L0stík♡OP13mo ago
I need the speed add out of void to that list under that void But I can't add the list into the void
ZacharyPatten
ZacharyPatten13mo ago
there seems to be a language barrier. your comment doesn't make sense your code is wrong you cannot have code outside of methods you have to include listpick.Add(speed + "km/h " + azimute + "az " + attitude + "m "); in the body of a method that is just how code works
♡Mr. L0stík♡
♡Mr. L0stík♡OP13mo ago
When I used the listpick.add in the method, I couldn't get the value out of the method even though I added it to the list. Can't the list do something about it?
ZacharyPatten
ZacharyPatten13mo ago
I would recommend you go back and do the $helloworld tutorials. You are trying to code a winforms project, but you seem lack the understanding of the basics of OOP principles. You can't really get anywhere in Winforms if you don't know how to make classes and access their members properly
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server