Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Function; changing ByRef to ByVal

Function; changing ByRef to ByVal

Scheduled Pinned Locked Moved Visual Basic
question
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aqzman_
    wrote on last edited by
    #1

    Hey guys, I'm writing this program for a visual basic class I'm taking in college, but I can't seem to figure this out. I have a function wrote by it passes some day ByRef, it gives me the right anwser, but I showed it to my teacher and she said I need to change it to ByVal, but when I do that it gives me an anwser of 0. So does anyone know how I'd be able change the function to ByVal and still give me the right anwser? Thanks What I'm trying to change is "Function CalcOwed(ByVal Number As Double, ByRef Data As Double) As Double" to "Function CalcOwed(ByVal Number As Double, ByVal Data As Double) As Double". Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim TaxIncome, Amount, Count As Double lstDisplay.Items.Clear() Input(TaxIncome, Amount) Display(Amount) End Sub Sub Input(ByRef TaxIncome As Double, ByRef Amount As Double) TaxIncome = CDbl(InputBox("Enter your taxable income", , "")) If TaxIncome > 0 Then CalcOwed(TaxIncome, Amount) Else Do While TaxIncome <= 0 TaxIncome = CDbl(InputBox("Enter your taxable income, above zero this time", , "")) Loop CalcOwed(TaxIncome, Amount) End If End Sub Sub Display(ByVal Amount As Double) lstDisplay.Items.Add("You owe " & (FormatCurrency(Amount))) End Sub Function CalcOwed(ByVal Number As Double, ByRef Data As Double) As Double Select Case Number Case 1 To 7150 Data = Number * 0.1 Case 7150 To 29050 Data = (Number * 0.15) + 715 Case 26050 To 70350 Data = (Number * 0.25) + 4000 Case 70350 To 146750 Data = (Number * 0.28) + 14325 Case 146750 To 319100 Data = (Number * 0.33) + 35717 Case Else Data = (Number * 0.35) + 92595.5 Return Number End Select Return Number End Function Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Close() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click lstDisplay.Items.Clear() End Sub End Class

    O 1 Reply Last reply
    0
    • A aqzman_

      Hey guys, I'm writing this program for a visual basic class I'm taking in college, but I can't seem to figure this out. I have a function wrote by it passes some day ByRef, it gives me the right anwser, but I showed it to my teacher and she said I need to change it to ByVal, but when I do that it gives me an anwser of 0. So does anyone know how I'd be able change the function to ByVal and still give me the right anwser? Thanks What I'm trying to change is "Function CalcOwed(ByVal Number As Double, ByRef Data As Double) As Double" to "Function CalcOwed(ByVal Number As Double, ByVal Data As Double) As Double". Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim TaxIncome, Amount, Count As Double lstDisplay.Items.Clear() Input(TaxIncome, Amount) Display(Amount) End Sub Sub Input(ByRef TaxIncome As Double, ByRef Amount As Double) TaxIncome = CDbl(InputBox("Enter your taxable income", , "")) If TaxIncome > 0 Then CalcOwed(TaxIncome, Amount) Else Do While TaxIncome <= 0 TaxIncome = CDbl(InputBox("Enter your taxable income, above zero this time", , "")) Loop CalcOwed(TaxIncome, Amount) End If End Sub Sub Display(ByVal Amount As Double) lstDisplay.Items.Add("You owe " & (FormatCurrency(Amount))) End Sub Function CalcOwed(ByVal Number As Double, ByRef Data As Double) As Double Select Case Number Case 1 To 7150 Data = Number * 0.1 Case 7150 To 29050 Data = (Number * 0.15) + 715 Case 26050 To 70350 Data = (Number * 0.25) + 4000 Case 70350 To 146750 Data = (Number * 0.28) + 14325 Case 146750 To 319100 Data = (Number * 0.33) + 35717 Case Else Data = (Number * 0.35) + 92595.5 Return Number End Select Return Number End Function Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Close() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click lstDisplay.Items.Clear() End Sub End Class

      O Offline
      O Offline
      OldWarhorse
      wrote on last edited by
      #2

      The reason she wants you to change it to ByVal is to see if you understand the difference between passing arguments by value and passing them by reference. Obviously, you don't yet. You just skimmed over that section in the book, and you can't do that. This key point is fundamental to just about all programming languages, not just VisualBasic. We could just show you how to change it and make it "give the right answer", but that would doing you and the industry a grave disservice. So go back to your book, find that section, and re-read it. Several times if necessary. This may also help. Visual Basic Language Concepts Differences Between Passing an Argument By Value and By Reference http://msdn2.microsoft.com/en-us/library/eek064h4(VS.80).aspx[^] Hint 1: ByRef gives your procedure code a direct view back to the original variable. ByVal gives your procedure code a COPY of, and an indirect view of, the VALUE of the original variable. Hint 2: Why code a Function if you are just going to call it as a Sub and not use the return value from it? ;)

      A 1 Reply Last reply
      0
      • O OldWarhorse

        The reason she wants you to change it to ByVal is to see if you understand the difference between passing arguments by value and passing them by reference. Obviously, you don't yet. You just skimmed over that section in the book, and you can't do that. This key point is fundamental to just about all programming languages, not just VisualBasic. We could just show you how to change it and make it "give the right answer", but that would doing you and the industry a grave disservice. So go back to your book, find that section, and re-read it. Several times if necessary. This may also help. Visual Basic Language Concepts Differences Between Passing an Argument By Value and By Reference http://msdn2.microsoft.com/en-us/library/eek064h4(VS.80).aspx[^] Hint 1: ByRef gives your procedure code a direct view back to the original variable. ByVal gives your procedure code a COPY of, and an indirect view of, the VALUE of the original variable. Hint 2: Why code a Function if you are just going to call it as a Sub and not use the return value from it? ;)

        A Offline
        A Offline
        aqzman_
        wrote on last edited by
        #3

        Thanks a lot for the help. =) I'll have another read over the chapter in my book, and thanks for the link. As for coding the function, rather then just doing it all in the sub procedure, for some reason our teacher wants it done in a function. heh Thanks again. -- modified at 12:08 Sunday 5th November, 2006 Edit: I got it working, thanks for all the help. =)

        O 1 Reply Last reply
        0
        • A aqzman_

          Thanks a lot for the help. =) I'll have another read over the chapter in my book, and thanks for the link. As for coding the function, rather then just doing it all in the sub procedure, for some reason our teacher wants it done in a function. heh Thanks again. -- modified at 12:08 Sunday 5th November, 2006 Edit: I got it working, thanks for all the help. =)

          O Offline
          O Offline
          OldWarhorse
          wrote on last edited by
          #4

          Excellent. Good work! :-D

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups