dcsimg
Results 1 to 9 of 9
  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    9

    Doubling up a value!

    hello,

    how do I double up a value in VB? I tried simply multiplying with 2 but it didn't work. I tried using 2 as the constant variable, it still didn't work..

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,025

    Re: Doubling up a value!

    Post your code please. It might be that you are not assigning the result back to the variable.

    Code:
            Dim val As Double = 2
            val = val * 2
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,625

    Re: Doubling up a value!

    This also works:-
    Code:
    Dim val As Double = 2
    val *= 2
    It's the short hand version of what dbasnett posted.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,149

    Re: Doubling up a value!

    Quote Originally Posted by hoeleeschitt View Post
    hello,

    how do I double up a value in VB? I tried simply multiplying with 2 but it didn't work. I tried using 2 as the constant variable, it still didn't work..
    Then you did it wrong. If you show us what you did then we can tell you what's wrong with it. Your current code is ALWAYS relevant because, if nothing else, it can reveal flaws in your thinking. If those can be corrected then you may be able to avoid other errors in future.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,497

    Re: Doubling up a value!

    Neither dbasnett's or Niya's code will work, for instance if you put the code they posted in a button's click event.
    Every time you click the button the value would be initialized to 2, then multiplied to make it 4 and then be discarded when you exited the sub.
    Variable scope is yet just another way that you could have done it wrong, so I'm just reiterating jmcihinney's post about showing us what you did, so that an incomplete answer is not given. dbasnett and Niya are answering the operation question, but not addressing the scope issue, which could have been what your issue actually was.

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    9

    Re: Doubling up a value!

    i am trying to double the value of an input when a certain check box is selected. here's what i have so far:

    Code:
      If chkCredit.Checked = True Then 
                FinalDeposit = FinalDeposit * 2
            End If
    i tried declaring "2" as a constant and gave it a name as "BadCredit" but the code is still not working
    Code:
      If chkCredit.Checked = True Then 
                FinalDeposit = FinalDeposit * BadCredit
            End If
    here's my declaration:
    Code:
     Dim FinalRent As Integer 
    Dim FinalDeposit As Integer

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,149

    Re: Doubling up a value!

    That first code snippet will do exactly what it says on the tin. If the specified CheckBox is checked then it will double the value of the FinalDeposit variable. There's nothing wrong with that code based on what you want to do.

    As passel alluded to, one potential issue might be that you have declared the FinalDeposit variable inside the same method that you have placed the code to double it. In that case, that variable ceases to exist as soon as that method completes. If you call the method again, a new variable will be created, set, doubled and cease to exist. Another potential issue is that you never actually assign a value to it to be doubled, in which case it will remain the zero that it defaults to.

    I suggest that you show us the entire method that that first code snippet appears in. If you FinalDeposit variable is declared in that method then that's your issue. You need it declared outside any method if it is to survive between method calls. You should declare it Static but that is rarely a good idea.

    If it's not declared inside that method then show us where it is declared and also any other methods that it is used in.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    9

    Re: Doubling up a value!

    Quote Originally Posted by jmcilhinney View Post
    That first code snippet will do exactly what it says on the tin. If the specified CheckBox is checked then it will double the value of the FinalDeposit variable. There's nothing wrong with that code based on what you want to do.

    As passel alluded to, one potential issue might be that you have declared the FinalDeposit variable inside the same method that you have placed the code to double it. In that case, that variable ceases to exist as soon as that method completes. If you call the method again, a new variable will be created, set, doubled and cease to exist. Another potential issue is that you never actually assign a value to it to be doubled, in which case it will remain the zero that it defaults to.

    I suggest that you show us the entire method that that first code snippet appears in. If you FinalDeposit variable is declared in that method then that's your issue. You need it declared outside any method if it is to survive between method calls. You should declare it Static but that is rarely a good idea.

    If it's not declared inside that method then show us where it is declared and also any other methods that it is used in.
    i declared it at "Calculate Button". same place as the double :/ does this mean that i need to declare it at form_load?

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,149

    Re: Doubling up a value!

    Quote Originally Posted by hoeleeschitt View Post
    i declared it at "Calculate Button". same place as the double :/
    I assume that what you actually mean is that you declared it inside the Click event handler of that Button. In that case, the variable only exists inside that method. When that method isn't executing, the variable doesn't exist at all. When the method is called, which happens when you click the Button and it raises its Click event, the variable is created and you presumably set its value, then your code to double it is executed and then, when the method completes, the variable ceases to exist. The next time you click the Button, a new variable is created and reinitialised, so whatever value it had on the previous call is forgotten.
    Quote Originally Posted by hoeleeschitt View Post
    does this mean that i need to declare it at form_load?
    That would be even more useless, because if you declare it in the form's Load event handler then that's the only place it exists, so you couldn't refer to it at all in the Click event handler of your Button. If you want the value of the variable to persist between method calls then you can either declare it Static inside the method or declare it outside all methods. If you need to access the variable in more than one method then the latter is your only option. Test out this code to see how it works:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    4.         Dim num1 As Integer
    5.  
    6.         num1 += 1
    7.  
    8.         MessageBox.Show(num1.ToString())
    9.     End Sub
    10.  
    11.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    12.         Static num2 As Integer
    13.  
    14.         num2 += 1
    15.  
    16.         MessageBox.Show(num2.ToString())
    17.     End Sub
    18.  
    19.     Private num3 As Integer
    20.  
    21.     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    22.         num3 += 1
    23.  
    24.         MessageBox.Show(num3.ToString())
    25.     End Sub
    26.  
    27.     Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    28.         'num3 is accessible here because it is declared at an outer scope.
    29.         'Neither num1 nor num2 are accessible here.
    30.         num3 += 1
    31.  
    32.         MessageBox.Show(num3.ToString())
    33.     End Sub
    34.  
    35. End Class
    If you click Button1 repeatedly then you'll see that 'num1' has the value 1 every time. That's because the value of a standard local variable is not persisted across calls so it gets initialised to 0 every time.

    If you click Button2 repeatedly then you'll see that 'num2' increments in value each time. That's because the value of a static local variable is persisted across calls. That variable is still local though, so it's value can only be accessed in the scope that it is declared, i.e. inside the Click event handler of Button2.

    If you click Button3 repeatedly then you'll see that 'num3' increments in value each time because the variable continues to exist between calls so its value doesn't change between calls. Because 'num3' is declared outside all methods, i.e. at the class level, it is accessible inside all methods. That means that it can be accessed and incremented inside the Click event handler for Button4 too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width