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. public variables

public variables

Scheduled Pinned Locked Moved C#
help
6 Posts 4 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
    ddpn42
    wrote on last edited by
    #1

    I have a variable defined as public in form1 and am trying to assign a value to it in form2, but when I try to run it I get this error message "The name 'x' does not exist in the current context" . I have tried to search in my books and in the help files but i am just missing. I am probably over thinking it but I just can't seem to find it.

    L S 2 Replies Last reply
    0
    • D ddpn42

      I have a variable defined as public in form1 and am trying to assign a value to it in form2, but when I try to run it I get this error message "The name 'x' does not exist in the current context" . I have tried to search in my books and in the help files but i am just missing. I am probably over thinking it but I just can't seem to find it.

      L Offline
      L Offline
      Le centriste
      wrote on last edited by
      #2

      Care to post code snippets? 1 with the variable declaration in form1 and another with the assignment statement in form2. -------- "I say no to drugs, but they don't listen." - Marilyn Manson

      D 1 Reply Last reply
      0
      • D ddpn42

        I have a variable defined as public in form1 and am trying to assign a value to it in form2, but when I try to run it I get this error message "The name 'x' does not exist in the current context" . I have tried to search in my books and in the help files but i am just missing. I am probably over thinking it but I just can't seem to find it.

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

        Declare an instance of form2, then pass in the variable value to a public method in form2 via a method in form1... --- public partial class Form1 : Form { Form2 form2; ... private void SendValueToForm2(variableValue) { form2.SetValue(variableValue) } --- HTH Jonny

        1 Reply Last reply
        0
        • L Le centriste

          Care to post code snippets? 1 with the variable declaration in form1 and another with the assignment statement in form2. -------- "I say no to drugs, but they don't listen." - Marilyn Manson

          D Offline
          D Offline
          ddpn42
          wrote on last edited by
          #4

          form1 varible declaration public class Form1 : System.Windows.Forms.Form { public Form2 Form2= new Form2 (); public int x = 0; .... } form2 assignment statement private void Form2_Load(object sender, System.EventArgs e) { //this is where i think the problem is x = 3; }

          P L 2 Replies Last reply
          0
          • D ddpn42

            form1 varible declaration public class Form1 : System.Windows.Forms.Form { public Form2 Form2= new Form2 (); public int x = 0; .... } form2 assignment statement private void Form2_Load(object sender, System.EventArgs e) { //this is where i think the problem is x = 3; }

            P Offline
            P Offline
            Praveen Nayak
            wrote on last edited by
            #5

            Whoa, hold on there. That code looks so... ok, see, if you want to access a public variable of form 1 in form 2, you an object of form2 must have a reference to an object of form1. So, the code should be: public class Form1 : System.Windows.Forms.Form { public Form2 Form2= new Form2 (this); public int x = 0; .... } // Form2 class public class Form2 { private Form1 owner; // Constructor public Form2( Form1 form1) { this.owner = form1; } private void Form2_Load(object sender, System.EventArgs e) { //this is where i know the problem is owner.x = 3; } ... } with this, the Form1 object's variable x will be set inside Form2 after Form2 loads. Please read up on the concepts of classes and objects. There has to be more to life than just this

            1 Reply Last reply
            0
            • D ddpn42

              form1 varible declaration public class Form1 : System.Windows.Forms.Form { public Form2 Form2= new Form2 (); public int x = 0; .... } form2 assignment statement private void Form2_Load(object sender, System.EventArgs e) { //this is where i think the problem is x = 3; }

              L Offline
              L Offline
              Le centriste
              wrote on last edited by
              #6

              Although Praveen response is perfectly good, I consider (and he probably also does, reading his comment on the first line) that you should review your design. You are implementing one of the worst design anti-pattern, tight coupling (Re-Coupling). In this design, 2 objects depend on each other. Form1 depends on Form2 which depends on Form1. You should implement something else, maybe event-base, or interface-based. -------- "I say no to drugs, but they don't listen." - Marilyn Manson

              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