global variable ....
-
hi all , I want to declar a varible (like int) as a golbal in Form1.cs.Can anyone guide me . thanks .
s_mostafa_h
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
-
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
-
I want to use it(this variable )in report Formula , is it possible ? regards ,
s_mostafa_h
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
-
I want to use it(this variable )in report Formula , is it possible ? regards ,
s_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
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 form2public void Calculate() { Form1 form1=new Form1(); form1.ShowDialog(); int result=new Formula().Calculate(form1.GetFormulaParameters()); }
-
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 form2public void Calculate() { Form1 form1=new Form1(); form1.ShowDialog(); int result=new Formula().Calculate(form1.GetFormulaParameters()); }
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, 2006s_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, 2006s_mostafa_h
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.
-
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.
-
surely , discreteval.Value = Chk1 is a value from Form1.cs . if not , it doesn't work.
s_mostafa_h