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.

Declaring a Variable then Changing it later in a program
ayeli
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 elasticityPrompt =
"Please enter the Modulus of Elasticity in ksi."Elasticity =
Double.Parse(InputBox(Prompt)) 'aslo you can parse the input directly here to doubleLabel3.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 evenElasticity = NewElasticity
End Subhope 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.ClickLabel1.Text =
"hello"var = 2
TextBox1.Text = var
End SubEnd
Classif 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.ClickLabel1.Text =
"hello"var = 2
changevariable(var)
End SubEnd
Classhope 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 StringPrompt =
"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.ClickMessageBox.Show(Elasticity)
End SubEnd
ClassBasically 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
Mark Henneman
Liren Zhao