Answered by:
Make Boolean true available to whole application

Question
-
My form application open to one of two forms, depending on a Boolean value queried from Program.cs I have
public static Boolean object;
in multiple forms.
If true, I assign in Program.cs
object= Convert.ToBoolean(Reader["object"])
In any other form object is false. How can I get all forms to see true?- Moved by Sara LiuMicrosoft contingent staff Monday, August 26, 2019 2:03 AM
Answers
-
Greetings Richard.
I realise you have probably moved on from this one by now, but I think I can guess your mistake.
If you have that line declaring a static boolean in each class, that means each class will have its own different static variable. So changing one of them won't affect the others. You need to declare the variable only once, in one class, and access it via the class name everywhere else.
A common way to deal with this situation is to put all your static variables together in a class that exists just for this purpose. Here's an example, using a static class called 'Globals'.
using System; using System.IO; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { Output output = new Output(); // Prefix the variable name with the class name 'Globals'. Globals.MyOption = true; output.ShowMyOption(); Globals.MyOption = false; output.ShowMyOption(); } } public static class Globals { public static bool MyOption { get; set; } } public class Output { public void ShowMyOption() { // And the 'Globals' prefix again. Console.WriteLine(Globals.MyOption.ToString()); } } }
- Marked as answer by RichardDunneBSc Monday, August 26, 2019 11:20 AM
All replies
-
Hi,
You defined a Boolean field in Program.cs. But what does it have to do with form?
And first, you can't name the field "object". It's a class.
Second, what is "Reader["object"]"? Where did you define it?
Last, could you please explain the following sentence in detail?
>> In any other form object is false. How can I get all forms to see true?
Regards,
Kyle
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Edited by Kyle Wang - MSFTMicrosoft contingent staff, Moderator Monday, August 26, 2019 2:36 AM
-
Greetings Richard.
I realise you have probably moved on from this one by now, but I think I can guess your mistake.
If you have that line declaring a static boolean in each class, that means each class will have its own different static variable. So changing one of them won't affect the others. You need to declare the variable only once, in one class, and access it via the class name everywhere else.
A common way to deal with this situation is to put all your static variables together in a class that exists just for this purpose. Here's an example, using a static class called 'Globals'.
using System; using System.IO; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { Output output = new Output(); // Prefix the variable name with the class name 'Globals'. Globals.MyOption = true; output.ShowMyOption(); Globals.MyOption = false; output.ShowMyOption(); } } public static class Globals { public static bool MyOption { get; set; } } public class Output { public void ShowMyOption() { // And the 'Globals' prefix again. Console.WriteLine(Globals.MyOption.ToString()); } } }
- Marked as answer by RichardDunneBSc Monday, August 26, 2019 11:20 AM