Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Whats the best way to make a global var?

I have an application in which I need to store a value.

This value must be accessable by the entire application.

E.G.

I have a main mdi form which launches a "choose site" form.

When the user selects a site, the form should update this variable and then close.

I need to be able to update this variable so that all other forms will be able to see it e.g. update the status bar in the main mdi form.

Could I make a singleton class to store this information (there will probably be more vars)?

or if I create a public property in the main MDI form, how can I update it from a child form e.g.

this.parent.site_code = "XYZ" does not seem to work.

Mike G
Friday, July 23, 2004

In C#, you would declare the variable as "static".  I'm sure there's an analog in VB.NET, probably Global.

Matt Conrad
Friday, July 23, 2004

The VB analog for 'static' is 'Shared'.

It seems like you're just popping up your form as a modal dialog box - why not something along these lines?

As taken from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsformclassshowdialogtopic2.asp

public void ShowMyDialogBox()
{
  Form2 testDialog = new Form2();

  // Show testDialog as a modal dialog and determine if DialogResult = OK.
  if (testDialog.ShowDialog(this) == DialogResult.OK)
  {
      // Read the contents of testDialog's TextBox.
      this.txtResult.Text = testDialog.TextBox1.Text;
  }
  else
  {
      this.txtResult.Text = "Cancelled";
  }
  testDialog.Dispose();
}

Greg Hurlman
Friday, July 23, 2004

You can declare the varialbe ae public static and the data type and if u want to initialise u can do it here.
in which ever page you want to assign values to it jus say
className.variableName = value;

this works perfect in C#.NET lot sure about the analog in VB.NET

Vasanth
Sunday, July 25, 2004

Eeep!

You are absolutely right Greg. It's was a long week. :-)

Thank you all for your help. I may have need in the future for a public static.

Mike G
Monday, July 26, 2004

/this is C# shit
but...

isn't gloal var global by default, unless declared as public?
It is in J++.

String string = "Morons took over the media and banks";

could also be public String string = "..";

without the sytem.window.forms.object.string.variable.trinvaliabe.isyellowish.andmoronically.idiotsh

extra  .verb stuff. that Microsft isn't famous for yet.

Satan
Sunday, August 08, 2004

*  Recent Topics

*  Fog Creek Home