-
Mar 18th, 2015, 02:37 PM
#1
Thread Starter
Registered User
Having problem in private and local variables ( arrays) :(
Hi there
In the following code I was trying to make the user capable of choosing a number form a Combo Box to create groups of text
boxes
and when that number is decreased the group's number decreases
but it gave me "local variable ( almost every variable ) cannot be referred to before it's declared "
what to do
PHP Code:
Public Class Form2
Dim MaxFieldsPreviousNum As Integer
Dim i As Integer = 10
Dim MedicineNameTxt(i) As TextBox
Dim MedicineNameLbl(i) As Label
Dim RequaredAmountTxt(i) As TextBox
Dim RequaredAmountLbl(i) As Label
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For n = 1 To i
MedicineNameLbl(n) = New Label
MedicineNameTxt(n) = New TextBox
RequaredAmountTxt(n) = New TextBox
RequaredAmountLbl(n) = New Label
Next
For x = 1 To 50
MaxFieldsNumCB.Items.Add(x)
Next
MaxFieldsNumCB.SelectedIndex = 9
End Sub
Private Sub MaxFieldsNumCB_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaxFieldsNumCB.DropDown
MaxFieldsPreviousNum = MaxFieldsNumCB.Text
End Sub
Private Sub MaxFieldsNum_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MaxFieldsNumCB.SelectedIndexChanged
If MaxFieldsPreviousNum > i Then
For x = (i + 1) To MaxFieldsPreviousNum
Controls.Remove(MedicineNameTxt(x))
MedicineNameTxt(x) = Nothing
'-----------------------------
Controls.Remove(MedicineNameLbl(x))
MedicineNameLbl(x) = Nothing
'-------------------------------
Controls.Remove(RequaredAmountTxt(x))
RequaredAmountTxt(x) = Nothing
'---------------------------------
Controls.Remove(RequaredAmountLbl(x))
RequaredAmountLbl = Nothing
Next
Dim i As Integer
i = MaxFieldsNumCB.Text
Dim MedicineNameTxt(i) As TextBox
Dim MedicineNameLbl(i) As Label
Dim RequaredAmountTxt(i) As TextBox
Dim RequaredAmountLbl(i) As Label
For n = 1 To i
MedicineNameLbl(n) = New Label
MedicineNameTxt(n) = New TextBox
RequaredAmountTxt(n) = New TextBox
RequaredAmountLbl(n) = New Label
Next
End If
MedicineNameLbl(1).Left = 813
MedicineNameLbl(1).Top = 64
MedicineNameLbl(1).Text = "اسم العينة"
Controls.Add(MedicineNameLbl(1))
'-------------------------
MedicineNameTxt(1).Top = 61
MedicineNameTxt(1).Left = 703
Controls.Add(MedicineNameTxt(1))
'------------------------------
RequaredAmountLbl(1).Left = 537
RequaredAmountLbl(1).Top = 64
RequaredAmountLbl(1).Text = "العدد"
Controls.Add(RequaredAmountLbl(1))
'------------------------------
RequaredAmountTxt(1).Top = 61
RequaredAmountTxt(1).Left = 409
Controls.Add(RequaredAmountTxt(1))
If i > 1 Then
For x = 2 To i
MedicineNameTxt(x).Top = MedicineNameTxt(x - 1).Top + 54
MedicineNameTxt(x).Left = 703
Controls.Add(MedicineNameTxt(x))
'--------------------------------
MedicineNameLbl(x).Top = MedicineNameLbl(x - 1).Top + 54
MedicineNameLbl(x).Left = 813
MedicineNameLbl(x).Text = "اسم العينة"
Controls.Add(MedicineNameLbl(x))
'----------------------------------
RequaredAmountTxt(x).Top = RequaredAmountTxt(x - 1).Top + 54
RequaredAmountTxt(x).Left = 409
Controls.Add(RequaredAmountTxt(x))
'---------------------------
RequaredAmountLbl(x).Left = 537
RequaredAmountLbl(x).Top = RequaredAmountLbl(x - 1).Top + 54
RequaredAmountLbl(x).Text = "العدد"
Controls.Add(RequaredAmountLbl(x))
Next
End If
End Sub
-
Mar 18th, 2015, 02:58 PM
#2
Re: Having problem in private and local variables ( arrays) :(
Where and when did you get that exception?
One thing to note is that arrays are 0 based, so when you are filling the arrays you are leaving index 0 empty, since you iterate from 1 to i.
My usual boring signature: Nothing
-
Mar 18th, 2015, 03:05 PM
#3
Re: Having problem in private and local variables ( arrays) :(
Ummm.... Up at the top of the class you have this:
Code:
Dim MaxFieldsPreviousNum As Integer
Dim i As Integer = 10
Dim MedicineNameTxt(i) As TextBox
Dim MedicineNameLbl(i) As Label
Dim RequaredAmountTxt(i) As TextBox
Dim RequaredAmountLbl(i) As Label
and then in the SelectedIndexChanged event handler you have this:
Code:
Dim i As Integer
i = MaxFieldsNumCB.Text
Dim MedicineNameTxt(i) As TextBox
Dim MedicineNameLbl(i) As Label
Dim RequaredAmountTxt(i) As TextBox
Dim RequaredAmountLbl(i) As Label
Which means the outer ones at the form level are never going to be used.
-tg
-
Mar 18th, 2015, 04:10 PM
#4
Thread Starter
Registered User
Re: Having problem in private and local variables ( arrays) :(
Originally Posted by techgnome
Ummm.... Up at the top of the class you have this:
Code:
Dim MaxFieldsPreviousNum As Integer
Dim i As Integer = 10
Dim MedicineNameTxt(i) As TextBox
Dim MedicineNameLbl(i) As Label
Dim RequaredAmountTxt(i) As TextBox
Dim RequaredAmountLbl(i) As Label
and then in the SelectedIndexChanged event handler you have this:
Code:
Dim i As Integer
i = MaxFieldsNumCB.Text
Dim MedicineNameTxt(i) As TextBox
Dim MedicineNameLbl(i) As Label
Dim RequaredAmountTxt(i) As TextBox
Dim RequaredAmountLbl(i) As Label
Which means the outer ones at the form level are never going to be used.
-tg
Umm.I tried to write it in the Form_load event to set a start value to the CB
Code:
MaxFieldsNumCB.SelectedIndex = 9
but it said "cannot be referred to before being declared" ...so I thought writing that in the public class will do it , but I guess it won't
my problem is in the following I have to make it remove the objects first , but all variables and objectives "cannot be referred to " even when declaring it in the public class
Code:
If MaxFieldsPreviousNum > i Then
For x = (i + 1) To MaxFieldsPreviousNum
Controls.Remove(MedicineNameTxt(x))
MedicineNameTxt(x) = Nothing
'-----------------------------
Controls.Remove(MedicineNameLbl(x))
MedicineNameLbl(x) = Nothing
'-------------------------------
Controls.Remove(RequaredAmountTxt(x))
RequaredAmountTxt(x) = Nothing
'---------------------------------
Controls.Remove(RequaredAmountLbl(x))
RequaredAmountLbl = Nothing
Next
End If
-
Mar 18th, 2015, 04:11 PM
#5
Thread Starter
Registered User
Re: Having problem in private and local variables ( arrays) :(
And sorry for my English , I'm Arab so ....
-
Mar 18th, 2015, 04:17 PM
#6
Thread Starter
Registered User
Re: Having problem in private and local variables ( arrays) :(
Originally Posted by Shaggy Hiker
Where and when did you get that exception?
One thing to note is that arrays are 0 based, so when you are filling the arrays you are leaving index 0 empty, since you iterate from 1 to i.
If MaxFieldsPreviousNum > i Then
For x = (i + 1) To MaxFieldsPreviousNum
Controls.Remove(MedicineNameTxt(x))
MedicineNameTxt(x) = Nothing
'-----------------------------
Controls.Remove(MedicineNameLbl(x))
MedicineNameLbl(x) = Nothing
'-------------------------------
Controls.Remove(RequaredAmountTxt(x))
RequaredAmountTxt(x) = Nothing
'---------------------------------
Controls.Remove(RequaredAmountLbl(x))
RequaredAmountLbl = Nothing
Next
all the variables and objects underlined :'/
-
Mar 18th, 2015, 10:57 PM
#7
Thread Starter
Registered User
Re: Having problem in private and local variables ( arrays) :(
My question is .. is there a way to set an selected amount of text boxes and add it ,and when the amount is decreased the text boxes number decreases as well ?
hope to find my answer
-
Mar 19th, 2015, 10:47 AM
#8
Re: Having problem in private and local variables ( arrays) :(
Yeah, there would be a way, but I've never liked that kind of approach, so I'd urge you to consider something different. The problem I have with adding and removing controls is that, at best, you can only do that for a little while before your form starts to look pretty bad...unless you are much more artistic than I am.
What I would do would be to create the maximum number of controls that seemed reasonable at design time, where I can lay them all out on the form and put them where I want them. I then hide the controls that won't be necessary. So, all the controls are there at all times, but some of them are just hidden.
The other alternative is to use a flowLayoutPanel.
If you really want to be adding and removing controls, then you will probably want to be using a List(of whatever), such as a List(of Textbox) for all your textboxes. The problem is that you will have to add handlers if you want to do anything with any of the events of the control, and you'd have to be sure to have RemoveHandler when you get rid of any of the controls, or else some strange things can happen. It can be done, you just have to be much more careful about it, which is why I build everything I can at design time and play with visibility rather than adding or removing the controls.
My usual boring signature: Nothing
-
Mar 19th, 2015, 02:26 PM
#9
Thread Starter
Registered User
Re: Having problem in private and local variables ( arrays) :(
Managed to do it somehow ,, I didn't now about ReDim
Code:
Public Class Form2
Dim MaxFieldsPreviousNum As Integer
Dim i As Integer = 10
Dim MedicineNameTxt(i) As TextBox
Dim MedicineNameLbl(i) As Label
Dim RequaredAmountTxt(i) As TextBox
Dim RequaredAmountLbl(i) As Label
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For n = 1 To i
MedicineNameLbl(n) = New Label
MedicineNameTxt(n) = New TextBox
RequaredAmountTxt(n) = New TextBox
RequaredAmountLbl(n) = New Label
Next
For x = 1 To 50
MaxFieldsNumCB.Items.Add(x)
Next
MaxFieldsNumCB.SelectedIndex = 9
End Sub
Private Sub MaxFieldsNumCB_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaxFieldsNumCB.DropDown
MaxFieldsPreviousNum = MaxFieldsNumCB.Text
End Sub
Private Sub MaxFieldsNum_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MaxFieldsNumCB.SelectedIndexChanged
Try
Dim i As Integer
i = MaxFieldsNumCB.Text
If MaxFieldsPreviousNum > i Then
For x = (i + 1) To MaxFieldsPreviousNum
Controls.Remove(MedicineNameTxt(x))
MedicineNameTxt(x) = Nothing
'-----------------------------
Controls.Remove(MedicineNameLbl(x))
MedicineNameLbl(x) = Nothing
'-------------------------------
Controls.Remove(RequaredAmountTxt(x))
RequaredAmountTxt(x) = Nothing
'---------------------------------
Controls.Remove(RequaredAmountLbl(x))
RequaredAmountLbl(x) = Nothing
Next
Else
ReDim Preserve MedicineNameTxt(i)
ReDim Preserve MedicineNameLbl(i)
ReDim Preserve RequaredAmountTxt(i)
ReDim Preserve RequaredAmountLbl(i)
For n = 1 To i
MedicineNameLbl(n) = New Label
MedicineNameTxt(n) = New TextBox
RequaredAmountTxt(n) = New TextBox
RequaredAmountLbl(n) = New Label
Next
MedicineNameLbl(1).Left = 813
MedicineNameLbl(1).Top = 64
MedicineNameLbl(1).Text = "اسم العينة"
Controls.Add(MedicineNameLbl(1))
'-------------------------
MedicineNameTxt(1).Top = 61
MedicineNameTxt(1).Left = 703
Controls.Add(MedicineNameTxt(1))
'------------------------------
RequaredAmountLbl(1).Left = 537
RequaredAmountLbl(1).Top = 64
RequaredAmountLbl(1).Text = "العدد"
Controls.Add(RequaredAmountLbl(1))
'------------------------------
RequaredAmountTxt(1).Top = 61
RequaredAmountTxt(1).Left = 409
Controls.Add(RequaredAmountTxt(1))
If i > 1 Then
For x = 2 To i
MedicineNameTxt(x).Top = MedicineNameTxt(x - 1).Top + 54
MedicineNameTxt(x).Left = 703
Controls.Add(MedicineNameTxt(x))
'--------------------------------
MedicineNameLbl(x).Top = MedicineNameLbl(x - 1).Top + 54
MedicineNameLbl(x).Left = 813
MedicineNameLbl(x).Text = "اسم العينة"
Controls.Add(MedicineNameLbl(x))
'----------------------------------
RequaredAmountTxt(x).Top = RequaredAmountTxt(x - 1).Top + 54
RequaredAmountTxt(x).Left = 409
Controls.Add(RequaredAmountTxt(x))
'---------------------------
RequaredAmountLbl(x).Left = 537
RequaredAmountLbl(x).Top = RequaredAmountLbl(x - 1).Top + 54
RequaredAmountLbl(x).Text = "العدد"
Controls.Add(RequaredAmountLbl(x))
Next
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
But one problem is that the removed objects from the array will be " nothing" and will stay at the memory and when I try to ReDim the array after removing some objects
Code:
Try
Dim i As Integer
i = MaxFieldsNumCB.Text
If MaxFieldsPreviousNum > i Then
For x = (i + 1) To MaxFieldsPreviousNum
Controls.Remove(MedicineNameTxt(x))
MedicineNameTxt(x) = Nothing
'-----------------------------
Controls.Remove(MedicineNameLbl(x))
MedicineNameLbl(x) = Nothing
'-------------------------------
Controls.Remove(RequaredAmountTxt(x))
RequaredAmountTxt(x) = Nothing
'---------------------------------
Controls.Remove(RequaredAmountLbl(x))
RequaredAmountLbl(x) = Nothing
Next
End if
ReDim Preserve MedicineNameTxt(i)
ReDim Preserve MedicineNameLbl(i)
ReDim Preserve RequaredAmountTxt(i)
ReDim Preserve RequaredAmountLbl(i)
-
Mar 20th, 2015, 09:49 AM
#10
Re: Having problem in private and local variables ( arrays) :(
You shouldn't need to know about Redim, because it no longer serves any purpose in .NET and is inefficient. What you should be using is Lists rather than arrays if you really want to add and remove items. So, you'd have two List(of Textbox) and two List(of Label). The List gives you .Add, .Insert, .Remove, and .RemoveAt methods along with a lot more. If you were to try to replicate the .Remove or .RemoveAt method with an array you'd have to manually move all the items down to get rid of the item that is now Nothing.
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|