HI,
Today is my first day at this wonderful forum.I wanted to ask How to :
Make a class globally accessible to all the classes ?I come from the C back ground so I
am thinking like this -- I want to use the class (say classGlobal) from the other files(Classes) I am doing something like this :

class1.cs

namespace Namspce
{
	    public class class1
		{
		/* Declaring the classGlobal as static */
	         public static classGlobal cg = new classGlobal();
			public void method()
			{
			    /* some updation to members of this class members(variables inside methods)*/
				cg.method1();
				cg.method2();
				cg.method3();
				
				/* Creating another class instance to accss this classGlobal members */
				classOther co = new classOther();
	
	
				/* Now here control goes to classOther.cs where I am supposed to use the class cg(its updated members - variables)*/
				/* Remember that due to too much parameter passing i dont want to pass the whole class variable cg to "co.somemethod()" */
				  co.somemethod();
			}
	
	}

}



classOther.cs
namespace Namspce
{
     class classOther
         {
             public void somemethod()
             {
              /* accessing the class which I have declared static */
                 class1.cg.method4();
             }
    }
}


classGlobal.cs
namespace Namspce
{
public  class classGlobal
{

	private string var1;
	private string var2;
	private string var3;
	private string var4;
   	 /* Class definition  */

	   public void method1()
	   {
	     /* updating var1 here */

	   }

	   public void method2()
	  {
	  /* updating var1 and var2 here */

	  }
	  public void method3()
	   {
	   /* updating var1,var2 and var3 here */

	   }

	  public void method4()
	   {
	     /* updating var1,var2,var3 and var4 here */
	   }


}
}

So am I doing it correct,For the OOPS concept is this approach correct?
Kindly spark the debate.?

Best Regards

I think you need to differentiate between a class and an object. A class is a blueprint, a type definition:

namespace JSW {
  public class Foo {
    public Foo() {
      Console.WriteLine ( "Foo!" );
    }
  }
}

Once you've defined the type Foo, you can create objects of type Foo:

Foo bar = new Foo();

In these two examples, Foo is a class and bar is an object.

I may be wrong, but I think you are asking on how to make a static class that all other classes can use.

You can change your classGlobal.cs to be static by changing the class definition to public static class classGlobal.
You will need to set your public properties and methods in this class to also be static.

When one of your other classes want to use something from classGlobal, just state it as classGlobal.Methodxyz();

Static classes do not have constructors, and you cannot or should not try to instanciate it. It is automatically instantiated when the application starts, and is immediately available to all of the application.

Hope this helps,
Jerry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.