Global structure in Main, Functions and .dlls
-
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 -
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 -
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.
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.
-
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.
-
The only solution to this issue is to remove the duplicated code from your application. You may even need to rethink your design.
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.
-
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.
-
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.StCommoI 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".
-
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.
-
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.