hog Posted May 6, 2003 Posted May 6, 2003 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 Quote My website
ailzaj Posted May 6, 2003 Posted May 6, 2003 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 Quote
*Experts* Nerseus Posted May 6, 2003 *Experts* Posted May 6, 2003 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 Quote "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
hog Posted May 7, 2003 Author Posted May 7, 2003 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? Quote My website
dsgreen57 Posted May 7, 2003 Posted May 7, 2003 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. Quote
hog Posted May 7, 2003 Author Posted May 7, 2003 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 Quote My website
ailzaj Posted May 7, 2003 Posted May 7, 2003 Are you using option strict? What code have you got in the constructors? ailzaj Quote
hog Posted May 7, 2003 Author Posted May 7, 2003 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! Quote My website
Heiko Posted May 7, 2003 Posted May 7, 2003 That could be the problem. We *are* using option strict and we do the stuff you described and we do not get the errors. Quote .nerd
*Gurus* divil Posted May 7, 2003 *Gurus* Posted May 7, 2003 You should always use Option Strict. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
dsgreen57 Posted May 7, 2003 Posted May 7, 2003 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. Quote
*Experts* jfackler Posted May 7, 2003 *Experts* Posted May 7, 2003 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 Quote
hog Posted May 7, 2003 Author Posted May 7, 2003 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 :-) Quote My website
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.