Problems with constructors

hog

Senior Contributor
Joined
Mar 17, 2003
Messages
984
Location
UK
I am coming up against this problem on a regular basis which makes me think I'm not understanding the constructor theory too well.

I'm sur eyou will see the problem as soon as you read .....

Visual Basic:
Public New(ByVal lngEquipmentID as Long)

Public New(ByVal blnLiveRecords as Boolean)

Public New(ByVal strSupplierName as String)

Yup, it's the ole narrowing conversion thingy. Is there a way around this as I'm having to invent stupid ways to get around it like this, which is rediculous!!

Visual Basic:
Public New(ByVal lngEquipmentID as Long)

Public New(ByVal blnLiveRecord as Boolean, ByVal strAnyCrap as String)

Thnx
 
Code:
Public Sub New()     ' default constructor
        m_CustID = 1
        m_Name = "Michael"
End Sub

Public Sub New(ByVal CustID As Integer, ByVal Name As String)
        m_CustID = CustID
        m_Name = Name
        m_NoCustomers = m_NoCustomers + 1    'Read only property
End Sub


Does this help?

ailzaj
 
I didn't see a question in your original post - what seems to be the problem/issue/question?

Maybe you forgot the word Sub in your constructor, as in:
Visual Basic:
Public [b]Sub[/b] New(ByVal lngEquipmentID As Long)
'instead of
Public New(ByVal lngEquipmentID As Long)

-Nerseus
 
Ooops!

Visual Basic:
Public Sub New(ByVal lngEquipmentID As Long)
Public SubNew(ByVal blnLiveRecords As Boolean)
Public Sub New(ByVal strSupplierName As String)

The problem I get is when I compile I get a message saying 'without a narrowing conversion'. Basically I think it is complaining that Long, Boolean and string can all pass the same value and it doesn't know which constructor to call?
 
Understand what you are saying.. one way probably round it is to rethink what your class is doing, and whether constructors in that format are necessary.

You would have to explain the purpose of each constructor for me to make a judgement on that... but if indeed all your constructors do have a valid purpose and it doesn't make sense to design them in a different way; I don't know off the top of my head away of combatting the problem you describe but will look into it.
 
OK,

Public Sub New(ByVal lngEquipmentID As Long)

This allows me to create an Equipment object based on the EquipmentID selected by the user from a combobox

Public Sub New(ByVal blnLiveRecords As Boolean)

This creates an Equipment object that contains either all live equipment or all archived equipment based on the boolean value. The method objEquipment.RecordSource is used to as the source data in reports.

Public Sub New(ByVal strSupplierName As String)

This creates an Equipment object based on the supplier name selected by the user
 
Alas, no I'm not :-(

I did loads of code prior to realising this! When I try to add option strict I get error messages all over the place!
 
You need to explicitly cast variables as passing them to the constructor if they are of another type other than defined within the constructor.

The compiler will then be able to determine, which constructor you are trying to call.
 
Are you sure it's not a problem with your class member variables?
Seems you may be trying to assign a variable an inappropriate value when the constructor is passing the parameter.
Thus the narrowing conversion error.

Jon
 
Mmm me thinks I'm going to have to spend time getting the option strict to work to see if this resolves it.

I'll keep ya posted....thanks for the tips :-)
 
Back
Top