Click here to Skip to main content
15,885,641 members

C# Global Variables Accessing

Chiranthaka Sampath asked:

Open original thread
I have created a class as the following it has one global variable.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LankaLab
{
    class dbConnect
    {

        public String strSuperPatID;

        public String StrSuperPatID
        {
            get { return strSuperPatID; }
            set { strSuperPatID = value; }
        }

                     
         }
}


Now I have created another class and I have created an instance of the dbConnect class.

C#
 public partial class frmFulBldCuntDet : Form
{

  private dbConnect dbConn = new dbConnect();

 }


Now I want to set values to the global variable in the dbConnect class from the frmBldCuntDet class. I already created the getter() & setter() methods. But now I want to add values to the strSuperPatID from the frmBldCuntDet and display it from another class ( haven't created yet). All I need is to use getter() & setter(0 methods.

Thank You!

Moved to question from OP "solution" - OriginalGriff

frmBldCuntDet class has the following variables and those use to generate a automatic patient id.


C#
 Class - 01
            
            public partial class frmFulBldCuntDet : Form
    {
            private dbConnect dbConn = new dbConnect();

            private String strID;
            private String strPatID;

            private int intYear;
            private String strYear;

            private int intMonth;
            private String strMonth;

         private void btnPrint_Click(object sender, EventArgs e)
        {
            intYear = DateTime.Now.Year;
            strYear = intYear.ToString();
            intMonth = DateTime.Now.Month;
            strID = cmbPatID.Text;

//MyMethod() in use.
            MyMethod();

      frmHBAIC HbIAC01 = new frmHBAIC();

      HbIAC01.show(); 

       }
         /* This MyMethod will pass the value of strPatID to the global variable StrSuperPatID */
           private string MyMethod() {

            return(dbConn.StrSuperPatID = strPatID);
        
        }

    }

 Class - 02

  public partial class frmHBAIC : Form
    {

     private dbConnect dbConn = new dbConnect();

   /* When the button1 is clicked I want to display the value of StrSuperPatID (global variable) in a messagebox.*/
   // I tried this as the follwoing but it didn't happen!
      private void button1_Click(object sender, EventArgs e)
        {
          MessageBox.show(dbConn.StrSuperPatID);  
        }

    }

  Class - 03 - This class includes the global variables and getter & setter metohds of its'.

  class dbConnect
    {

        public String strSuperPatID;

        public String StrSuperPatID
        {
            get { return strSuperPatID; }
            set { strSuperPatID = value; }
        }

                     
         }


I cannot find the problem yet that preventing passing the values to the global variable.

Hope that you will help me

Thank You!
Tags: C# (C# 3.0), Visual Studio, OOP

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900