Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Global structure in Main, Functions and .dlls

Global structure in Main, Functions and .dlls

Scheduled Pinned Locked Moved C#
helpcsharpdatabasevisual-studiocom
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zequion
    wrote on last edited by
    #1

    I have a class called Cls_Common.cs which contains a structure called St_Common where I declare the global variables that are common to all .cs files. I use it in Functions, compiled classes .dlls and in Mains. I create a compiled .dll class to encrypt using these functions and the global structure of Cls_Common.cs. When i add by reference the compiled class .dll to a Main, in the compile it generates in Visual Studio, there are actually two versions of the file Cls_Common.cs. When doing =new of the global structure, an ambiguity error occurs but execution continues and when an attempt to assign or retrieve a global structure value, occurs an error exception. Issue: In order to compile the Cls_Encrypt.dll class I need to include Cls_Common.cs to use common functions. At Main that includes by reference Cls_Encrypt.dll I also need to include Cls_Common.cs and that produces ambiguity. According to Microsoft's brief solution to error CS0433, you have to compile from the command line with /reference and use an alias: : https://msdn.microsoft.com/es-es/library/64wh5743.aspx // compile with: /reference:cs0433_1.dll /reference:TypeBindConflicts=cs0433_2.dll

    using TypeBindConflicts;
    public class Test
    {
    public static void Main()
    {
    AggPubImpAggPubImp n6 = new AggPubImpAggPubImp();
    }
    }

    My Program: ========== Cls_Common.cs: ----------------

    namespace Name_Common
    {
    public struct St_Common
    {
    public string Languaje;
    }

    public static partial class Cls\_Common
    {   // I define the structure so that there is no error in compiling.
        public static Name\_Common.St\_Common StCommon;
    }
    

    }

    Class compiled in an assembly called Cls_Encrypt.dll This class uses in Mains adding it as reference. Used to encrypt. Inside the class, calls are made to functions such as Func1.cs, Func2.cs ... That use the global variable structure Name_Common.Cls_Common.StCommon Cls_Encrypt.cs:

    ---------------
    public class Cls_Encrypt
    {
    public Cls_Encrypt() // Constructor.
    { // Example line:
    if (Name_Common.Cls_Common.StCommon.Languaje == "Eng") ...
    }
    }

    Main where I added as reference to compiled class Cls_Encrypt.dll Main.cs -----------

    namespace Name_Main
    {
    public Cls_Main()
    {
    // At this line, no error event occurs. But if in the debugger I query by Name_Common.Cls_Common.StCommon, it appears:
    // Name_Common.Cls_Common.StCommo

    L 2 Replies Last reply
    0
    • Z zequion

      I have a class called Cls_Common.cs which contains a structure called St_Common where I declare the global variables that are common to all .cs files. I use it in Functions, compiled classes .dlls and in Mains. I create a compiled .dll class to encrypt using these functions and the global structure of Cls_Common.cs. When i add by reference the compiled class .dll to a Main, in the compile it generates in Visual Studio, there are actually two versions of the file Cls_Common.cs. When doing =new of the global structure, an ambiguity error occurs but execution continues and when an attempt to assign or retrieve a global structure value, occurs an error exception. Issue: In order to compile the Cls_Encrypt.dll class I need to include Cls_Common.cs to use common functions. At Main that includes by reference Cls_Encrypt.dll I also need to include Cls_Common.cs and that produces ambiguity. According to Microsoft's brief solution to error CS0433, you have to compile from the command line with /reference and use an alias: : https://msdn.microsoft.com/es-es/library/64wh5743.aspx // compile with: /reference:cs0433_1.dll /reference:TypeBindConflicts=cs0433_2.dll

      using TypeBindConflicts;
      public class Test
      {
      public static void Main()
      {
      AggPubImpAggPubImp n6 = new AggPubImpAggPubImp();
      }
      }

      My Program: ========== Cls_Common.cs: ----------------

      namespace Name_Common
      {
      public struct St_Common
      {
      public string Languaje;
      }

      public static partial class Cls\_Common
      {   // I define the structure so that there is no error in compiling.
          public static Name\_Common.St\_Common StCommon;
      }
      

      }

      Class compiled in an assembly called Cls_Encrypt.dll This class uses in Mains adding it as reference. Used to encrypt. Inside the class, calls are made to functions such as Func1.cs, Func2.cs ... That use the global variable structure Name_Common.Cls_Common.StCommon Cls_Encrypt.cs:

      ---------------
      public class Cls_Encrypt
      {
      public Cls_Encrypt() // Constructor.
      { // Example line:
      if (Name_Common.Cls_Common.StCommon.Languaje == "Eng") ...
      }
      }

      Main where I added as reference to compiled class Cls_Encrypt.dll Main.cs -----------

      namespace Name_Main
      {
      public Cls_Main()
      {
      // At this line, no error event occurs. But if in the debugger I query by Name_Common.Cls_Common.StCommon, it appears:
      // Name_Common.Cls_Common.StCommo

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Including the same file in different parts of the application will always cause this. Your main program should only use the common methods from the DLL, not include the entire file.

      Z 1 Reply Last reply
      0
      • L Lost User

        Including the same file in different parts of the application will always cause this. Your main program should only use the common methods from the DLL, not include the entire file.

        Z Offline
        Z Offline
        zequion
        wrote on last edited by
        #3

        55/5000
        if I use two dlls? Again I have the same problem. Think of a programmer or business who sells dlls (my case) and sells more than one to the same client.

        in this case, from Main I can reference the global space of the first dll (not a good solution), but from the second dll I can not. I do not know how to refer from dlls to a common global space from Main, because I can not do without the global variables since many functions use them constantly. Seriously c# does not have a global space and Microsoft closes the topic with a brief patch That does not solve this problem? Can not be.

        L 1 Reply Last reply
        0
        • Z zequion

          55/5000
          if I use two dlls? Again I have the same problem. Think of a programmer or business who sells dlls (my case) and sells more than one to the same client.

          in this case, from Main I can reference the global space of the first dll (not a good solution), but from the second dll I can not. I do not know how to refer from dlls to a common global space from Main, because I can not do without the global variables since many functions use them constantly. Seriously c# does not have a global space and Microsoft closes the topic with a brief patch That does not solve this problem? Can not be.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          The only solution to this issue is to remove the duplicated code from your application. You may even need to rethink your design.

          Z 1 Reply Last reply
          0
          • L Lost User

            The only solution to this issue is to remove the duplicated code from your application. You may even need to rethink your design.

            Z Offline
            Z Offline
            zequion
            wrote on last edited by
            #5

            I'm wondering if I can compile the two dlls with the reference to Cls_Common.cs and from main do something like (if possible): Name_Common.Cls_Common.StCommon = new global::Name_Common.St_Common(); And when I have to call the dlls do something like: Name_Encrypt.Cls_Encrypt.StCommon = (Name_Common.St_Common)Name_Common.Cls_Common.StCommon Help, thank you.

            L 1 Reply Last reply
            0
            • Z zequion

              I'm wondering if I can compile the two dlls with the reference to Cls_Common.cs and from main do something like (if possible): Name_Common.Cls_Common.StCommon = new global::Name_Common.St_Common(); And when I have to call the dlls do something like: Name_Encrypt.Cls_Encrypt.StCommon = (Name_Common.St_Common)Name_Common.Cls_Common.StCommon Help, thank you.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              That does not sound right. It seems to me that you are trying to make things much more complicated than they need to be with this. Whatever purpose this class serves, you can only have it in one place, either in the DLL or in the EXE part, not in both.

              Z 1 Reply Last reply
              0
              • Z zequion

                I have a class called Cls_Common.cs which contains a structure called St_Common where I declare the global variables that are common to all .cs files. I use it in Functions, compiled classes .dlls and in Mains. I create a compiled .dll class to encrypt using these functions and the global structure of Cls_Common.cs. When i add by reference the compiled class .dll to a Main, in the compile it generates in Visual Studio, there are actually two versions of the file Cls_Common.cs. When doing =new of the global structure, an ambiguity error occurs but execution continues and when an attempt to assign or retrieve a global structure value, occurs an error exception. Issue: In order to compile the Cls_Encrypt.dll class I need to include Cls_Common.cs to use common functions. At Main that includes by reference Cls_Encrypt.dll I also need to include Cls_Common.cs and that produces ambiguity. According to Microsoft's brief solution to error CS0433, you have to compile from the command line with /reference and use an alias: : https://msdn.microsoft.com/es-es/library/64wh5743.aspx // compile with: /reference:cs0433_1.dll /reference:TypeBindConflicts=cs0433_2.dll

                using TypeBindConflicts;
                public class Test
                {
                public static void Main()
                {
                AggPubImpAggPubImp n6 = new AggPubImpAggPubImp();
                }
                }

                My Program: ========== Cls_Common.cs: ----------------

                namespace Name_Common
                {
                public struct St_Common
                {
                public string Languaje;
                }

                public static partial class Cls\_Common
                {   // I define the structure so that there is no error in compiling.
                    public static Name\_Common.St\_Common StCommon;
                }
                

                }

                Class compiled in an assembly called Cls_Encrypt.dll This class uses in Mains adding it as reference. Used to encrypt. Inside the class, calls are made to functions such as Func1.cs, Func2.cs ... That use the global variable structure Name_Common.Cls_Common.StCommon Cls_Encrypt.cs:

                ---------------
                public class Cls_Encrypt
                {
                public Cls_Encrypt() // Constructor.
                { // Example line:
                if (Name_Common.Cls_Common.StCommon.Languaje == "Eng") ...
                }
                }

                Main where I added as reference to compiled class Cls_Encrypt.dll Main.cs -----------

                namespace Name_Main
                {
                public Cls_Main()
                {
                // At this line, no error event occurs. But if in the debugger I query by Name_Common.Cls_Common.StCommon, it appears:
                // Name_Common.Cls_Common.StCommo

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                I don't see why you can't just create a "Common.dll" (in the "MyApp.Common" namespace, for example), that contains singletons or statics for your "shared" data and methods. As long as this dll does not reference other dll's "higher up" (to avoid circular references), then there should be no problem "sharing".

                1 Reply Last reply
                0
                • L Lost User

                  That does not sound right. It seems to me that you are trying to make things much more complicated than they need to be with this. Whatever purpose this class serves, you can only have it in one place, either in the DLL or in the EXE part, not in both.

                  Z Offline
                  Z Offline
                  zequion
                  wrote on last edited by
                  #8

                  I have many functions that use global variables. I need to access a common space from the dlls and Main. Is it too much to ask? Does not C# do? We are not going well.

                  L 1 Reply Last reply
                  0
                  • Z zequion

                    I have many functions that use global variables. I need to access a common space from the dlls and Main. Is it too much to ask? Does not C# do? We are not going well.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Regequion wrote:

                    Is it too much to ask?

                    No, not at all. But you do need to get the design right, and work within the rules of what is possible and allowed. And, most of all, what is logical.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups