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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Declaring and Passing value to public variable

Declaring and Passing value to public variable

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
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.
  • K Offline
    K Offline
    klaydze
    wrote on last edited by
    #1

    hi, I'm having a problem in declaring and passing a value to a public variable in C#.Net. I am a VB.Net user not a C#.Net user, i just thinking if i convert my VB.Net code to C#.Net and that's what I'm doing now. In VB.Net you can create a Module where you can declare all your public variables to use it again and again. My problem now is, how can i declare a public variable/s in C#.Net so that i can use it for many times and also to pass a value to that variable so that i can get the value i pass to public variable. Example. Public strFullName as String, where strFullName is the variable where i pass the FullName of the person who logged to the system. Thank You.

    Don't block the drive way of all the newbies in programming. :)

    V 1 Reply Last reply
    0
    • K klaydze

      hi, I'm having a problem in declaring and passing a value to a public variable in C#.Net. I am a VB.Net user not a C#.Net user, i just thinking if i convert my VB.Net code to C#.Net and that's what I'm doing now. In VB.Net you can create a Module where you can declare all your public variables to use it again and again. My problem now is, how can i declare a public variable/s in C#.Net so that i can use it for many times and also to pass a value to that variable so that i can get the value i pass to public variable. Example. Public strFullName as String, where strFullName is the variable where i pass the FullName of the person who logged to the system. Thank You.

      Don't block the drive way of all the newbies in programming. :)

      V Offline
      V Offline
      Vikram A Punathambekar
      wrote on last edited by
      #2

      Google not working for you?

      class YourClass
      {
      public string fullName;
      // other stuff goes here
      }

      Having said that, using public variables is a Bad Thing. You should use properties instead.

      class YourClass
      {
      private string _fullName;
      public string FullName
      {
      get { return _fullName; }
      set { _fullName = value; }
      }
      }

      Cheers, Vikram.


      "I will put my new found knolage to good use" - Captain See Sharp. "Every time Lotus Notes starts up, somewhere a puppy, a kitten, a lamb, and a baby seal are killed." - Gary Wheeler.

      K 1 Reply Last reply
      0
      • V Vikram A Punathambekar

        Google not working for you?

        class YourClass
        {
        public string fullName;
        // other stuff goes here
        }

        Having said that, using public variables is a Bad Thing. You should use properties instead.

        class YourClass
        {
        private string _fullName;
        public string FullName
        {
        get { return _fullName; }
        set { _fullName = value; }
        }
        }

        Cheers, Vikram.


        "I will put my new found knolage to good use" - Captain See Sharp. "Every time Lotus Notes starts up, somewhere a puppy, a kitten, a lamb, and a baby seal are killed." - Gary Wheeler.

        K Offline
        K Offline
        klaydze
        wrote on last edited by
        #3

        what's wrong with this? code of Class class getUserName { private String _UserName; public String UserName { get { return this._UserName; } set { this._UserName = value; } } } code of Button 1 in Form 1 i put the "myUsername" to Username inside the getUsername class so i can get the Username whereever i open a different form. private void button1_Click(object sender, EventArgs e) { getUsername objgetUsername = new getUsername(); objgetUsername.UserName = "myUsername"; } code of Button 2 in Form 2 here i will retrieve the myUsername to put it in the Messagebox to show the Username i used. private void button2_Click(object sender, EventArgs e) { getUsername objgetUsername = new getUsername(); MessageBox.Show(getUsername.UserName); } my problem now is, I'm getting a null value in Form 2 Button 2 everytime i click the Button 2. Any help out there. Thank You.

        if(you type your code here) { Messagebox.Show("You help me a lot!"); } else { You help me = null; }

        V 1 Reply Last reply
        0
        • K klaydze

          what's wrong with this? code of Class class getUserName { private String _UserName; public String UserName { get { return this._UserName; } set { this._UserName = value; } } } code of Button 1 in Form 1 i put the "myUsername" to Username inside the getUsername class so i can get the Username whereever i open a different form. private void button1_Click(object sender, EventArgs e) { getUsername objgetUsername = new getUsername(); objgetUsername.UserName = "myUsername"; } code of Button 2 in Form 2 here i will retrieve the myUsername to put it in the Messagebox to show the Username i used. private void button2_Click(object sender, EventArgs e) { getUsername objgetUsername = new getUsername(); MessageBox.Show(getUsername.UserName); } my problem now is, I'm getting a null value in Form 2 Button 2 everytime i click the Button 2. Any help out there. Thank You.

          if(you type your code here) { Messagebox.Show("You help me a lot!"); } else { You help me = null; }

          V Offline
          V Offline
          Vikram A Punathambekar
          wrote on last edited by
          #4

          Simple - the two objects you create in the two methods are different. Anyway, this is wrong on so many levels. Your classes should be nouns, not verbs. Also, you don't even need a new class to store the user name in this case. In your form, just declare private string _userName; Then, in your methods, you can read and modify _userName directly.

          Cheers, Vikram.


          "I will put my new found knolage to good use" - Captain See Sharp. "Every time Lotus Notes starts up, somewhere a puppy, a kitten, a lamb, and a baby seal are killed." - Gary Wheeler.

          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