Click to See Complete Forum and Search --> : Invoke Parameterized Constructor
ryanbutler
09-19-2007, 04:22 PM
I have the following application (this resides inside one public class, Slip):
Two constructors, one w/ a parameter, one w/out like so:
Public Overrides Function LeaseSlip()
End Sub
Public Overrides Function LeaseSlip(ByVal aDiscountPrice as Single)
End Sub
I have since created two more constructors like so:
Public Overrides Function GetFormattedCurrency()
End Sub
Public Overrides Function GetFormattedCurrency(ByVal aDiscountPrice as Single)
End Sub
How can I invoke the parameterized constructor LeaseSlip from the parameterized constructor of GetFormattedCurrency?
I've tried (inside GetFormattedCurrency (parameterized)):
Dim aDiscountedPrice=Me.LeaseSlip()
Appreciate any help.
Thanks,
lmf232s
09-19-2007, 04:37 PM
Try
Dim aDiscountedPrice = Slip.LeaseSlip()
ryanbutler
09-20-2007, 08:03 AM
These really aren't constructors, more just overloaded methods, sorry for the confusion.
That didn't work because I was referencing a non-shared method. Hmmm...this is sort of irritating :D
lmf232s
09-20-2007, 10:47 AM
This may or may not be what your after but give this a shot. Pay no attention to the return values as they dont match. I was just puttin gstuff in to satisfy the code.
Public Class slip
Public Shared Function LeaseSlip()
Dim retValue As Boolean = True
Return retValue
End Function
Public Shared Function LeaseSlip(ByVal Something As String)
Dim retValue As Boolean = True
Return retValue
End Function
Public Shared Function GetFormattedCurrency()
Dim retValue As Boolean = True
Return retValue
End Function
Public Shared Function GetFormattedCurrency(ByVal aDiscountPrice As Single)
Dim aDiscountedPrice As String = LeaseSlip()
Return aDiscountedPrice
End Function
End Class
ryanbutler
09-20-2007, 03:27 PM
Thanks for the help...I'm still not quite following how the second overloaded parameterized method GetFormattedCurrency is going to invoke the overloaded parameterized method LeaseSlip.
Perhaps I should wait and ask the professor.
lmf232s
09-21-2007, 09:33 AM
Well ryan I don't know the specific terminology but I put this class together in VS to make sure it all worked correctly. Basically because they are shared functions your able to make call to them.
If you were to type this up in VS you'll see that as you start to type LeasSlip it will show you that it has 2 overloads for that function. 1 with no parameters and the 2nd one that will accept 1 parameter.
Again this is possible because they are shared.
ryanbutler
09-21-2007, 09:54 AM
That's true, but these overloaded methods aren't supposed to be shared.
lmf232s
09-21-2007, 01:36 PM
ryan,
whats up. Ok I think i might have it for you this time.
Look at your original code and youll notice that its set up like this
Public Overrides ...............
I dont believe you can use Overrides unless your overriding a base class.
An example of this would be overriding the property that sets the StyleSheetTheme. That code would look like this:
Public Overrides Property StyleSheetTheme() As String
Get
Return GetTheme()
End Get
Set(ByVal value As String)
End Set
End Property
'Or overriding the Initialize sub.
Public Overrides Sub Initialize(ByVal name As String, ByVal attributes As System.Collections.Specialized.NameValueCollection)
End Sub
Based on what your trying to do I think you should be using Overloads instead of Overrides. Here is your original code again but changed to use overrides. This will allow you to override a function or a sub and you are able to make a call to one of those functions or subs with in that class only.
Public Class slip
Public Overloads Function LeaseSlip() As String
Return "LeasSlip Overloads 1"
End Function
Public Overloads Function LeaseSlip(ByVal aDiscountPrice As Single) As String
Return "LeasSlip Overloads 2 w/ Value: " & aDiscountPrice
End Function
Public Overloads Function GetFormattedCurrency() As String
Return "GetFormattedCurrency Overloads 1"
End Function
Public Overloads Function GetFormattedCurrency(ByVal aDiscountPrice As Single) As String
Return Me.LeaseSlip()
End Function
End Class
ryanbutler
09-22-2007, 11:32 AM
Thanks for the help. Actually, I have it figured out except I can figure out why the compiler doesn't like something. I have the following setup:
Public Overloads Function GetFormattedFee() As Single
'invoke LeaseSlip to get fee
Dim formattedFee As Single = Me.LeaseSlip
Return formattedFee
End Function
Public Overloads Function GetFormattedFee(ByVal aDiscountedPercent As Single) As Single
'invoke LeaseSlip for discounted fee
Dim discountedFee As Single = Me.LeaseSlip(ByVal aDiscountPercent As Single)as Single
'Return discountedPercent
End Function
Then I have LeaseSlip:
'custom method LeaseSlip calculates and returns fee
Public Overloads Function LeaseSlip() As Single
Dim fee As Single
Select Case slipWidth
Case 10
fee = 800
Case 12
fee = 900
Case 14
fee = 1100
Case 16
fee = 1500
Case Else
fee = 0
End Select
Return fee
End Function
'overloaded custom method LeaseSlip if discount requested
Public Overloads Function LeaseSlip(ByVal aDiscountPercent As Single) As Single
'invoke LeaseSlip() to get fee
Dim fee As Single = Me.LeaseSlip()
'calculate and return discount fee
Dim discountFee As Single = fee * (100 - aDiscountPercent) / 100
Return discountFee
End Function
All I need to is invoke the parameterized method LeaseSlip from the parameterized method of GetFormattedFee from GetFormattedFee. So it would look like this:
Public Overloads Function GetFormattedFee(ByVal aDiscountedPercent As Single) As Single
'invoke LeaseSlip for discounted fee
Dim discountedFee As Single = Me.LeaseSlip(ByVal aDiscountPercent As Single)as Single 'Return discountedPercent
End Function
However, the compiler chokes on the argument and I can't figure out why at the moment. My object-oriented skills are most assuredly intermediate.
lmf232s
09-24-2007, 12:59 PM
Change
Dim discountedFee As Single = Me.LeaseSlip(ByVal aDiscountPercent As Single)as Single
TO
Dim discountedFee As Single = Me.LeaseSlip(aDiscountPercent)
You dont have to declare the parameter or a return type as you have it. You just need to pass a value to Leaseslip. Leaseslip declares the parameter and the return type.
ryanbutler
09-25-2007, 08:01 AM
Thanks, did the trick.
lmf232s
09-25-2007, 09:16 AM
ryan,
I see your in Springfield, MO and you mentioned something about your professor. What school are you going to? SMS/MSU, OTC, Drury?
I graduated from SMS back in early 2000. I miss it, had a lot of good times down there.
ryanbutler
09-25-2007, 12:00 PM
MSU. I'm a transfer student taking CIS 334 with Shannon McMurtrey. I've had a little bit of object-oriented programming but there's still stuff way over my head. By choice, I'm not a full programmer, but intermediate. Probably be an analyst one of these days. Springfield is a lovely town, been here all my life. If you like routine, and simplicity of life, its for you. Though KC is good to, been there before.
Hopefully I'll be done a year from this coming spring.
lmf232s
09-25-2007, 01:16 PM
McMurtrey is a good teacher. He was actually one of the best programming teachers I had.
There's a DB teacher as well but I can't remember his name. I took a DB class with him and a programming class. He had Long hair (think its grey) and wore glasses. Really cool guy looks like the type who smoked some pot back in day, lol.
Other than that there were not that many good CIS teachers. Not sure if you have had Dick Dick (Richard Johnson) yet, I hated him. I would advise staying away from any of his classes. Although he was the only one teaching Java at the time.
Any way good luck.
ryanbutler
09-26-2007, 08:08 AM
Nothing like carrying on a conversation in a forum! My only complaint with programming instructors in they tend to throw stuff at you and leave you to sit there and think "how am I going to do this?" But overall, McMurtrey is the most practical I've had, except for a few at OTC.
You don't have to worry about Johnson, if he's teaching Java, it's below a 300 level course, so I won't be taking it. I'm sure I'll be back for me help before all is said and done.