Jump to content

Quick assistance: Function calling

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
Just need a quick help from you guys, if possible as I am working on this visual basic (VB 2008.net) assignment for uni - windows A

I attempted to look up this in my visual basic textbook, but it seems useless to my eyes but borrowing your brains would be helpful, how can I call this function? It brings up the error - would you be able to correct it?


 Private Sub btnCost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCost.Click


        [B]DVDCosts()[/B]

        lblResults.Text = "Costs of Renting " & dvds(ix).category & txtDays.Text & " = $" & totalCost

    End Sub


    Function DVDCosts(ByVal rental As Decimal, ByVal days As Integer, ByVal activity As String) As Decimal

        Const RegularDiscount As Single = 0.95

        Const FrequentDiscount As Single = 0.9

        Const DaysWithoutDiscount As Integer = 7


        Dim DayDiscount = 0

        'Dim Totalcost As Integer



     Do While (totalDays > DaysWithoutDiscount) Then

            DayDiscount = totalDays - DaysWithoutDiscount

        Loop


        totalCost = (totalDays * rental) - DayDiscount

        Return totalCost


        If (activity = "R") Then

            totalCost = totalCost * RegularDiscount

        ElseIf (activity = "optFreq") Then

            totalCost = totalCost * FrequentDiscount

        End If

    End Function



You see, the bold text above the function code, is it the correct method to call the function? if not, could you correct me please? Thank you for your apperication in this. :thumbup1:

#2
Shaddix

Shaddix

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
that's not correct, a function gives a result, so you have to put the result of a function into a variable or something

like you call it, it doesn't have any affect

if you don't need it to return a value you should use a "sub"

if you can post the function itself I might be able to help you more

btw, I see you don't declare your variables at the top with "DIM", it's not bad, but a lot of programmers have the opinion this it's better to use DIM, but there are a lot of opinions over this topic

#3
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
Well am I right in thinking you aren't actually passing any of the values the function needs into the function?

DVDCosts(ByVal [b]rental[/b] As Decimal, ByVal [b]days[/b] As Integer, ByVal [b]activity[/b] As String) As Decimal

Also, you have a 'totalDays' var, yet I can't see a value given to that anywhere either. Is that all the code, or are these given values elsewhere?

Hope this helps a little bit :)

#4
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
Function code wasn't needed, so I removed it and replaced with sub DVDCosts, and it works fine. :S ALL is good now. :) Thanks

#5
wiltwipin

wiltwipin

    Newbie

  • Members
  • Pip
  • 1 posts
This is a very big undertaking to do a website like that. I'm just saying that if you want to make something like this, work your way up. Start with simple websites and add functionality afterwards. This is not a 5 hour website.