Solved

Where to put enumerations?

Posted on 2011-09-09
13
186 Views
Last Modified: 2012-05-12
I have an SMTP class which sets the following property

 Public Property SmtpSAuthenticationType() As AuthenticationTypeException.AuthenticationType
        Get
            Return _smtpSAuthenticationType
        End Get
        Set(ByVal value As AuthenticationTypeException.AuthenticationType)
            Try
                If Not [Enum].IsDefined(GetType(AuthenticationTypeException.AuthenticationType), value) Then
                    Throw New AuthenticationTypeException
                End If
                _smtpSAuthenticationType = value
            Catch ex As Exception
                _errors.Add("SMTP Server Authentication Type", ex.Message)
            End Try
        End Set
    End Property

I have defined the enumeration in the exception, should it be in the class instead?
0
Comment
Question by:rocky050371
  • 7
  • 6
13 Comments
 

Author Comment

by:rocky050371
Comment Utility
As an addition to that if I am using a flags enumeration should they be decalred in the same place too
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
This:

If Not [Enum].IsDefined(GetType(AuthenticationTypeException.AuthenticationType), value) Then

Open in new window


doesn't really benefit you. You have defined the type of SmtpSAuthenticationType to be AuthenticationTypeException.AuthenticationType, so anybody using your code is restricted to assigning values of that type--otherwise a compiler error is generated. The only reason I could think to have code like this is if your property were being assigned via reflection, but I believe even then, an error would occur (a runtime error) due to trying to assign an inappropriate value.

I have defined the enumeration in the exception, should it be in the class instead?
I believe MS suggests enums get their own code file, although they seem to have done a good job of creating enums and other classes inside of other classes, themselves! Put your enum wherever it is the most logical. To me, including the enum inside of the AuthenticationTypeException class seems a bit counter-intuitive.
0
 

Author Comment

by:rocky050371
Comment Utility
there is a try block around it, surely checking whether the value is defined within the enumeration provides feedback to those who are consuming the business object
0
 

Author Comment

by:rocky050371
Comment Utility
or in fact should I set the property to an integer then check it within the assignment?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
You're missing what I'm saying. The type of value inside your set will ALWAYS be AuthenticationTypeException.AuthenticationType. The compiler won't allow you to pass in "cat", 1, new Boat(), etc. You always have to assign a value of type AuthenticationTypeException.AuthenticationType to the SmtpSAuthenticationType member. In other words:

Good
obj.SmtpSAuthenticationType = AuthenticationTypeException.AuthenticationType.FirstEnumMember
obj.SmtpSAuthenticationType = DirectCast(1, AuthenticationTypeException.AuthenticationType)

Open in new window


Bad
obj.SmtpSAuthenticationType = "cat"
obj.SmtpSAuthenticationType = 1
obj.SmtpSAuthenticationType = new Boat()

Open in new window



Note, the difference between #2 Bad and #2 Good is that the good version has an explicit cast. This is assuming you don't have Option Strict set to Off.
0
 

Author Comment

by:rocky050371
Comment Utility
I appreciate what you are saying, but to cater for any value being passed in is it better then for calling object to treat the public property as an integer then cast it to the enum for internal use?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
but to cater for any value being passed in is it better then for calling object to treat the public property as an integer then cast it to the enum for internal use?
If you were to change the type of SmtpSAuthenticationType to int, then you would want the if logic, because int can hold many more values than what you would probably define in your AuthenticationType enum. The benefit of using an enum over an int is that your range of valid values is restricted to only those defined by the enum; if you use an int, then your possible values become -2147483648 to 2147483647, but you enum may only define 1 to 5. This is why you would want a check like what you have--to weed out the other 4294967291 values that don't correspond to a valid enum value. If you keep the enum as the type of your SmtpSAuthenticationType property, then you avoid having to check for these unacceptable values.
0
 

Author Comment

by:rocky050371
Comment Utility
but this then  relies on the calling program to know that the public property only accepts a value within the allowed range.

How would you deal with this?

Presumably if I am right in thinking that removing he enum check is also effectively removing the business rule




0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
It looks like I might be eating crow after this conversation  : \

In testing my assertions, it seems that VB is a tad more "forgiving" than C# is, even though we're still playing in .NET land. Passing in a casted value to an a field/property of type enum will still allow you to store the value without error--C# would not allow this. For example:

Module Module1

    Sub Main()
        Dim x As AuthenticationType = 1700

        Console.WriteLine(x.ToString())
        Console.ReadKey()
    End Sub

End Module

Public Enum AuthenticationType
    FirstEnumMember = 1
End Enum

Open in new window


I've only defined one enum member: FirstEnumMember. Its value is 1. However, in my test code, I am casting the value of 1700 to the enum's type. The assignment will succeed, but when the ToString call hits, you will simply see the value 1700 rather than the default of the enum member's name--if you assigned the value of FirstEnumMember to the variable x, the ToString call would print "FirstEnumMember" and not "1".

Based on this test, you are correct in your logic for the property. If you ever switch to C#, you shouldn't need such logic  = )
0
 

Author Comment

by:rocky050371
Comment Utility
:-)

I would still be interested to know how you would do it, would you always just force consumers to pass a correct range value in, or would you switch it to an int etc


0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
I would use an enumeration because it associates friendly names with values. It much easier to remember:

Public Enum LampState
    Off = 0
    On = 1
End Enum

than it is to remember:

obj.LampState = 1  ' Lamp is on

Open in new window


especially since Intellisense will pop up and list "Off" and "On" as possible values when using an enumeration.

In reality, enumerations are integer values under the hood anyway, so when using an enum, you are still using an int--you just get a friendly name associated with the value.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Correction to my first example (to make it more in line with the second:

Public Enum LampState
    Off = 0
    On = 1
End Enum

Open in new window


obj.LampState = LampState.Off

Open in new window

0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Another benefit to using enumerations is that if the value of any particular enum member ever needs to change, then you can simply change it in the definition of the enum rather than searching for every spot where you used a "1" in code. So my last example could be:

Public Enum LampState
    Off = 2  ' The boss decided that 2 makes more sense as an "off" value
    On = 1
End Enum

Open in new window


Now our other code doesn't need to be changed:

obj.LampState = LampState.Off

Open in new window


because we simply changed the definition.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now