• 0

Visual Basic - Retail Calculator


Question

  So this is what the assignment ask me:

Write an application that accepts from the user the wholesale cost of an item and its

markup percentage. (For example, if an item?s wholesale cost is $5 and its retail
price is $10, then the markup is 100%.) 
The program should contain a function named CalculateRetail that receives the
wholesale cost and markup percentage as arguments, and returns the retail price of
the item.
 
The one that i am confuse is that "The program should contain a function named CalculateRetail that receives the
wholesale cost and markup percentage as arguments, and returns the retail price of
the item." 
Can anybody help me to interpret what' CalculateRetail?
Link to comment
https://www.neowin.net/forum/topic/1170147-visual-basic-retail-calculator/
Share on other sites

12 answers to this question

Recommended Posts

  • 0

I guess the assignment means, create your own function/subroutine (sub) to calculate the markup. Just make sure you name your sub as the assignment specifies, and trigger it in the rest of your code.

  • 0

You seem to REALLY not understand what is being asked.  Let me try:

 

* Write an application

* It has two textboxes - one for "Wholesale Cost" and one for "Markup Percentage"

* In the code, create a function (sub?) that takes two inputs (matching the above textboxes) and returns the retail price

* Name this function "CalculateRetail"

 

Nobody is going to (nor should they) tell you what to do here - it's your assignment.  But "CalculateRetail" is simply a name for the function.  Don't get hung up on that!

  • 0

The only part that i do not understand is the function since i never make any functions.

I already make a form with 3 labels 3 textboxes (Wholesale cost, Markup%, Retail Price) but than we need to make function and i never make function

Like i already saw several examples of functions but i still do no get it so do we just make it like

Private sub CalculateRetail? or smthing like that?

and how about the code inside it?

  • 0

"CalculateRetail" - is it within the style conventions of VB to name the first character of a function with an uppercase character? How irritating; I'm aware it's merely semantics, but I adhere to a strict visual guideline with my code.

  • 0

I think that's the part of the assignment YOU are meant to learn...

 

But let me explain...  A function is a self-contained bit of code that performs a function.  In this case it does your calculation, but let's look at it another way...

 

We could want all text in a textbox to be uppercase.  Could write that code for EVERY textbox, or just write it once as a function and call it as needed.

  • 0

Okay guide me please, so this is my code so far:

Option Strict On
Option Explicit On
 
Public Class Form1
 
    Private Sub btnGet_Click(sender As Object, e As EventArgs) Handles btnGet.Click
        TextBox3.Text = CStr(Val(CDbl(TextBox1.Text) + (CDbl(TextBox1.Text) * (CDbl(TextBox2.Text) * 0.01))))
    End Sub
    Dim CalculateRetail As String
    Private Function CalculateRetail()
 
    End Function
    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
    End Sub
 
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        End
    End Sub
End Class
 
And is that the right way to make functions?
It says that the CalculateRetail has an error : Option strict on requires all Function, property, and Operator declarations to have an 'As' Clause
  • 0

Go back to your "BillCalc" assignment. The following are all functions: btnGo_Click, AddBillsToList, ComputeBillTotal, ClearAll. A function is simply a wrapper around a block of code. A function can be used ("called") from other code. When your code calls a function, the code in that function is then run before returning back to where you called it and continuing onwards. This was done in the btnGo_Click function, where you called some of the other functions to get stuff done.

Note that the btnGo_Click function itself is run upon clicking the "btnGo" button in the program's interface. Understand that the interface for your program was designed and built simply with drag and drop tools, but behind the scenes this is translated into a lot of code that's hidden from you which gets the computers operating system to construct and display this interface when your program is run. It also sets up the btnGo_Click function to be executed on clicking the respective button. While this is hidden behind the scenes for you in VB, in c++ such stuff is not hidden from you, it can be a lot more involved.

Note that sub/subroutine is just another term for a function. Also note that in the "BillCalc" assignment all functions are wrapped up within a "class" called "frmExQ6". When a function is contained within a class the more correct technical term instead of function is "method". Classes are part of something called "object oriented programming" (OOP) but don't worry about that now, you'll learn about that stuff later. The "public/private" keyword is also something to do with oop, don't worry about it's meaning just yet.

So, your assignment wants you to create a function which you should call "CalculateRetail". The function that is executed on clicking the button in this assignment (the button's click "event handler") should capture the data and then call this "CalculateRetail" function to get the calculation done. It is in principle good practice to break code up into multiple functions.

You should be learning simple stuff like this from that book you've got. Once you've got the beginnings of a function set up, go back to your book to learn about function arguments/parameters to understand how to give data to your function and about data being returned from functions. Come back if you get stuck.

  • 0
  On 12/08/2013 at 00:20, atyemail said:

Okay guide me please, so this is my code so far:

Option Strict On

Option Explicit On

Public Class Form1

Private Sub btnGet_Click(sender As Object, e As EventArgs) Handles btnGet.Click

TextBox3.Text = CStr(Val(CDbl(TextBox1.Text) + (CDbl(TextBox1.Text) * (CDbl(TextBox2.Text) * 0.01))))

End Sub

Dim CalculateRetail As String

Private Function CalculateRetail()

End Function

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click

TextBox1.Clear()

TextBox2.Clear()

TextBox3.Clear()

End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click

End

End Sub

End Class

And is that the right way to make functions?

It says that the CalculateRetail has an error : Option strict on requires all Function, property, and Operator declarations to have an 'As' Clause

No, see my post above (which I started before this post of yours). The terms Sub/ subroutine and function mean the same thing, but specific languages require specific terms, in this case "sub" is the term you should be using. Your "Dim CalculateRetail As String" line should not exist. Your btnGet_Click function is currently doing the maths, which it shouldn't, CalculateRetail should be doing it, and btnGet_Click should be calling the CalculateRetail function, letting it calculate and return the answer. Look into function arguments/parameters and return values.

  • 0

So is it something like this:

Option Strict On
Option Explicit On
 
Public Class Form1
 
    Private Sub btnGet_Click(sender As Object, e As EventArgs) Handles btnGet.Click
        If Not IsNumeric(TextBox1.Text & TextBox2.Text) Then
            MessageBox.Show("Numeric")
            Return
        End If
        If CDbl(TextBox1.Text & TextBox2.Text) < 1 Then
            MessageBox.Show("Positiveer Only")
            Return
        End If
        TextBox3.Text = CStr((CDbl(TextBox1.Text) + (CDbl(TextBox1.Text) * (CDbl(TextBox2.Text) * 0.01))))
        'Focus
        TextBox1.Focus()
        TextBox1.SelectionStart = 0
        TextBox1.SelectionLength =
            TextBox1.TextLength
    End Sub
 
    Private Sub CalculateRetail()
        Dim answer As Double
        answer = CDbl(CStr((CDbl(TextBox1.Text) + (CDbl(TextBox1.Text) * (CDbl(TextBox2.Text) * 0.01)))))
        Return
    End Sub
 
    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
    End Sub
 
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        End
    End Sub
End Class
  • 0

Getting there! Note that you're still doing the calculation in btnGet_Click, which you shouldn't be. Understand that a function can return a value when it finishes. You do this by putting the name of the variable who's value you wish to return after the return keyword, and back in the code where you call this function, you capture the returned value by assigning it into a variable (or if you like assign it directly into the output text box). This is the next thing you need to get working.

Next, if you read the assignment again it is clear that CalculateRetail should not be accessing the values in the text boxes itself directly. btnGet_Click should be accessing them and giving the values to the CalculateRetail function when you call it. Go back to your BillCalc assignment again. Look at how the AddBillsToList and ComputeBillTotal functions are called within the btnGo_Click function. When calling ComputeBillTotal you are passing it the variable dblTotBills as an argument (aka parameter). If you look at the ComputeBillTotal function itself you will see how it has a single argument defined called dblVal which you can think of as a variable that recieves what it being given when the function is called and can be used within the function. Your CalculateRetail function, as per the description in the assignment brief needs two arguments, one for a wholesale cost figure and one for a markup figure. Your btnGet_Click function should get these values from the text boxes and give them to CalculateRetail via it's arguments.

Consult your book to understand the difference between "ByVal" and "ByRef" argument types.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.