using enums and strongly typed parameters

  • Thread starter Thread starter Wazir
  • Start date Start date
W

Wazir

Hi all,

I have one class in a C# Class Library project and one class in a Windows
Application project. Both are a part of my solution.
This is a skeleton representation of my classes

namespace classlib
{
public class sample
{
public sample(example value)
{
}

public enum example : int
{
One = 1,
Two = 2,
Three = 3,
Four = 4
}

} // end of class
}

My consumer class in the windows application project creates an instance of
the sample class. The windows app references the class library

public class consumer
{
public void testSample() {
classlib.sample s = new
classlib.sample(classlib.sample.example.One);
}
}

When passing the parameter I have to give the whole path to the enum. I am
not able to call the constructor like this: classlib.sample s = new
classlib.sample(example.One);
I suppose the example enum would have the be in some sort of a global class
in the class library. Can anyone help me out here.

Many thanks
Wazir
 
Many thanks, I didnt know that.

What about constants. I come from a vb background, and all global constants
were defined in a module class. How would we do that in C#.

Regards,
Wazir
 
Have a separate class called 'consts' - all consts are static by default.
So in your class library, you could have
namespace myclasslib
{
public class MyConsts
{
public const int Const1 = 42;
//... (other consts)
}
}

then in the main application...
using myclasslib;
namespace theapp
{
class Class1
{
[STAThread]
static void Main()
{
int i = MyConsts.Const1; //42
//... other code
}
}
}



I *think* that's it...
 
Back
Top