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. How to use public int?!!?!!? windows form.

How to use public int?!!?!!? windows form.

Scheduled Pinned Locked Moved C#
helptutorialquestionperformancelounge
6 Posts 5 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.
  • D Offline
    D Offline
    dimo1982
    wrote on last edited by
    #1

    Hello All I`m very new to the programming so this might be easy question to all of you, i`m hoping someone can help me out. So i`m trying to write very easy program in windows form but i have encountered the problem. lets say that i have an event on the button1 (CLICK) which should pick up a random number and hold it in the memory unless i click it again to change it. My problem is that i cannot figure out how to use this variable which has been set up in the first place with another event button2(Click) which will use this variable and do other task with using int previously declared. Short example:

    public void Random_Click(object sender, EventArgs e)
    {
    Random myRandom = new Random();
    int myNumber = myRandom.Next(1,10);
    }
    public void Button2_Click(object sender, EventArgs e)
    {
    MessageBox(myNumber.ToString());
    }

    This will not work and i cannot find the way to get it right plese help !!!

    I M L 3 Replies Last reply
    0
    • D dimo1982

      Hello All I`m very new to the programming so this might be easy question to all of you, i`m hoping someone can help me out. So i`m trying to write very easy program in windows form but i have encountered the problem. lets say that i have an event on the button1 (CLICK) which should pick up a random number and hold it in the memory unless i click it again to change it. My problem is that i cannot figure out how to use this variable which has been set up in the first place with another event button2(Click) which will use this variable and do other task with using int previously declared. Short example:

      public void Random_Click(object sender, EventArgs e)
      {
      Random myRandom = new Random();
      int myNumber = myRandom.Next(1,10);
      }
      public void Button2_Click(object sender, EventArgs e)
      {
      MessageBox(myNumber.ToString());
      }

      This will not work and i cannot find the way to get it right plese help !!!

      I Offline
      I Offline
      ichiganboya
      wrote on last edited by
      #2

      one way is to declare a global variable try this: public int myNumber; public void Random_Click(object sender, EventArgs e) { Random myRandom = new Random(); myNumber = myRandom.Next(1,10); } public void Button2_Click(object sender, EventArgs e) { MessageBox(myNumber.ToString()); } hope this helps

      D 1 Reply Last reply
      0
      • D dimo1982

        Hello All I`m very new to the programming so this might be easy question to all of you, i`m hoping someone can help me out. So i`m trying to write very easy program in windows form but i have encountered the problem. lets say that i have an event on the button1 (CLICK) which should pick up a random number and hold it in the memory unless i click it again to change it. My problem is that i cannot figure out how to use this variable which has been set up in the first place with another event button2(Click) which will use this variable and do other task with using int previously declared. Short example:

        public void Random_Click(object sender, EventArgs e)
        {
        Random myRandom = new Random();
        int myNumber = myRandom.Next(1,10);
        }
        public void Button2_Click(object sender, EventArgs e)
        {
        MessageBox(myNumber.ToString());
        }

        This will not work and i cannot find the way to get it right plese help !!!

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        Your problem is working out the SCOPE of your variable. If you declare it in the method it dies when the method completes. if you declare it provate in the form (I ususally put them just before the form_load method) then the variable will be available until you close the form. public clsLimitValue oValue { get; set; } private BindingSource oBS = new BindingSource(); private void frmNewLimit_Load(object sender, EventArgs e)

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • D dimo1982

          Hello All I`m very new to the programming so this might be easy question to all of you, i`m hoping someone can help me out. So i`m trying to write very easy program in windows form but i have encountered the problem. lets say that i have an event on the button1 (CLICK) which should pick up a random number and hold it in the memory unless i click it again to change it. My problem is that i cannot figure out how to use this variable which has been set up in the first place with another event button2(Click) which will use this variable and do other task with using int previously declared. Short example:

          public void Random_Click(object sender, EventArgs e)
          {
          Random myRandom = new Random();
          int myNumber = myRandom.Next(1,10);
          }
          public void Button2_Click(object sender, EventArgs e)
          {
          MessageBox(myNumber.ToString());
          }

          This will not work and i cannot find the way to get it right plese help !!!

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, maybe this[^] will inspire you. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          1 Reply Last reply
          0
          • I ichiganboya

            one way is to declare a global variable try this: public int myNumber; public void Random_Click(object sender, EventArgs e) { Random myRandom = new Random(); myNumber = myRandom.Next(1,10); } public void Button2_Click(object sender, EventArgs e) { MessageBox(myNumber.ToString()); } hope this helps

            D Offline
            D Offline
            dimo1982
            wrote on last edited by
            #5

            Hello still nothing cannot solve this problem, so what i tried is i tried declare variable as a (public int nameOfInteger) just before the method, in the begining oif the class and everywhere else. Still confused thats my code

            namespace ExerciseInt
            {
            public partial class Form1 : Form
            {
            int newNumber;

                public Form1()
                {
                    InitializeComponent();
                   
                }
            
                public void button1\_Click(object sender, EventArgs e)
                {
                    Random myRandom = new Random
                    myNumber = myRandom.Next(1,10);
                }
            
                private void button2\_Click(object sender, EventArgs e)
                {
                    MessageBox.Show(myNumber.ToString());
                }
            }
            

            }

            so when i click button1 variable should be set up and when click button2 i would like to see this variable in the message box. looking forward to hear from you.

            H 1 Reply Last reply
            0
            • D dimo1982

              Hello still nothing cannot solve this problem, so what i tried is i tried declare variable as a (public int nameOfInteger) just before the method, in the begining oif the class and everywhere else. Still confused thats my code

              namespace ExerciseInt
              {
              public partial class Form1 : Form
              {
              int newNumber;

                  public Form1()
                  {
                      InitializeComponent();
                     
                  }
              
                  public void button1\_Click(object sender, EventArgs e)
                  {
                      Random myRandom = new Random
                      myNumber = myRandom.Next(1,10);
                  }
              
                  private void button2\_Click(object sender, EventArgs e)
                  {
                      MessageBox.Show(myNumber.ToString());
                  }
              }
              

              }

              so when i click button1 variable should be set up and when click button2 i would like to see this variable in the message box. looking forward to hear from you.

              H Offline
              H Offline
              Huxley Stronghead
              wrote on last edited by
              #6

              Are you using some IDE? It would be helpful if you could give some error reports and I think I could think of one. Now, that would be newNumber and myNumber. Are those supposed to be the same? I think so. :^)

              I came, saw, and then coded.

              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