atyemail Posted August 11, 2013 Share Posted August 11, 2013 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 More sharing options...
0 jren207 Posted August 11, 2013 Share Posted August 11, 2013 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. Link to comment https://www.neowin.net/forum/topic/1170147-visual-basic-retail-calculator/#findComment-595873129 Share on other sites More sharing options...
0 atyemail Posted August 11, 2013 Author Share Posted August 11, 2013 I still do not get it i tried looking at several examples and the codes are very confusing because i do not see any connection between the subroutine functions toward what it links to... Can you maybe give me an example for this assignment? Link to comment https://www.neowin.net/forum/topic/1170147-visual-basic-retail-calculator/#findComment-595873161 Share on other sites More sharing options...
0 +Nik Louch Subscriber² Posted August 11, 2013 Subscriber² Share Posted August 11, 2013 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! Link to comment https://www.neowin.net/forum/topic/1170147-visual-basic-retail-calculator/#findComment-595873171 Share on other sites More sharing options...
0 atyemail Posted August 11, 2013 Author Share Posted August 11, 2013 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? Link to comment https://www.neowin.net/forum/topic/1170147-visual-basic-retail-calculator/#findComment-595873179 Share on other sites More sharing options...
0 astropheed Veteran Posted August 12, 2013 Veteran Share Posted August 12, 2013 "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. Link to comment https://www.neowin.net/forum/topic/1170147-visual-basic-retail-calculator/#findComment-595873183 Share on other sites More sharing options...
0 +Nik Louch Subscriber² Posted August 12, 2013 Subscriber² Share Posted August 12, 2013 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. Link to comment https://www.neowin.net/forum/topic/1170147-visual-basic-retail-calculator/#findComment-595873187 Share on other sites More sharing options...
0 atyemail Posted August 12, 2013 Author Share Posted August 12, 2013 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 Link to comment https://www.neowin.net/forum/topic/1170147-visual-basic-retail-calculator/#findComment-595873197 Share on other sites More sharing options...
0 +theblazingangel MVC Posted August 12, 2013 MVC Share Posted August 12, 2013 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. Link to comment https://www.neowin.net/forum/topic/1170147-visual-basic-retail-calculator/#findComment-595873241 Share on other sites More sharing options...
0 +theblazingangel MVC Posted August 12, 2013 MVC Share Posted August 12, 2013 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. Link to comment https://www.neowin.net/forum/topic/1170147-visual-basic-retail-calculator/#findComment-595873265 Share on other sites More sharing options...
0 atyemail Posted August 12, 2013 Author Share Posted August 12, 2013 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 Link to comment https://www.neowin.net/forum/topic/1170147-visual-basic-retail-calculator/#findComment-595873387 Share on other sites More sharing options...
0 atyemail Posted August 12, 2013 Author Share Posted August 12, 2013 So is this the correct way? Link to comment https://www.neowin.net/forum/topic/1170147-visual-basic-retail-calculator/#findComment-595873429 Share on other sites More sharing options...
0 +theblazingangel MVC Posted August 12, 2013 MVC Share Posted August 12, 2013 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. Link to comment https://www.neowin.net/forum/topic/1170147-visual-basic-retail-calculator/#findComment-595874153 Share on other sites More sharing options...
Question
atyemail
So this is what the assignment ask me:
Write an application that accepts from the user the wholesale cost of an item and its
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