How can I overload a Constructor with arguments that have the same datatype?

I want to create one implementation of a constructor if an employee number is passed as an argument and a different implementation if a user Id is passed as an argument, but both arguments are of type "long".  I could create two one element structures (one for EmployeeNo with a Public variable "EmployeeNo", and one for UserId with a Public variable "UserId") and call the constructor with code similar to the following ...

Dim EmployeeNo As EmployeeNo

EmployeeNo.EmployeeNo = 240

Dim Class1 As New Class1(EmployeeNo)

Does anyone have another idea?  I would like this better if it was possible to make "Employee" the default variable so the programmer would only have to type "EmployeeNo = 240", and if I had a default property expose the "EmployeeNo" variable it would have to have an index.
LVL 1
Declan_BasileITAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Well, without further information, this is normally not a good idea. Cause you create an extra layer of complexety to overcome a necessary restriction.

The normal approach would be to create a common - maybe abstract - base class and create two derived versions. One which takes the employee and the other which takes the user as argument.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Well...you could modify the Singleton pattern and have two accessors, "FromEmpoyeeNumber" and "FromUserID", instead of the standard single "Instance" method.

Then it would be perfectly clear which one was being used:
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim c1 As Class1 = Class1.FromEmployeeNumber(240)
        Dim c2 As Class1 = Class1.FromUserID(123)
    End Sub

End Class

Public Class Class1

    Public EmployeeNumber As Long
    Public UserID As Long

    Private Sub New()
        ' default constructor is PRIVATE so it cannot be created directly!
        ' This is not possible now:
        ' Dim c1 As New Class1() ' <-- impossible from OUTSIDE of Class1
    End Sub

    Public Shared Function FromEmployeeNumber(ByVal empNum As Long) As Class1
        Dim c1 As New Class1 ' <-- this is allowed because we are already inside Class1
        c1.EmployeeNumber = empNum
        ' ... some code here specific to empNum? ...
        Return c1
    End Function

    Public Shared Function FromUserID(ByVal userID As Long) As Class1
        Dim c1 As New Class1 ' <-- this is allowed because we are already inside Class1
        c1.UserID = userID
        ' ... some other code here specific to userID? ...
        Return c1
    End Function

End Class

Open in new window

frankhelkCommented:
Just a simple approach ... just add another parameter (Enum) that directs the constructor how to treat the argument:

Enum EmpRefType
    EmpId
    EmpNo
End Enum

Private Sub New( eType as EmpRefType, eNumberOrId as long)
       if  eType = EmpId then  
           ' treat 2nd parameter as ID
       else
           'treat 2nd parameter as Employee No
       End If
End Sub

Open in new window


That solves the ambivalence and - as a little treat - makes the calling code "speaking". The use of the constructor would read

Dim xyz, abc as theClass
xyz = new theClass(EmpId, 12345)
abc = new theClass(EmpNo, 34567)

Open in new window


Couldn't be more self-documenting, could it ?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

sarabandeCommented:
you might add a string argument (name) or date argument (start of employment) for employees.

but surely the approach ste5an has suggested to have two classes employee and user derived from same  baseclass is the way you should go.

Sara
ste5anSenior DeveloperCommented:
@Mike, without further information, I don't think this works. Cause the only reason do to this in the ctor is to get an immutable EmployeeID or UserID. This can only be set in the constructor.

When an immutable property is not needed and it's all about readable instantiation, then we don't need a singleton. Just using Shared helpers would be sufficient.

This would be acceptable, cause they implement the factory method pattern. Thus it is also a clean solution.

Another solution could be the repository pattern..
Declan_BasileITAuthor Commented:
Great comments everyone.  I ended up using an enumerated list (thank you Frank).  I thought of the idea of using a parameter to tell the constructor the significance of the other parameter, but didn't think of using an enumerated list.  It works great because intellisense enumerates the options for the first parameter as you are writing the call to the procedure.  All the other comments helped me understand other options and understand VB.net/OOP much better too.  I didn't think of the possibility of making a constructor private (thanks Mike) and also, as ste5an further added, that the employeeno and userid wouldn't be immutable (thanks ste5an), which in my case wouldn't have mattered, but I didn't think of that.  Thanks so much everyone!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.