How and where do I declare variables that will be used by all my Forms?
Thanks in advance
Printable View
How and where do I declare variables that will be used by all my Forms?
Thanks in advance
In a Module.
Sounds logic, indeed... Like on VB... How do I get to make a module?
Oh... and also... Lets say I have a comboBox... How would be a procedure that would populate it?
Like:
comboBox1.Items = GetItems("DoubleType");
?
In this case I would have a Dictionary and I would get all of those that are Double Typed in my dictionary... Any ideas? (This has to be handed in in less than an hour) :(
No in C#...;)Quote:
In a Module.
Wrap your variable in a class and make them static.
Just enumeate through the collection and query the GetType() method and use the typeof() method to do the comparison.
Lethal, I am really tired.. Could you please give me an example? I would really appreciate it... You will save my life (and my grade too) if you do...
Thanks in advance
Example 1:
Example 2:Code:namespace VSSolutions.Sprocs
{
public class Sprocs
{
private static string addUser;
public static string AddUser
{
get { return addUser; }
set { addUser = value; }
}
public static void AddUserToSytem(int userID)
{
// Call Stored Procedure
}
}
}
Code:using System;
using System.Collections;
public class EnumerateTypes
{
public static void Main()
{
ArrayList types = new ArrayList();
string name = "John Doe";
double age = 23;
types.Add(name);
types.Add(age);
foreach (Object o in types) {
if (o.GetType() == typeof(System.String)) {
Console.WriteLine("String");
}
else if (o.GetType() == typeof(System.Double)){
Console.WriteLine("Double");
}
}
}
}
Damn, thought this was the VB.NET forum. :D Do what Lethal said, put them in a Utility class and declare them as static.Quote:
Originally posted by Lethal
No in C#...;)
Wrap your variable in a class and make them static.
(I was given more time since nobody finished) Thanks Lethal... I still don't grasp all of it though why is the main there? Should I remove the main from the Login Form I have it on?
Do I need to use get and set? I just wanted to be able to Declare something like...
myDictClass myDictionary;
myDictClass2 myOtherDictionary;
int i;
bool FinishProject;
and maybe some initialization as...
FinishProject = false;
Would I need to do the set and get for this too? I am sorry but I really don't get why there isn't a given place on C# to make public variable declarations for all forms in a project.
Thanks again!
:( And where should I declare a new instance of the class?
You can use them in your code by simply doing this:Code:namespace MyApplicationNamespace
{
public class GlobalVariables
{
private GlobalVariables
{}
public myDictClass myDictionary;
public myDictClass2 myOtherDictionary;
public int i;
public bool FinishProject;
}
}
Also, you can check this sample I put together, you can take ideas from it:Code:bool myValue = GlobalVariables.FinishProject;
http://localhost/VariantXWebSite/Dow...lVariables.zip
Thanks, I will try that right now... Please keep in touch if I can't make it work.. :p
Isn't it necessary that I declare an instance of the object? will the changes made to this variables will remain persistent while the project is running?
Oh, and I think the link won't work since it says "localhost"... (I already tried it also)
Thanks!
That worked it out, thanks a lot! :)