Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi friends..

I am working on a project in c#, and I declared all constant, error msgs etc in a dll.(using classlibrary).
Now I want to access all of them into my project.For that I declared the namespace and then I created an object of the class, but I am not able to access the variables.

What can I do as I don't have any idea? Is there any other way to access these variables.

Thanks
Swapnil

[Edit] Removed TextSpeak and improved grammar[/Edit]
Posted
Updated 14-Mar-12 1:42am
v2
Comments
johannesnestler 14-Mar-12 10:49am    
Are you shure you know what the "const" keyword is for in c#? you won't get instance variables this way. Caution if you come from c++! (like I did..)

Have you referenced the dll in Solution Explorer - Add Reference ? If so, have you declared the variables public in the dll ?
 
Share this answer
 
Comments
swapnilKumbhar 14-Mar-12 7:40am    
Yes i did..
I hv also create Object of that Class.And all variables are in public.
But am not able access them.
Wayne Gaylard 14-Mar-12 7:45am    
Are the variables static or are they non-static ? If they are static you cannot access them using an instance of the class, you need to just preface the variable with the class name i.e YourClass.YourVariable as a pose to classObj.YourVariable.
swapnilKumbhar 14-Mar-12 7:54am    
All are non static
namespace BatchCard
{
class BCC
{
#region BLANK String
Public Const string IssDates="";
#endregion
}
}


Then
In my Project, i add BatchCard dll from reference,
Then Created Obj of class i.e.
BCC b1=new BCC();

Now i want to access tht variable. i.e. b1.IssDates
obj didnt show reference to variable.
Wayne Gaylard 14-Mar-12 8:13am    
I tried a test and it would only let me access that const with a type name and not an instance of the class.
dll:-
public class Class1
{
public const string IsDate = "";
}
usage:
public Form1()
{
Class1 class1 = new Class1();
string isDate = class1.IsDate;//Throws error - need to use type name not instance
string isDate = Class1.IsDate;//No error
}

Hope this helps

johannesnestler 14-Mar-12 10:53am    
const variable static or not static? http://msdn.microsoft.com/en-us/library/e6w8fe1b(v=vs.71).aspx - you won't get "instance" variables with const...
Maybe a soltion would be using static variables (maybe even in a static class),
or use "readonly" keyword. Compare this:

C#
MyConstants mc = new MyConstants();
    string strDefaultInstance = mc.strDEFAULT_INSTANCE;
    string strDefault = MyConstants.strDEFAULT;


public class MyConstants
{
    public const string strDEFAULT = "Default";
    public readonly string strDEFAULT_INSTANCE = "Default";
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Mar-12 0:22am    
Such class should also be done static. For that purpose, also make strDEFAULT_INSTANCE static.
All instance objects here do not make sense.
--SA
johannesnestler 15-Mar-12 4:28am    
Yes you are right (I mentioned that I'd use static variables in a static class), maybe the example is not the best, just wanted to point OP to the possibillity to create kind of "instance constants" - but I agree this doesn't make much sense - Thank you for pointing that out.
Sergey Alexandrovich Kryukov 15-Mar-12 21:44pm    
Thank you.
After your words I decided to add my own answer with a link showing how to implement it correctly -- please see.
--SA
There is no a notion of global variable in .NET (thank goodness! finally!).

You may need to know how the singleton pattern works:
http://en.wikipedia.org/wiki/Singleton_pattern[^].

Good .NET implementation of it is explained here:
http://csharpindepth.com/Articles/General/Singleton.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900