Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a DLL Assembly with constants

I have a solution with about 3 projects in the solution. These 3 projects share some common constants that, until now, I have been copying and pasting between the 3 projects.

What I want to do instead is create an assembly that contains these constants and just reference the assembly from the other project. The problem is, I think I remember reading somewhere once that when the project is compiled, the constant values are simply "copied and pasted" from the assembly into the assembly referencing it. Which would mean that if I wanted to change the value of one of these constants, I couldn't just change the one assembly. I'd have to recompile the entire solution again.

Can anyone please confirm if this is true and if it is, please tell me the preferred and alternative way to do this so that I can just change the constant in my assembly?

like image 740
Icemanind Avatar asked May 05 '14 23:05

Icemanind


People also ask

How do you declare a constant variable in C#?

Use the const keyword in C# The const (read: constant) keyword in C# is used to define a constant variable, i.e., a variable whose value will not change during the lifetime of the program. Hence it is imperative that you assign a value to a constant variable at the time of its declaration.

How DLL files are created?

In Visual C++ 6.0, you can create a DLL by selecting either the Win32 Dynamic-Link Library project type or the MFC AppWizard (dll) project type. The following code is an example of a DLL that was created in Visual C++ by using the Win32 Dynamic-Link Library project type.

What is difference between assembly and DLL?

An assembly is a collection of one or more files and one of them DLL or EXE. DLL contains library code to be used by any program running on Windows. A DLL may contain either structured or object oriented libraries. A DLL file can have a nearly infinite possible entry points.

What is a DLL in C#?

A Dynamic Link library (DLL) is a library that contains functions and codes that can be used by more than one program at a time. Once we have created a DLL file, we can use it in many applications.


1 Answers

Instead of declaring const values, declare the actual values in a static class.

public static readonly int someConst = 6;

That will force your program to use the updated constant when you change it in the linked DLL.

For what it's worth, some sort of configuration file is probably a better solution, if you want to do this at runtime, rather than compile time.

Further Reading
Static readonly vs const — different assemblies POV?

like image 166
Robert Harvey Avatar answered Oct 15 '22 19:10

Robert Harvey