Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 .....

 


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!!

 


Public New(ByVal lngEquipmentID as Long)

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

 

Thnx

My website
Posted

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

  • *Experts*
Posted

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:

Public [b]Sub[/b] New(ByVal lngEquipmentID As Long)
'instead of
Public New(ByVal lngEquipmentID As Long)

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Ooops!

 


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?

My website
Posted

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.

Posted

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

My website
Posted

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!

My website
Posted

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.

  • *Experts*
Posted

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

Posted

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 :-)

My website

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...