Declaring a Variable then Changing it later in a program

I'm a complete beginner at VB and programming in a general.  So forgive me in this question is a no-brainer.  Basically what I would like to do is have my program start with a variable with an initial value globally available then if the user chooses to a click a button have that variable changed through the input prompt and the returned new value to be carried back out to be available to the entire program.

At this point I have my variable declared at the beginning and globally availabe and I have the button click set up to prompt for a new input.  When the input is entered my lable change takes place and the new value is displayed as I want but the new value is not carried back into the main program for use by other routines.



Answer this question

Declaring a Variable then Changing it later in a program

  • ayeli

    Just figured it out.  The problem was when I redefined Elasticity in the button3 click event it became a temporary variable different than my global variable.  By just not defining it it defaults to the global one and everything works.  An incredibly simple fix man I feel a little stupid right now.
  • PrashanthSpark

    hi,

    you have two problems in your code

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    Dim Prompt As String ' the problem was here you have 2 variables have the same name so when you intialize your input box it give the value to the string variable not to the global variable so remove the local variable elasticity

    Prompt = "Please enter the Modulus of Elasticity in ksi."

    Elasticity = Double.Parse(InputBox(Prompt)) 'aslo you can parse the input directly here to double

    Label3.Text = ("Modulus of Elasticity is " & Elasticity & " ksi")

    End Sub

    Private Sub changevariable(ByVal NewElasticity As Double)

    'remove this sub you even didn't call it and you don't have to call it even

    Elasticity = NewElasticity

    End Sub

    hope it was helpfull



  • LucyS

    hi,

    if yo use this variable for particular sub   you can call the sub after you change the value



  • Eran Kampf

    hi,

    i don't know what you mean for example this is a form has label, textbox, button to call the sub is what you mean something like that

    Public Class Form1

    Private var As Integer = 1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Label1.Text = "hello"

    var = 2

    TextBox1.Text = var

    End Sub

    End Class

    if you read the value of variable from another sub it will change the variable but will not show its value in the form untill you call this sub

    i suggest to make a function or sub and you call it inside your code after your variable value changed like that

    Public Class Form1

    Private var As Integer = 1

    Private Sub changevariable(ByVal newvar As Integer)

    TextBox1.Text = newvar

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Label1.Text = "hello"

    var = 2

    changevariable(var)

    End Sub

    End Class

    hope it was helpfull



  • Michel

    I appreciate you help on this and I may be missing something simple because i said i'm a complete noob at this.  I couldn't get the code you pasted to work.  Perhaps it would be best for me to show you the code I and explain how I want it to work.

    Public Class Form1

    Dim Elasticity As Double = 29500

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    Dim Prompt, Elasticity As String

    Prompt = "Please enter the Modulus of Elasticity in ksi."

    Elasticity = InputBox(Prompt)

    Label3.Text = ("Modulus of Elasticity is " & Elasticity & " ksi")

    End Sub

    Private Sub changevariable(ByVal NewElasticity As Double)

    Elasticity = NewElasticity

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    End

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    MessageBox.Show(Elasticity)

    End Sub

    End Class

    Basically what this form is doing is initally setting Elasticity = 29500 then if Button3 is clicked it asks you for a new elasticity value and inputs that in the Label3.  Then on the button 1 click it pops up with a message box that shows the value of the Elasticity variable.  I need it to take the string value I'm inputting on the button3 click event and convert that to a double and replace the global Elasticity value with the new value I have entered through the Button3 click event.  Is this possible


  • AFuchigami

    Done and Done and its working great, thanks!
  • Mark Henneman

    you are welcome

  • Liren Zhao

    Exactly, thats what I want to do.  Start off with the variable as a default value then if the user chooses to change it have it changed to his new input and the changed variable will be used by later subroutine.  The problem is that initially I make the default value of the variable globally available, use a subroutine to allow the user to change it (this subroutine also changes a label to match the users input), but then when the subroutine closes yes my labels have changed the but new value is not available globally, its still the default value I had initally declared.
  • Declaring a Variable then Changing it later in a program