Hi all
,
I'm just starting to learn C# after learning a lot of Visual Basic. However, I have become a bit stuck! It's a really simple question but I really can't work it out.
I added two settings to a project, X and Y, both integers. I set the scope to Application. How do I then access these settings I need to be able to read from and write to these variables, which is the problem I have at the moment. A site told me to use
RocksnGems.Properties.Settings.Default.X //and
RocksnGems.Properties.Settings.Default.Y
to access the settings (RocksnGems is the default namespace for the project). I don't know if reading the variables in this way works, but when i tried to write to these values using
private void move(object sender, EventArgs e)
{
RocksnGems.Properties.Settings.Default.X = this.Location.X;
RocksnGems.Properties.Settings.Default.Y = this.Location.Y;
}
it gave an error. How do I access these variables so I can write to them. I did think that maybe I could remove the "default" part of it, but when I do that, IntelliSense doesn't show X and Y in the list.. so that can't be right!!
Any help would be appreciated ![]()
![]()
- Javawag

C# and application settings
Dan Thomas - DAGWare
Here is a link
http://msdn2.microsoft.com/en-us/library(d=robot)/ms173136.aspx
KatyTX
OK thanks... I'm going to go with the Visual Basic "My" class as I'm familiar with that and it seems easier! How would I go about using that in C# though... I'm assuming you create a reference to Microsoft.VisualBasic... but i can't find where to go from there!
Thanks for the quick reply
-Javawag
barteknj
Ooook I may just use registry settings
.. which leads to another completely unrelated question- how do I declare a variable which is visible to any form in my project I used the following code to make an object called myRegistry that can be used to save settings in the correct place:
Microsoft.Win32.RegistryKey myRegistry = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("software").CreateSubKey("RockGem");
What would I have to do so that i declare this once and then am able to use it in any form i add to my project Is this even possible
Thanks,
-Javawag
mmikedm1000
I had that same problem recently.
See
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=256265&SiteID=1
You need to either create a custom class derived from ApplicationSettingsBase, or you need to use the Visual Basic "My" classes, which are accessible from C# if you include it.
MaqboolHussain
Right i've done that... I can now access Audio and Clock etc. as that tutorial says. Do you you know how I can now access the settings part from here
Thanks,
- Javawag
Tanmoy007
I have seen posts that say you can use the VB My classes in C#. Evidently, you must initialize the My library and there is some code for that.
For my own part, I am doing it the C# native way, so I cannot give you the code.
However, before you go to the trouble, see the last reply here first
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=281035&SiteID=1
You can use the properties editor to specify a user-scoped application setting. Then, you don't need any code at all.
Then search your project and you will see the auto-generated code that gives you that.
Then you might "borrow" that code for other settings that are not bound to controls.
lindaonlytry
All projects consist of namespaces. A namespace can extend across multiple files and include any number of classes.
For a Windows Forms application, you will have Program.cs file and that will have the Main() method in it. (Search for "main").
It would look like this
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
"Application" in the above is actually a class reference not an object reference. There is an application object there somewhere but it is hidden from view. The Run method and the other two methods are static members. It is a strange way to access the object, but they make you do that because they do not want you to reference the object itself. The hidden application object will keep a list of all running forms, and you can reference them and iterate through them using the OpenForms method and other methods. But, you won't need to.
Who calls Main(), I don't know. That is under the hood stuff, handled by the operating system when the program loads. It is enough to know that a Form1 instance "lives" at that point. Note that there is no global variable that points to the Form1 instance.
The other forms and modules in your application are created in one of two ways:
Either in the Main method above, just prior to the Run() method, other objects can be created and become owned by the application object, in the same way that the Form1 instance is owned by the application object. You may notice the compiler create additional forms or modules there. If they are there, know that they are owned by the application object.
Alternatively, and simpler, you can create the objects yourself within the form create event. Then they become owned by the Form1 instance. Because they are owned by Form1, they have visibility to it (Form1 is the owner). Form1 then exposes public variables and methods. Any objects owned by Form1 can reference those public variables or methods, through the owner property.
Note, when you reference the Owner, you must cast it as an object of type Form1. Then you can access the methods contained within it.
Did that make sense
A lot of complicated talk. The bottom line is, know who owns your objects. If you know that then you can know how to access the variables.