SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Wizard rbutler's Avatar
    Join Date
    Jul 2003
    Location
    Springfield, MO
    Posts
    1,867
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Invoke Parameterized Constructor

    I have the following application:

    Two constructors, one w/ a parameter, one w/out like so:

    Code VBNET:
    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:

    Code VBNET:
    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 using an object?

    Would it be something like?

    Code VBNET:
    Dim aDiscountedPrice=Me.LeaseSlip()

    Appreciate any help.

    Thanks,
    Ryan Butler

    Midwest Web Design

  2. #2
    SitePoint Member
    Join Date
    Sep 2007
    Location
    China
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Public Overrides Function GetFormattedCurrency(ByVal aDiscountPrice as Single)

    return new LeaseSlip(aDiscountPrice)

    End Sub

    perhaps like this?hoho~,sorry ,my English is not well
    LeadNT.COM

    Lead new technology

  3. #3
    SitePoint Wizard rbutler's Avatar
    Join Date
    Jul 2003
    Location
    Springfield, MO
    Posts
    1,867
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply...I'll give that a shot.
    Ryan Butler

    Midwest Web Design

  4. #4
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,576
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Consult the MSDN documentation for constructors, I think they cover how to handle inherited constructors there. Not sure what the VB syntax is, but in C# you would do something like:

    Code:
    public class foo 
    {
        foo(string s) { //construct }
    }
    
    public class bar : foo
    {
        bar() : base("some string") { }
    }

  5. #5
    SitePoint Wizard rbutler's Avatar
    Join Date
    Jul 2003
    Location
    Springfield, MO
    Posts
    1,867
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well that didn't work. More Googling.
    Ryan Butler

    Midwest Web Design

  6. #6
    SitePoint Guru Wullie's Avatar
    Join Date
    Oct 2002
    Location
    Greenock, Scotland
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Those are functions not constructors (unless you are using a singleton design pattern in your objects)

    You need to use the NEW keyword for constructors in VB.NET. Also, constructors CANNOT return a value.

    To do what you want you should write your code as follows

    Code:
    Public Class LeaseSlip()
    
        Sub New()
        End Sub
    
        Sub New(ByVal aDiscountPrice as Single)
        End Sub
    
    End Class
    
    Public Class GetFormattedCurrency()
    
        Private _leaseSlip as LeaseSlip
    
        Sub New()
        End Sub
    
        Sub New(ByVal aDiscountPrice as Single)
            _leaseSlip = New LeaseSlip(aDiscountPrice)
        End Sub
    
    End Class
    ASP.NET Freelance Web Developer
    Bored? Check out my photography folio at Flickr

  7. #7
    SitePoint Wizard rbutler's Avatar
    Join Date
    Jul 2003
    Location
    Springfield, MO
    Posts
    1,867
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I realized after I posted, that I confused constructors with functions. Really, what I was after was with this VB.NET code:

    Code VBNET:
    Public Overloads Function LeaseSlip(ByVal aDiscountPrice as Single)
     
    End Function
     
    Public Overloads Function GetFormattedFee(byVal aDiscountedPrice as Single)
     
    End Function

    Is be able to invoke the parameterized LeaseSlip method from the parameterized GetFormattedFee method. For some reason:

    Code VBNET:
    Me.LeaseSlip(aDiscountFee)

    Doesn't work.
    Ryan Butler

    Midwest Web Design

  8. #8
    SitePoint Guru Wullie's Avatar
    Join Date
    Oct 2002
    Location
    Greenock, Scotland
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are not setting a return type for those functions, do they need to be functions or could they just be subs?

    Not sure why you would be having any trouble doing something as simple as you are saying. The code below should work. If not you could post the complete object code to give me a better understanding of what you are tyring to do.

    Public Overloads Function LeaseSlip(ByVal aDiscountPrice as Single)
    'do something
    End Function

    Public Overloads Function GetFormattedFee(byVal aDiscountedPrice as Single)
    LeaseSlip(aDiscountedPrice)
    End Function
    ASP.NET Freelance Web Developer
    Bored? Check out my photography folio at Flickr

  9. #9
    SitePoint Wizard rbutler's Avatar
    Join Date
    Jul 2003
    Location
    Springfield, MO
    Posts
    1,867
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    VB.NET Code (pretty straightforward):


    Code VBNET:
    Public Class Slip
     
        'attributes
        Private slipId As Integer
        Private slipWidth As Integer
        Private slipLength As Integer
     
       'shared (class) attribute 
        Private Shared numberOfSlips As Integer = 0
     
       'constants 
       Private Const DEFAULT_SLIP_WIDTH As Integer = 12
       Private Const DEFAULT_SLIP_LENGTH As Integer = 25
     
       '1-parameter constructor 
        'Overloads keyword not used with constructors
        Public Sub New(ByVal aSlipId As Integer)
          'invoke 3-parameter constructor, passing default values
            Me.New(aSlipId, DEFAULT_SLIP_WIDTH, DEFAULT_SLIP_LENGTH)
        End Sub
     
       '3-parameter constructor 
        Public Sub New(ByVal aSlipId As Integer, ByVal aSlipWidth As Integer, _
         ByVal aSlipLength As Integer)
            'invoke setter methods to populate attributes
            SetSlipId(aSlipId)
            SetSlipWidth(aSlipWidth)
            SetSlipLength(aSlipLength)
            'increment shared attribute
          numberOfSlips += 1
        End Sub
        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
     
        '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
     
        'custom method TellAboutSelf
        Public Overrides Function ToString() As String
            Dim info As String
            info = "Slip: Id = " & GetSlipId() & ", Width = " & GetSlipWidth() _
             & ", Length = " & GetSlipLength()
            Return info
        End Function
     
        'shared (class) accessor method 
        Public Shared Function GetNumberOfSlips() As Integer
            Return numberOfSlips
        End Function
     
        'get accessor methods
        Public Function GetSlipId() As Integer
            Return slipId
        End Function
        Public Function GetSlipWidth() As Integer
            Return slipWidth
        End Function
        Public Function GetSlipLength() As Integer
            Return slipLength
        End Function
     
        'set accessor methods
        Public Sub SetSlipId(ByVal aSlipId As Integer)
            slipId = aSlipId
        End Sub
        Public Sub SetSlipWidth(ByVal aSlipWidth As Integer)
            slipWidth = aSlipWidth
        End Sub
        Public Sub SetSlipLength(ByVal aSlipLength As Integer)
            slipLength = aSlipLength
        End Sub
     
    End Class


    In the parameterized function for GetFormattedFee I can't get the parameterized function LeaseSlip to pass correctly.
    Ryan Butler

    Midwest Web Design

  10. #10
    SitePoint Guru Wullie's Avatar
    Join Date
    Oct 2002
    Location
    Greenock, Scotland
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is a few syntax errors in GetFormattedFee. I fixed them and it appeared to work ok.

    Code:
    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
    should be


    Code:
    Public Overloads Function GetFormattedFee(ByVal aDiscountedPercent As Single) As Single
            'invoke LeaseSlip for discounted fee
            Dim discountedFee As Single = Me.LeaseSlip(aDiscountedPercent)
            Return discountedFee
        End Function
    ASP.NET Freelance Web Developer
    Bored? Check out my photography folio at Flickr

  11. #11
    SitePoint Wizard rbutler's Avatar
    Join Date
    Jul 2003
    Location
    Springfield, MO
    Posts
    1,867
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I just figured that out. Thanks for your help.
    Ryan Butler

    Midwest Web Design

  12. #12
    SitePoint Guru Wullie's Avatar
    Join Date
    Oct 2002
    Location
    Greenock, Scotland
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No probs
    ASP.NET Freelance Web Developer
    Bored? Check out my photography folio at Flickr


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •