I need a variable whose value is known in all program code.
No it's not a constant, it's a variable whose value changes, but i need to access to its value in other program parts.
If i create a class with a property, everytime i change module i need to create a class reference again, and the property is reset to its default value, i need to keep its value all along the program life, this is simple in all other programing languages, but in C# i didn't manage to do this.
have you tried creating a public sealed static class with a public static property to get and set the value for that variable. if the class is sealed and static then you don't have to create a reference, all you need is classname.propertyname ....
I did not see any reference to 'public sealed static classes' (uufff..., i'm tired) anywhere in the documentation.
In Visual FoxPro i would do: 'addproperty(_application,PropertyName)', anywhere in the code, and voilá i would have a property that i could access at any part of the code simply by writing:
_application.Propertyname = "value"
this.text1.value = _application.Propertyname
Global variables, or in this case properties, are a means to an end, bad use of global variables are indication of poor design.
I didn't say all uses were bad. I said "generally" they were bad. Every time I have the urge to use a global variable I sit and think if it is really really necessary first. Most of the time I come up with a better solution that doesn't involve global variables. These days I rarely think of global variables at all.
Next, properties do not have values. In C# properties are methods dressed up in syntactic sugar. Any value they appear to have is a result of the value that is returned from the underlying method.
And as we now see: C# support them
It doesn't. It is just sugar dressing. It just appears to support them in a contrived way.
That is a good candidate for something that should be in the app.config file. I have a configuration class in my applications that convert and cache the values from the config file so they are available throughout. In fact, I often have several configuration handling classes because the application has many subsystems that need handling separately.
It is in the same directory as the executable. It will have the same name as the EXE with a .config extension added. You can override that, but that is the default for .NET applications. Or are you not using the builtin configuration classes for some reason?
If you are not using the inbuilt method then how would you be intending to populate the initial value of your global variable?
The app.config file defaults to being in the same folder as the .EXE. If you didn't want to use the inbuilt configuration handler classes, it's a trivial matter to get the app's, or any assembly for that matter, path that it was loaded from. For example, Application.StartupPath returns the path to the folder that the .EXE was launched from. Couple that with Path.Combine and you can build a fully qualified path to any file or folder in your application.
Dave Kreskowiak Microsoft MVP
Visual Developer - Visual Basic 2006, 2007
There used to be a little tool by Lutz Roeder[^] that was great for that... I'm not seeing it now. He used to have Reflector and Documentor. I can't find it now, but I have a copy of it. Mail me at firstname.lastname@example.org and I'll send you a copy.
Here is my situation. I have several controls on a form that I would like the end user to be able to drag around the form. However, the DragOver Event in the DragDrop Event handler does not appear to resample the points on the screen adequately while the control is being dragged around the form. As a result, the dragging needs to occur until the cursor reaches the edge of the control and the cursor has just touched the form before it is again dragged. This creates a jerky type of movement where the control is readjusted only when the cursor touches the form.
Please, somebody kindly provide some hints as to how I could get the dragPosition coordinates to be resampled so that the cursor does not need to touch the form before it is again moved on the form.
The code for the DragOver Event handler is as follows:
privatestaticvoid _ctrlParent_DragOver(object sender, DragEventArgs e)
if (DragDropHandler.CanDropHere((Control)sender, e.Data))
Control cthis = (Control)sender;
e.Effect = DragDropEffects.Move;
Control ctrl = DragDropHandler.GetControl(e.Data, true, true);
if (!(ctrl is IDragDropEnabled))
if (cthis.Name != ctrl.Name) // attempt to prevent a circular control reference in which a control may potentially refer to itself as a parent.
ctrl.Parent = cthis;
// this is the place where the dragPoint needs to be resampled more often because the cursor needs to touch the form before dragging occurs.
Point NewLocation = cthis.PointToClient(new Point(e.X, e.Y));
ctrl.Left = NewLocation.X - dragPoint.X;
ctrl.Top = NewLocation.Y - dragPoint.Y;
catch (System.Exception ex)
MessageBox.Show("Here is the DragOver Error: " + ex.Message + " The ctrl.Name is " + ctrl.Name + " and cthis is " + cthis.Name);
e.Effect = DragDropEffects.None;
Last Visit: 31-Dec-99 19:00 Last Update: 29-Nov-23 9:48