Hello,
I'm beginner in c#, but i have 7 years experience in VB6.
I just stuck with creating public static class.
my steps is:
1.creating new winform project
2.adding from solution explorer new class
3.changing type of class from "public Class1" to "public static Class1"
4.got an error "The modifier 'static' is not valid for this item"
what can i do
thanks

public static class
Ankit Mehta
LealTing
Hi,
creating static class is possible in .NET 2.0
using
System;using
System.Collections.Generic;using
System.Text;namespace
staticinitializers{
public static class SC{
public static int x = 20; public static int y = 30;}
class Program{
static void Main(string[] args){
Console.WriteLine (SC.x.ToString() +" " + SC.y.ToString()); Console.ReadLine();}
}
}
The above will print 20 30.
Regards
Katie446
anyway, i need some object similar to module in vb6.
i need to store variables and constants visible for all objects in my project.
what the best way to do that
RMon
First, define private parameterless constructor, so the class cannot be instanciated.
Then, create public static methods and properties of Your choice. Since instance (non-static) methods cannot be called, Your class acts almost like static class.
I'm not sure, but I think that You cannot add static constructor in C# 1.1. If You need some initializing logic before calling the class methods, You must have for example "public static Initialize" -method that You call before anything else gets called.
You could add a flag to the class that indicates if the class has been initialized, and if its not, always raise an exception when you call methods. That way You always are forced to initialize the class. That's just an option, and You could do without it, but it sure helps You to track problems if they occure.
Here's example "static" class in C# 1.1:
public class MyClass
{
private static string message;
private static bool initialized;
private MyClass()
{
}
public static void Initialize()
{
message = "Hello:)";
initialized = true;
}
public static int Add(int x, int y)
{
if( ! initialized )
throw new Exception("MyClass is not initialized.");
return x+y;
}
public static string Message
{
get
{
if( ! initialized )
throw new Exception("MyClass is not initialized.");
return message;
}
}
}
Example of usage:
public static void Main()
{
MyClass.Initialize();
Console.WriteLine( MyClass.Message );
int result = MyClass.Add(12,143);
}
Hope this helps. But if it's possible, I recommend You to turn to C# 2.0.