PDA

Click to See Complete Forum and Search --> : Public Variables? (Solved)


Tec-Nico
May 21st, 2003, 12:42 AM
How and where do I declare variables that will be used by all my Forms?

Thanks in advance

DevGrp
May 21st, 2003, 06:33 AM
In a Module.

Tec-Nico
May 21st, 2003, 07:21 AM
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) :(

Lethal
May 21st, 2003, 07:26 AM
In a Module.

No in C#...;)

Wrap your variable in a class and make them static.

Lethal
May 21st, 2003, 07:28 AM
Just enumeate through the collection and query the GetType() method and use the typeof() method to do the comparison.

Tec-Nico
May 21st, 2003, 08:19 AM
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

Lethal
May 21st, 2003, 09:35 AM
Example 1:

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
}
}
}


Example 2:

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");
}
}
}
}

DevGrp
May 21st, 2003, 03:42 PM
Originally posted by Lethal
No in C#...;)

Wrap your variable in a class and make them static.

Damn, thought this was the VB.NET forum. :D Do what Lethal said, put them in a Utility class and declare them as static.

Tec-Nico
May 21st, 2003, 11:23 PM
(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!

Tec-Nico
May 21st, 2003, 11:32 PM
:( And where should I declare a new instance of the class?

hellswraith
May 22nd, 2003, 12:01 AM
namespace MyApplicationNamespace
{
public class GlobalVariables
{

private GlobalVariables
{}

public myDictClass myDictionary;
public myDictClass2 myOtherDictionary;
public int i;
public bool FinishProject;
}
}


You can use them in your code by simply doing this:

bool myValue = GlobalVariables.FinishProject;


Also, you can check this sample I put together, you can take ideas from it:
http://localhost/VariantXWebSite/Downloads/GlobalVariables.zip

Tec-Nico
May 22nd, 2003, 12:23 AM
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!

Tec-Nico
May 22nd, 2003, 01:00 AM
That worked it out, thanks a lot! :)