Saltar al contenido principal

 none
Make Boolean true available to whole application RRS feed

  • Pregunta

  • 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?
    domingo, 18 de agosto de 2019 21:55

Respuestas

  • 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());
          }
       }
    
       
    }
    

    lunes, 26 de agosto de 2019 4:01

Todas las respuestas

  • 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.

    lunes, 26 de agosto de 2019 2:35
    Moderador
  • 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());
          }
       }
    
       
    }
    

    lunes, 26 de agosto de 2019 4:01