Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,098 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,867 people online right now. Registration is fast and FREE... Join Now!




Private Sub/Function

 
Reply to this topicStart new topic

Private Sub/Function, Keep getting a zero for a total

neongabby
22 Jun, 2008 - 05:35 PM
Post #1

New D.I.C Head
*

Joined: 6 Jun, 2008
Posts: 19


My Contributions
Top of the day to all:

I need to create a function that will return the cost of a single ingredient in the selected mixture. The function needs the following criteria: % of product, product size and market price.

Here is the code I have so far. Obviously I've mucked up somewhere as I keep getting a zero cost. I also attached a print screen of the user interface:
CODE

Public Class mainForm

    Private Function ingredientcost1()

        Const peanuts As Integer = 2.5 * 0.55
        Const cashews As Integer = 4.0 * 0.15
        Const hazelnuts As Integer = 7.5 * 0.25
        Const macadamia As Integer = 12.0 * 0.05
        Dim cost As Integer
        Dim markup As Integer
        Dim quantity As Integer
        Dim totalcost As Decimal
        totalLabel.Text = totalcost

        cost = peanuts + cashews + hazelnuts + macadamia
        markup = cost * 0.15
        totalcost = (cost + markup) * quantity

        Return totalcost

    End Function
    Private Function ingredientcost2()
        Const almonds As Integer = 6 * 0.15
        Const brazil As Integer = 0.25 * 5.5
        Const hazelnuts As Integer = 7.5 * 0.15
        Const cashews As Integer = 4 * 0.2
        Const sunflower As Integer = 2 * 0.25
        Dim cost As Integer
        Dim markup As Integer
        Dim quantity As Integer
        Dim totalcost As Decimal
        totalLabel.Text = totalcost

        cost = almonds + brazil + hazelnuts + cashews + sunflower
        markup = cost * 0.15
        totalcost = (cost + markup) * quantity

        Return totalcost

    End Function
    
    Private Sub mainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mixture1RadioButton.Focus()
        comboBox.Items.Add("2 lbs")
        comboBox.Items.Add("5 lbs")

    End Sub

    Private Sub orderButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles orderButton.Click

        Dim totalcost As Decimal

        If mixture1RadioButton.Checked AndAlso comboBox.SelectedIndex Then
            Call ingredientcost1()
        ElseIf mixture2RadioButton.Checked AndAlso comboBox.SelectedIndex Then
            Call ingredientcost2()
        End If
        totalLabel.Text = totalcost

    End Sub

    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub

    Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
        comboBox.SelectedIndex = False
        quantityTextBox.Text = String.Empty
        totalLabel.Text = String.Empty
    End Sub
End Class



Attached File(s)
Attached File  UserInterface.doc ( 73k ) Number of downloads: 5
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Private Sub/Function
22 Jun, 2008 - 07:46 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well one of the main problems with this is that you have defined variables as of type integer and then attempted to add to decimal values together.

What is an integer? It is a whole number. There are no decimals in an integer. So if I was to add 4.5 and .3 I would not get 4.8 in an integer I would get 4. It is not flagging it as an error because the conversion from a decimal to an integer can be done but the result is not accurate.

So first off, everywhere you are going to be adding two decimals together, make the variable a Decimal or a Double (depending on usage). Both of those data types handle decimal values.

Instead of defining "cost" as an integer, it would be a decimal. After converting all the variables to the proper data types, rerun the program and see what you get for an answer.

The second thing to pay attention to is that in your definition of the problem you state you need a function that takes three parameters. So that should be a tip to you that you need to create a function that takes in three parameters and return a value. If you look closely at your ingredient functions you will also notice that they are very similar. This is a big red flag that you could combine all your functions into a generic function that takes in the three parameters and returns a value. This is called "refactoring".

See what you can do with that and after the changes, show us what you got and what isn't working. We can then help you further.

Good luck. smile.gif
User is offlineProfile CardPM
+Quote Post

Damage
RE: Private Sub/Function
22 Jun, 2008 - 08:03 PM
Post #3

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 754



Thanked: 7 times
Dream Kudos: 75
My Contributions
Martyr makes some really good points, but your function is returning zero because you're declaring quantity but not assigning a value to it and then trying to use it later. Try giving it a value then see what happens


CODE

        Dim quantity As Integer = 6 'or a textbox value or whatever
        Dim totalcost As Decimal
        totalLabel.Text = totalcost

        cost = peanuts + cashews + hazelnuts + macadamia
        markup = cost * 0.15
        totalcost = (cost + markup) * quantity


This post has been edited by Damage: 22 Jun, 2008 - 08:17 PM
User is offlineProfile CardPM
+Quote Post

neongabby
RE: Private Sub/Function
23 Jun, 2008 - 04:54 AM
Post #4

New D.I.C Head
*

Joined: 6 Jun, 2008
Posts: 19


My Contributions
I made all the suggested changes. I still get a zero result. I went ahead and attached the full instructions that I have and the update code below. I am a beginner at this as you tell. I apologize for my lack of knowledge and experience.

Thanks again for the help.

Public Class mainForm

Private Function ingredientcost1(ByVal cost, _
ByVal markup, _
ByVal quantity)

Const peanuts As Decimal = 2.5 * 0.55
Const cashews As Decimal = 4.0 * 0.15
Const hazelnuts As Decimal = 7.5 * 0.25
Const macadamia As Decimal = 12.0 * 0.05

Dim totalcost As Decimal
quantityTextBox.Text = quantity
totalLabel.Text = totalcost

cost = peanuts + cashews + hazelnuts + macadamia
markup = cost * 0.15
totalcost = (cost + markup) * quantity

Return totalcost

End Function
Private Function ingredientcost2(ByVal cost, _
ByVal markup, _
ByVal quantity)

Const almonds As Decimal = 6 * 0.15
Const brazil As Decimal = 0.25 * 5.5
Const hazelnuts As Decimal = 7.5 * 0.15
Const cashews As Decimal = 4 * 0.2
Const sunflower As Decimal = 2 * 0.25

Dim totalcost As Decimal
quantityTextBox.Text = quantity
totalLabel.Text = totalcost

cost = almonds + brazil + hazelnuts + cashews + sunflower
markup = cost * 0.15
totalcost = (cost + markup) * quantity

Return totalcost

End Function

Private Sub mainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mixture1RadioButton.Focus()
comboBox.Items.Add("2 lbs")
comboBox.Items.Add("5 lbs")

End Sub

Private Sub orderButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles orderButton.Click

Dim cost As Decimal
Dim markup As Decimal
Dim quantity As Integer
Dim totalcost As Decimal


If mixture1RadioButton.Checked AndAlso comboBox.SelectedIndex Then
Call ingredientcost1(cost, markup, quantity)
ElseIf mixture2RadioButton.Checked AndAlso comboBox.SelectedIndex Then
Call ingredientcost2(cost, markup, quantity)
End If


totalLabel.Text = totalcost

End Sub

Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub

Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
comboBox.SelectedIndex = False
quantityTextBox.Text = String.Empty
totalLabel.Text = String.Empty
End Sub
End Class


Attached File(s)
Attached File  mixtureorder.doc ( 102k ) Number of downloads: 10
User is offlineProfile CardPM
+Quote Post

Damage
RE: Private Sub/Function
23 Jun, 2008 - 05:16 AM
Post #5

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 754



Thanked: 7 times
Dream Kudos: 75
My Contributions
dude welcome to the noob club smile.gif but the only way to learn is by asking if you don't understand something.

As far as i can tell your still not assigning quantity a value. In your code you say "quantitytextbox.text = quantity" this will output the value of quantity to the screen. Are you sure your not trying to say quantity = quantitytextbox.text? this would assign the value in the textbox to the variable quantity.
If that is what your trying to do the you do not need to pass the quantity variable in the function header ("ByVal quantity")
CODE


Private Function ingredientcost1(ByVal cost, ByVal markup,||ByVal quantity||) 'try take this part out  

Dim totalcost As Decimal
||quantityTextBox.Text = quantity||                      'try quantity = quantitytextbox.text
totalLabel.Text = totalcost



hope that helps

oh and same goes for all your other variables
Dim cost As Decimal
Dim markup As Decimal
Dim quantity As Integer
Dim totalcost As Decimal

just double check that you've assigned them a value before you try use them



This post has been edited by Damage: 23 Jun, 2008 - 05:13 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:27AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month