basic class question

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

I have a class with methods that return an enum value to the main page.
I have the enum declared both in the class and the main page so that I
can read the value that is returned from each class method. Here is the
enum and code calling the class method on the main page. Is this the
right way to go about this (converting the returned value etc) or is
there a more clever way to do it?

public enum VResult
{
Valid = 0,
Invalid = 1
}

....

creditcard ccPackage = new creditcard();

VResult vrValidCardType =
(VResult)ccPackage.Validate_Card_Type(strCardTypePackage);

if (vrValidCardType == VResult.Invalid)
{
//do stuff
}

Thanks,

Mike
 
Declare the enum in the same assembly as the class (but don't make it part of the class) and mark it as public, then just have the method signature as

public VResult ccPackage.Validate_Card_Type(string cardType);

Now you code doesn't need the cast:

VResult vrValidCardType - ccPackage.Validate_Card_Type(strCardTypePackage);
if( vrValidCardType == VResult.Invalid )
...

You don't need to declare the enum in two locations

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

I have a class with methods that return an enum value to the main page.
I have the enum declared both in the class and the main page so that I
can read the value that is returned from each class method. Here is the
enum and code calling the class method on the main page. Is this the
right way to go about this (converting the returned value etc) or is
there a more clever way to do it?
 
Richard,

This would mean that if I make a DLL and reference it from my main page,
I would gain access to the enum that way? Why are you prefixing the
method in the class with the instance name though?


Regards,

Mike
 
Hi,


Mike P said:
I have a class with methods that return an enum value to the main page.
I have the enum declared both in the class and the main page

Declare it only once, if you could declare it twice is cause either they
are in different namespaces or one is part of a class, in any way they are
DIFFERENTS


That's why you have to cast it, otherwise you would get an error.

Cheers,
 
The instance name on the method declaration was a typo :-)

Yes you could gain access to the enum that way - although I assumed (obviously incorrectly) that the class was already in a different assembly. If it isn't, simply declare the enum outside of the scope of the class and change the code as I stated before (without the erroneous instance name on the method of course ;-) )

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog


Richard,

This would mean that if I make a DLL and reference it from my main page,
I would gain access to the enum that way? Why are you prefixing the
method in the class with the instance name though?


Regards,

Mike
 
Back
Top