Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a static class in same namespace but another assembly?

Tags:

namespaces

c#

wpf

I am working with a WPF application in C#. I have a number of constants defined in a static class as the following:

Project1:

namespace MyCompany
{
   public static class Constants
   {
      public static int MY_CONSTANT = 123456;
   }
}

Then all I need to do to access my constant anywhere inside Project 1 is:

int x = Constants.MY_CONSTANT;

Now I add another project to the same solution, and use the same root namespace:

Project 2

namespace MyCompany.MyControl
{
   class VideoControl
   {
      int x;
      x = Constants.MY_CONSTANT; //<-- doesn't work
      x = MyCompany.Constants.MY_CONSTANT; //<-- doesn't work either
   }
}

I just can't figure out a way to access my static Constants class from the second assembly. I also can't add a reference to the first assembly, because it leads to circular dependency (the second project assembly is a WPF control used by the first project assembly).

Is what I'm trying to do even possible? Currently my workaround is passing all the required constants in constructor, but I'd rather just access them directly.

like image 350
Eternal21 Avatar asked Nov 03 '22 21:11

Eternal21


1 Answers

You can move all of the static constants from project 1 to project 2, therefore, all of the constants are visible to both project 1 and project 2; I recommended that introduce another project (maybe common) which could help to manage all of the stuffs that shared by all of the other projects. it's a common infrastructure project.

like image 179
Nathan Avatar answered Nov 14 '22 06:11

Nathan