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. global variable ....

global variable ....

Scheduled Pinned Locked Moved C#
tutorial
10 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.
  • M Offline
    M Offline
    mostafa_h
    wrote on last edited by
    #1

    hi all , I want to declar a varible (like int) as a golbal in Form1.cs.Can anyone guide me . thanks .

    s_mostafa_h

    J B 2 Replies Last reply
    0
    • M mostafa_h

      hi all , I want to declar a varible (like int) as a golbal in Form1.cs.Can anyone guide me . thanks .

      s_mostafa_h

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

      C# doesn't allow global variables. Every field is contained in a class. If you need global-like access, you can stick a static field in a static class:

      public static class MyStuff
      {
      public static int MyVar = 15;
      }

      // To access it from anywhere:
      int val = MyStuff.MyVar;

      To be honest though, if you're needing lots of globals, you probably have flawed design. I find static classes to be a rare need; you only truely need them for functions that have zero reliance on other classes, zero dependencies, and each function is completely self-contained with no side effects.

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: And in this corner, the Party of Allah The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

      M 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        C# doesn't allow global variables. Every field is contained in a class. If you need global-like access, you can stick a static field in a static class:

        public static class MyStuff
        {
        public static int MyVar = 15;
        }

        // To access it from anywhere:
        int val = MyStuff.MyVar;

        To be honest though, if you're needing lots of globals, you probably have flawed design. I find static classes to be a rare need; you only truely need them for functions that have zero reliance on other classes, zero dependencies, and each function is completely self-contained with no side effects.

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: And in this corner, the Party of Allah The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

        M Offline
        M Offline
        mostafa_h
        wrote on last edited by
        #3

        I want to use it(this variable )in report Formula , is it possible ? regards ,

        s_mostafa_h

        J L 2 Replies Last reply
        0
        • M mostafa_h

          I want to use it(this variable )in report Formula , is it possible ? regards ,

          s_mostafa_h

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

          mostafa_h wrote:

          I want to use it(this variable )in report Formula , is it possible ?

          A variable in a report's formula? Sure, so long as you can put the values into the formula from code, there's nothing stopping you from using a variable as input. But I don't see how this relates to global variables.

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: And in this corner, the Party of Allah The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

          1 Reply Last reply
          0
          • M mostafa_h

            I want to use it(this variable )in report Formula , is it possible ? regards ,

            s_mostafa_h

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You can use it anywhere you like.

            1 Reply Last reply
            0
            • M mostafa_h

              hi all , I want to declar a varible (like int) as a golbal in Form1.cs.Can anyone guide me . thanks .

              s_mostafa_h

              B Offline
              B Offline
              beatles1692
              wrote on last edited by
              #6

              Using a global variable is not a good idea. I think that it's better to use parameter objects rather than global variables. For example if you have to pass x,y and z to a formula calculator you can have a object that gets x,y and z as parameters and then you can pass this object to a suitable metohd of your calculator. In form1 public FormulaParams GetFormulaParameters() { FormulaParams params=new FormulaParams(); params.x=int.Parse(this.txtX.Text); params.y=int.Parse(this.txtY.Text); params.z=int.Parse(this.txtZ.Text); return params; } In form2 public void Calculate() { Form1 form1=new Form1(); form1.ShowDialog(); int result=new Formula().Calculate(form1.GetFormulaParameters()); }

              M 1 Reply Last reply
              0
              • B beatles1692

                Using a global variable is not a good idea. I think that it's better to use parameter objects rather than global variables. For example if you have to pass x,y and z to a formula calculator you can have a object that gets x,y and z as parameters and then you can pass this object to a suitable metohd of your calculator. In form1 public FormulaParams GetFormulaParameters() { FormulaParams params=new FormulaParams(); params.x=int.Parse(this.txtX.Text); params.y=int.Parse(this.txtY.Text); params.z=int.Parse(this.txtZ.Text); return params; } In form2 public void Calculate() { Form1 form1=new Form1(); form1.ShowDialog(); int result=new Formula().Calculate(form1.GetFormulaParameters()); }

                M Offline
                M Offline
                mostafa_h
                wrote on last edited by
                #7

                Hi plz , read this :

                public bool Chk1;
                ...
                private void btnShowName_Click(object sender, EventArgs e)
                {
                if(CheckBox1.Checked)
                Chk1=true;
                sqlconn.Open();
                da = new SqlDataAdapter(strQuery, sqlconn);
                SqlCommandBuilder scb = new SqlCommandBuilder(da);
                da.Fill(DS.MyTable);//Ds is a DataSet
                crystalReport11.SetDataSource(DS);
                //definition Parameter for CR
                ParameterField paramfield = new ParameterField();
                ParameterFields paramfields = new ParameterFields();
                ParameterDiscreteValue discreteval = new ParameterDiscreteValue();
                paramfield.Name = "fieldName";
                discreteval.Value = Chk1 ;
                paramfield.CurrentValues.Add(discreteval);
                paramfields.Add(paramfield);
                crystalReportViewer1.ParameterFieldInfo = paramfields;
                crystalReportViewer1.ReportSource = crystalReport11;
                sqlconn.Close();}

                and Crystal Report , I create a parameter named :"fieldName" , and in Formula Editor , I wrote this : Global booleanVar Chk1; if Chk1=true then {MyTable.FirstName}={?fieldName} finally When I run the App. ,and I checked the CheckBox1, I have no report . (I want if I checked the CheckBox1 , all record of FirstName field is Shown) Where is My Wrong ??????? thanx a lot , Regards -- modified at 3:41 Wednesday 16th August, 2006

                s_mostafa_h

                B 1 Reply Last reply
                0
                • M mostafa_h

                  Hi plz , read this :

                  public bool Chk1;
                  ...
                  private void btnShowName_Click(object sender, EventArgs e)
                  {
                  if(CheckBox1.Checked)
                  Chk1=true;
                  sqlconn.Open();
                  da = new SqlDataAdapter(strQuery, sqlconn);
                  SqlCommandBuilder scb = new SqlCommandBuilder(da);
                  da.Fill(DS.MyTable);//Ds is a DataSet
                  crystalReport11.SetDataSource(DS);
                  //definition Parameter for CR
                  ParameterField paramfield = new ParameterField();
                  ParameterFields paramfields = new ParameterFields();
                  ParameterDiscreteValue discreteval = new ParameterDiscreteValue();
                  paramfield.Name = "fieldName";
                  discreteval.Value = Chk1 ;
                  paramfield.CurrentValues.Add(discreteval);
                  paramfields.Add(paramfield);
                  crystalReportViewer1.ParameterFieldInfo = paramfields;
                  crystalReportViewer1.ReportSource = crystalReport11;
                  sqlconn.Close();}

                  and Crystal Report , I create a parameter named :"fieldName" , and in Formula Editor , I wrote this : Global booleanVar Chk1; if Chk1=true then {MyTable.FirstName}={?fieldName} finally When I run the App. ,and I checked the CheckBox1, I have no report . (I want if I checked the CheckBox1 , all record of FirstName field is Shown) Where is My Wrong ??????? thanx a lot , Regards -- modified at 3:41 Wednesday 16th August, 2006

                  s_mostafa_h

                  B Offline
                  B Offline
                  beatles1692
                  wrote on last edited by
                  #8

                  Well I'm not working with CR anymore

                  Global booleanVar Chk1;

                  I can't see where your Chk1 value(the one in formula editor) is assigned. You create a ParameterDiscreteValue

                  ParameterDiscreteValue discreteval = new ParameterDiscreteValue();

                  and pass Chk1 value to it

                  discreteval.Value = Chk1 ;

                  but how can you tell the CR that this value should be assigned to your global variable? It seems that something is missing. I think that may be you should define a paramter named Chk1 and set its value.

                  M 1 Reply Last reply
                  0
                  • B beatles1692

                    Well I'm not working with CR anymore

                    Global booleanVar Chk1;

                    I can't see where your Chk1 value(the one in formula editor) is assigned. You create a ParameterDiscreteValue

                    ParameterDiscreteValue discreteval = new ParameterDiscreteValue();

                    and pass Chk1 value to it

                    discreteval.Value = Chk1 ;

                    but how can you tell the CR that this value should be assigned to your global variable? It seems that something is missing. I think that may be you should define a paramter named Chk1 and set its value.

                    M Offline
                    M Offline
                    mostafa_h
                    wrote on last edited by
                    #9

                    surely , discreteval.Value = Chk1 is a value from Form1.cs . if not , it doesn't work.

                    s_mostafa_h

                    M 1 Reply Last reply
                    0
                    • M mostafa_h

                      surely , discreteval.Value = Chk1 is a value from Form1.cs . if not , it doesn't work.

                      s_mostafa_h

                      M Offline
                      M Offline
                      mostafa_h
                      wrote on last edited by
                      #10

                      is there any help ?.....

                      s_mostafa_h

                      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