Results 1 to 10 of 10

Thread: Having problem in private and local variables ( arrays) :(

  1. #1

    Thread Starter
    Registered User
    Join Date
    Mar 2015
    Posts
    7

    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.ObjectByVal e As System.EventArgsHandles MyBase.Load
            
    For 1 To i
                MedicineNameLbl
    (n) = New Label
                MedicineNameTxt
    (n) = New TextBox
                RequaredAmountTxt
    (n) = New TextBox
                RequaredAmountLbl
    (n) = New Label
            Next
            
    For 1 To 50
                MaxFieldsNumCB
    .Items.Add(x)
            
    Next
            MaxFieldsNumCB
    .SelectedIndex 9
        End Sub

        
    Private Sub MaxFieldsNumCB_DropDown(ByVal sender As ObjectByVal e As System.EventArgsHandles MaxFieldsNumCB.DropDown
            MaxFieldsPreviousNum 
    MaxFieldsNumCB.Text
        End Sub
        
    Private Sub MaxFieldsNum_SelectedIndexChanged(ByVal sender As System.ObjectByVal e As System.EventArgsHandles MaxFieldsNumCB.SelectedIndexChanged
           
            
    If MaxFieldsPreviousNum i Then
                
    For = (1To 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 
    1 Then
                
    For 2 To i
                    MedicineNameTxt
    (x).Top MedicineNameTxt(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(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 

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Registered User
    Join Date
    Mar 2015
    Posts
    7

    Re: Having problem in private and local variables ( arrays) :(

    Quote Originally Posted by techgnome View Post
    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

  5. #5

    Thread Starter
    Registered User
    Join Date
    Mar 2015
    Posts
    7

    Re: Having problem in private and local variables ( arrays) :(

    And sorry for my English , I'm Arab so ....

  6. #6

    Thread Starter
    Registered User
    Join Date
    Mar 2015
    Posts
    7

    Re: Having problem in private and local variables ( arrays) :(

    Quote Originally Posted by Shaggy Hiker View Post
    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 :'/

  7. #7

    Thread Starter
    Registered User
    Join Date
    Mar 2015
    Posts
    7

    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

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  9. #9

    Thread Starter
    Registered User
    Join Date
    Mar 2015
    Posts
    7

    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
    
    Removing "Else
    End if ReDim Preserve MedicineNameTxt(i) ReDim Preserve MedicineNameLbl(i) ReDim Preserve RequaredAmountTxt(i) ReDim Preserve RequaredAmountLbl(i)

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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
  •  



Click Here to Expand Forum to Full Width