❔ 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?
26 Replies
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
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
so what should I do?
where are you calling
new Form2()
?I'm not because I'm programming in framework
oh wait
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 projectsIn form1
and what do you see when you write
simulator.
after simulator.Show()
?nothing more. Just to open new window
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-staticI already deleted the static
As you said
if you deleted the
static
then you should see simulator.speed
for exampleyes I can
But I need the speed variable in Form2. I need the value of speed further
rephrase your question. that doesn't make much sense
I need the value of speed = random.Next(600, 1400); out of the Form_Load()
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 benefitNo. 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 ");
please $codegif when sending code
but I still don't understand what your current issue is. you will need to elaborate
I need the speed add out of void to that list under that void
But I can't add the list into the void
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 worksWhen 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?
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
Written interactive course https://learn.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/tutorials/
Videos https://dotnet.microsoft.com/learn/videos
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.