Reputation: 33
I've been programming for about two months now. I have a problem with keeping a dim value after an if statement. I have a loop that if doesn't add up to 100 (it is for a percentage) stops entering in a radgrid. My work around for this is to store the value in a textbox but this hardly seems efficient. So how do i store this value in memory as opposed to in a textbox. Here is the code.
Dim p As Integer = RadGridView1.CurrentRow.Index - 1
Try
For int1 = p To 0 Step -1
If RadGridView1.Rows(Row).Cells(1).IsCurrent AndAlso Me.RadGridView1.IsInEditMode AndAlso RadGridView1.ActiveEditor.Value <> 0 Then
If RadGridView1.Rows(int1).Cells(1).Value <> Nothing Then Exit For
Dim total As String = 0
total += RadGridView1.Rows(int1).Cells("PC").Value
Dim int2 As Integer
int2 += RadGridView1.Rows(int1 - 1).Cells("PC").Value
`this is my problem here i want this dim value after the if statement
Dim percenttotal = int2 + RadGridView1.Rows(Row - 1).Cells("PC").Value
'my workaround is to keep the value in a textbox
TextBox3.Text = int2 + RadGridView1.Rows(Row - 1).Cells("PC").Value
End If
Next
If RadGridView1.Rows(Row).Cells(1).IsCurrent AndAlso Me.RadGridView1.IsInEditMode AndAlso RadGridView1.ActiveEditor.Value <> 0 Then
' i want this code to be the above dim value not to come from a textbox
If TextBox3.Text <> 100 Then
MsgBox("Your % above does not equal 100", MsgBoxStyle.Critical, "Value Error")
e.Cancel = True
End If
End If
Upvotes: 0
Views: 836
Reputation: 4838
You are likely running into variable scoping issues.
Consider the following code:
Public Class Form1
Dim counter As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Integer
i += 1
counter += 1
MsgBox("i: was clicked " & i & " times!" & vbCrLf & "counter: was clicked " & counter & " times!")
End Sub
End Class
i is defined within the function, and therefore will have its value reset each time the function is called.
counter is defined as the class or module level, so it retains its value in between function calls and its value is persistent until the form closes.
See: https://msdn.microsoft.com/en-us/library/ms973875.aspx
Edit: The other folks are more correct in that a variable defined with a "block" like If, For, While, etc. will have the scope of that block.
See: https://msdn.microsoft.com/en-us/library/1t0wsc67.aspx
Upvotes: 0
Reputation: 16991
You are having a problem with Scope
. I like to think of Scope as a set of boxes, like those nesting Russian dolls. You can keep smaller boxes inside other boxes. A new Scope gets created and placed INSIDE the previous scope every time code enters a Sub , function , loop , and a few other cases. When you declare a variable it is placed in the current scope. Your code will be able to access the variables from any scope that it is currently inside, but it can't see any variables that are declared inside the smaller scopes inside it. Your code "can see out, but not in".
The problem that you are having is that you are declaring your variable inside the "If" scope, and when it hits the "End If" that scope goes away, taking your variable with it. The solution is to declare your variable at a higher scope so that it sticks around for the entire time you need it. Try declaring your variable BEFORE the "If" and setting it's value inside.
BTW, this is also why the form controls work for storing your data. They exist at a very high scope, the lifetime of the form instance.
You can read more about how scope works in VB.NET here https://msdn.microsoft.com/en-us/library/1t0wsc67.aspx
Upvotes: 1