0

IDE: C# .net, winforms VS 2010

I have a class SystemConstants.cs

As shown below:

public class SystemConstants
{

    //String Constants
    public const string Hello = "Hello";
    public const string Youth = "Youth";
}

And have Form1.cs

Now in form1.cs

txtbox1.text = SystemConstants.Hello;
txtbox2.text = SystemConstants.Youth;

Now I want to know is there any why by which instead of writing

SystemConstants.Hello; //I want to avoid writing class name or I want to use alias for class name


 Like this SystemConstants alias sc;
  txtbox1.text = sc.Hello;
user3410150
  • 61
  • 1
  • 2
  • 8
  • Since `SystemConstants` is not a `static` class, why not have a `private static SystemConstants sc = new SystemConstants;` in your `Form1` and then use that? By the way, be very *very* careful with public constants. If they're public you should probably prefer `public static readonly`. See http://stackoverflow.com/questions/755685/c-static-readonly-vs-const – Corak Apr 09 '14 at 06:38
  • If `SystemConstants` is not in the namespace of `Form1` then you need to write namespace name before class name `using sc = YourNamespace.SystemConstants;`. – Software Engineer Apr 09 '14 at 06:46

2 Answers2

2

Sure; you can use a using alias directive. Add this to the top of your Form1.cs file:

using Sc = MyNamespace.SystemConstants;
Ani
  • 111,048
  • 26
  • 262
  • 307
0

You can do this by writing class alias at the top:

using sc = SystemConstants;

If SystemConstants is not in the namespace of Form1 then you need to write namespace name before class name:

using sc = YourNamespace.SystemConstants;
Software Engineer
  • 3,906
  • 1
  • 26
  • 35