PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 27, 2022

[FIXED] How to define a Class which is accessible in all classes in C#?

 November 27, 2022     c#, module     No comments   

Issue

Am new to C#, but have a plenty of experience of VB.net, now my issue is that there are no modules in C# and i need to define a class which is accessible in all classes and i don't know how to do it. For example I have a "classProject" and I need to make it accessible everywhere, so in vb.net , I will define it in module like below.

Module ModuleMain
Public tProject As New ClassProject
End Module

Now, I need to do same in C#. Thanks in advance.


Solution

You can do this in your case:

namespace MyProject
{
    public static class classProject
    {
      int myIntvar = 0;
      string myStringvar = "test";
    }
}

And you can use this static class in your other classes like:

public class Test
{
  int intTest = classProject.myIntvar;  //will be 0
  string stringTest = classProject.myStringvar; // will be test
}

You can use the variables in the static class since a static variable shares the value of it among all instances of the class. When you create multiple instances of classProject class, the variables myIntvar and myStringvar are shared across all of other classes in your project. Thus, at any given point of time, there will be only one integer and one string value contained in the respective variable's.



Answered By - Rahul Sharma
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing