Wrox Programmer Forums
BOOK: Beginning Microsoft Visual Basic 2008 ISBN: 978-0-470-19134-7
This is the forum to discuss the Wrox book Beginning Microsoft Visual Basic 2008 by Thearon Willis, Bryan Newsome; ISBN: 9780470191347
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Microsoft Visual Basic 2008 ISBN: 978-0-470-19134-7 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 3rd, 2008, 11:35 AM
Registered User
Join Date: Oct 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default calling a sub

hey im new to VB & mostly understand pseudo code, i have a program which has a sub (method? - function?) what it does is calculate a number when you hit a "calculate" button. i created a menu item called calculate and i am trying to call the function that calculates without duplicating the code...heres what it looks like, should i create a totally seperate function that calcualtes and call this function from both the botton click and the menu select items?

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
        Dim BCuser As Long
        Dim IMuser As Long
        Dim BLuser As Long
        Dim AVuser As Long
        Dim ENuser As Long
        Dim MAuser As Long
        Dim PMuser As Long
        Dim EMuser As Long
        Dim BC2user As Long
        Dim AMVCost As Long
        Dim MRR As Long

        EMuser = txtAMuser.Text
        BCuser = txtBCuser.Text
        IMuser = txtIMuser.Text
        BLuser = txtBLuser.Text
        AVuser = txtAVuser.Text
        ENuser = txtENuser.Text
        MAuser = txtMAuser.Text
        PMuser = txtPMuser.Text
        BC2user = txtBC2user.Text

        MyCost= ((7 * EMuser) + (2.1 * (BCuser + BC2user + PMuser + AVuser)) + (3.5 * (IMuser + BLuser)) + (2.5 * ENuser) + (4.2 * MAuser))
        MRR = ((10 * EMuser) + (3 * (AVuser + BCuser + BC2user)) + (5 * (IMuser + BLuser)) + (3.5 * ENuser) + (6 * MAuser) + (4 * PMuser))

        MsgBox("This deal will cost Me $" & MyCost & " per month. Your Client will be charged $" & MRR & " per month.")

End Sub

    Private Sub CalculateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateToolStripMenuItem.Click

//Heres where i want to call the previous function//

    End Sub

Old October 3rd, 2008, 02:07 PM
Friend of Wrox
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts

First of all: Both functions and subs are methods and can also jointly be called "subroutines". In C#/Java/C++, there are only functions, and the difference is only in whether they return void (no value) or not. VB has a long legacy of the two different names, but truly the only difference is whether the method returns a value (function) or not (sub).


Do you want both methods to do exactly the same thing, *INCLUDING* displaying that MsgBox?

If so, you could just code
Private Sub CalculateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles CalculateToolStripMenuItem.Click
    Button1_Click( sender, e )
End Sub

But another trick is to just have one method handle multiple events:
Private Sub GiveMsgBoxAboutCost(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles CalculateButton.Click, CalculateToolStripMenuItem.Click
End Sub
Nobody said that the name of the sub has to match the name of the event it is handling, at all. So why not use a name that is meaningful to you?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling C# from C NoXuS C# 10 May 12th, 2006 10:40 AM
Calling C# DLL from C virajithasarma C# 1 August 17th, 2005 04:01 AM
cannot use parentheses when calling a sub Adam H-W Classic ASP Basics 5 October 29th, 2004 03:41 AM
Let's keep it friendly here - no name calling jminatel Forum and Wrox.com Feedback 0 March 16th, 2004 10:33 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.