Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Forms & variables & suchlike

Forms & variables & suchlike

Scheduled Pinned Locked Moved C#
csharptutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    surgeproof
    wrote on last edited by
    #1

    Hiya. I'm using Visual C# - does anyone know how to declare a variable in say FrmMain - int myVar = 6; and then in a child of FrmTempTools set myVar to a value so i can read it again in my main form? I have tried and can only get meesages such as undeclared variable in FrmTempTools. Thanks anyone, surgeproof.

    J 1 Reply Last reply
    0
    • S surgeproof

      Hiya. I'm using Visual C# - does anyone know how to declare a variable in say FrmMain - int myVar = 6; and then in a child of FrmTempTools set myVar to a value so i can read it again in my main form? I have tried and can only get meesages such as undeclared variable in FrmTempTools. Thanks anyone, surgeproof.

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      Make the variable as public, or create a public getter property for the variable. Then just pass the Form instance to your FrmTempTools child.

      public class FrmMain : Form
      {
      public int myVar = 6;

      public FrmMain()
      {
      ChangeTheValue();
      }

      private void ChangeTheValue()
      {
      ChildOfFrmTempTools t = new ChildOfFrmTempTools(this); // pass this class

        Console.WriteLine(myVar); // outputs "12"
      

      }
      }

      public class ChildOfFrmTempTools
      {
      public ChildOfFrmTempTools(FrmMain mainInstance)
      {
      mainInstance.myVar = 12;
      }
      }

      Also note you can use the ref or out keywords to pass the variable by reference, rather than the default of passing by value. For instance

      public void Test()
      {
      int i = 6;
      Modify(ref i);
      Console.WriteLine(i); // outputs 12
      }

      public void Modify(ref int i)
      {
      i = 12;
      }

      --------------------------- He who knows that enough is enough will always have enough. -Lao Tsu

      S 2 Replies Last reply
      0
      • J Judah Gabriel Himango

        Make the variable as public, or create a public getter property for the variable. Then just pass the Form instance to your FrmTempTools child.

        public class FrmMain : Form
        {
        public int myVar = 6;

        public FrmMain()
        {
        ChangeTheValue();
        }

        private void ChangeTheValue()
        {
        ChildOfFrmTempTools t = new ChildOfFrmTempTools(this); // pass this class

          Console.WriteLine(myVar); // outputs "12"
        

        }
        }

        public class ChildOfFrmTempTools
        {
        public ChildOfFrmTempTools(FrmMain mainInstance)
        {
        mainInstance.myVar = 12;
        }
        }

        Also note you can use the ref or out keywords to pass the variable by reference, rather than the default of passing by value. For instance

        public void Test()
        {
        int i = 6;
        Modify(ref i);
        Console.WriteLine(i); // outputs 12
        }

        public void Modify(ref int i)
        {
        i = 12;
        }

        --------------------------- He who knows that enough is enough will always have enough. -Lao Tsu

        S Offline
        S Offline
        surgeproof
        wrote on last edited by
        #3

        thanks. i think i get it now! ;) surgeproof.

        1 Reply Last reply
        0
        • J Judah Gabriel Himango

          Make the variable as public, or create a public getter property for the variable. Then just pass the Form instance to your FrmTempTools child.

          public class FrmMain : Form
          {
          public int myVar = 6;

          public FrmMain()
          {
          ChangeTheValue();
          }

          private void ChangeTheValue()
          {
          ChildOfFrmTempTools t = new ChildOfFrmTempTools(this); // pass this class

            Console.WriteLine(myVar); // outputs "12"
          

          }
          }

          public class ChildOfFrmTempTools
          {
          public ChildOfFrmTempTools(FrmMain mainInstance)
          {
          mainInstance.myVar = 12;
          }
          }

          Also note you can use the ref or out keywords to pass the variable by reference, rather than the default of passing by value. For instance

          public void Test()
          {
          int i = 6;
          Modify(ref i);
          Console.WriteLine(i); // outputs 12
          }

          public void Modify(ref int i)
          {
          i = 12;
          }

          --------------------------- He who knows that enough is enough will always have enough. -Lao Tsu

          S Offline
          S Offline
          surgeproof
          wrote on last edited by
          #4

          On trying to follow through and adapt your code, i only get this error: No overload for method 'bladeblahFormName' takes '1' arguments. Any ideas? thanks.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups