INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How-to

How To: Restrict values passed to a function, procedure or a property. by PankajBanga
Posted: 6 Jul 04

Sometime we have to restrict the values passed into a function, procedure or a property. A common practice is to validate values inside the code. For instance the following procedure accepts marital status of a customer, checks for valid marital status and displays it in a message box.

Public Sub GetMaritalStatus(ByVal mStatus As String)
        If mStatus = "Defacto" Then
            MessageBox.Show("Defacto")
        ElseIf mStatus = "Divorced" Then
            MessageBox.Show("Divorced")
        ElseIf mStatus = "Married" Then
            MessageBox.Show("Married")
        ElseIf mStatus = "Separated" Then
            MessageBox.Show("Separated")
        ElseIf mStatus = "Single" Then
            MessageBox.Show("Single")
        Else
            MessageBox.Show("Invalid marital status.")
        End If
End Sub

This is a simple example where I had to check only five conditions. Imagine having multiple data values, which becomes a sluggish, time consuming process involving heap of IF blocks. There is an alternative, which makes the process more efficient and regarded as a good programming practice, it is using Enumeration:

Enum MaritalStatus
        Defacto
        Divorced
        Married
        Separated
        [Single]
End Enum

Now all you have to do is declare your argument as type MaritalStatus:

Public Sub GetMaritalStatus(ByVal mStatus As MaritalStatus)
        .......

End Sub

Enumeration is basically a type which can hold a set of values. In .NET Framework Enumeration can be of any type except Char. If no underlying type is explicitly declared, Int32 is used.

For more information on Enumeration visit the following link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemenumclasstopic.asp


Happy Programming!!!

Back to Visual Basic (Microsoft) VB.NET FAQ Index
Back to Visual Basic (Microsoft) VB.NET Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close