Results 1 to 12 of 12

Thread: [RESOLVED] [2005] For Each Loop

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Location
    UK
    Posts
    96

    Resolved [RESOLVED] [2005] For Each Loop

    Hi

    I'm having some problems with a For Each loop and wondered if anyone could help.

    I have a combo box (coPropID) on my screen that has been preloaded with the IDs of all people in my database. I have another combo called coProp that has been preloaded with the names of all the people on the database in the same order, so that index 0 of coPropID contains the ID for the person whose name appears in the index 0 of coProp. When this screen is loaded to show an existing record, I retrieve the person's ID from the database and want to be able to use it to find the name in the coProp combobox.

    I'm using a For Each loop to find the id retrieved from the database (which has been assigned to a string called supStr) and find the index within the combobox that this ID sits on. I then want to set the index of the coProp combobox to that index and display the name.

    However, it is not working. I have tried to step through it, but it appears to look at the first item in the combo, not finding a match and not looping through to any of the other items. My code appears below:

    Code:
    If supStr IsNot "" Then
                comboColl = coPropID.Items()
                For Each thisObject As String In comboColl
                    If thisObject <> supStr Then
                        'go to next thisObject
                    Else
                        ind = coPropID.Items.IndexOf(supStr)
                        Exit For
                    End If
                Next thisObject
                coProp.SelectedIndex = ind
            End If

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: [2005] For Each Loop

    From what I understand, you dont actually need a for loop at all.
    You can use the
    ind = coPropID.Items.IndexOf(supStr)
    line to tell you what the index of that item is, check if its a positive number (ie its in the list) and if so, use it to select the other one.

    The for loop looks ok to me, but without seeing the data, cant tell what its doing.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Location
    UK
    Posts
    96

    Re: [2005] For Each Loop

    Quote Originally Posted by Grimfort
    From what I understand, you dont actually need a for loop at all.
    You can use the
    ind = coPropID.Items.IndexOf(supStr)
    line to tell you what the index of that item is, check if its a positive number (ie its in the list) and if so, use it to select the other one.

    The for loop looks ok to me, but without seeing the data, cant tell what its doing.

    Thanks Grimfort. I've tried just using the IndexOf line but am getting the same problems.

    Code:
    If supStr IsNot "" Then
                ind = coPropID.Items.IndexOf(supStr)
                If ind >= 0 Then
                    coProp.SelectedIndex = ind
                End If
            End If
    supStr is variable that is local to my sub and is being set shortly before the above section of code. However the line "If supStr IsNot "" Then" has a warning attached to it saying that supStr is being used before it has been assigned a value. The IndexOf function produces a result of -1 and despite my checking if ind is greater than or equal to 0, it still goes into this piece of code and assigns the selectedIndex of coProp to -1!!! Am I missing something here?! I'm totally confused!

  4. #4
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] For Each Loop

    How are you populating this Combobox? If you use a dataset as the source then you can bind the values to the box using the DisplayMember and ValueMember
    VB Code:
    1. Dim ds As Dataset
    2. 'Code to Fill Dataset With names and ID's
    3. Me.ComboBox1.DataSource = ds.Tables(0)
    4. Me.ComboBox1.ValueMember = "ID" 'The field name for the id
    5. Me.ComboBox1.DisplayMember = "Name" 'The field name for the name

    You can then just get the SelectedValue for the item that is the text that you want to return the ID
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  5. #5
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] For Each Loop

    Instead of using two comboxs I use on combo and an array of integers to hold the primary key of the row from the database select query. I redim the array with preserve keywork then place the PK integer value there and the words into the combobox. When someone selects something from the combobox I check if the selectedindex is greater then -1 if it is I use the selectedindex to get the same intem from the array: int = arPKs(me.combobox.SelectedIndex)
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: [2005] For Each Loop

    Quote Originally Posted by obscuregirl
    The IndexOf function produces a result of -1 and despite my checking if ind is greater than or equal to 0, it still goes into this piece of code and assigns the selectedIndex of coProp to -1!!! Am I missing something here?! I'm totally confused!
    If this is happening you got problems . Is "ind" actually declared as an integer? Thats the only thing I can think of. You shouldnt be able to do that anyway if you have option explict on.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Location
    UK
    Posts
    96

    Re: [2005] For Each Loop

    Quote Originally Posted by Grimfort
    If this is happening you got problems . Is "ind" actually declared as an integer? Thats the only thing I can think of. You shouldnt be able to do that anyway if you have option explict on.

    ind is declared as an integer and option explicit is on!!

    I've even tried:

    If ind <> -1 Then

    but it just seems to ignore this check completely! it's just weird.

  8. #8
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: [2005] For Each Loop

    Step through the code with breakpoints and watch it, this sounds strange. Are you editing on the fly and not recompiling your code? Ive not played about with edit-and-continue but I have heard of "difficulties" .

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Location
    UK
    Posts
    96

    Re: [2005] For Each Loop

    Quote Originally Posted by bmahler
    How are you populating this Combobox? If you use a dataset as the source then you can bind the values to the box using the DisplayMember and ValueMember
    VB Code:
    1. Dim ds As Dataset
    2. 'Code to Fill Dataset With names and ID's
    3. Me.ComboBox1.DataSource = ds.Tables(0)
    4. Me.ComboBox1.ValueMember = "ID" 'The field name for the id
    5. Me.ComboBox1.DisplayMember = "Name" 'The field name for the name

    You can then just get the SelectedValue for the item that is the text that you want to return the ID

    thanks, but there is a reason why I can't use a dataset. Can't remember what it was now, but know that some time ago it was deemed not to be an option!

    I was originally doing it based on the name of the assigned person (Title Initials Forename Surname) which seemed to work until I noticed that it actually didn't work for anyone who didn't have a forename specified! I thought doing it on the ID might be more reliable!

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Location
    UK
    Posts
    96

    Re: [2005] For Each Loop

    Quote Originally Posted by Grimfort
    Step through the code with breakpoints and watch it, this sounds strange. Are you editing on the fly and not recompiling your code? Ive not played about with edit-and-continue but I have heard of "difficulties" .

    ummm.... not quite sure what you mean... VS won't let me edit the code when I'm stepping through it. I have to stop it running to edit... is that what you mean?

  11. #11
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] For Each Loop

    hmmm. Seems strange that you would not be able to use a dataset. I can't think of any reasons not too off the top of my head, but anyway,
    Another option is to use a class to store your id and name
    VB Code:
    1. 'The Classs
    2. Public Class dataItem
    3.     Private _id As String
    4.     Private _value As String
    5.     Private _value2 As String
    6.     Private _value3 As String
    7.  
    8.     'Constructors
    9.     Public Sub New(ByVal id As String, ByVal value As String)
    10.         _id = id
    11.         _value = value
    12.     End Sub
    13.  
    14.     Public Sub New(ByVal id As String, ByVal value As String, ByVal value2 As String)
    15.         _id = id
    16.         _value = value
    17.         _value2 = value2
    18.     End Sub
    19.  
    20.     Public Sub New(ByVal id As String, ByVal value As String, ByVal value2 As String, ByVal value3 As String)
    21.         _id = id
    22.         _value = value
    23.         _value2 = value2
    24.         _value3 = value3
    25.     End Sub
    26.  
    27.     'Properties
    28.     Public ReadOnly Property ID() As String
    29.         Get
    30.             Return _id
    31.         End Get
    32.     End Property
    33.  
    34.     Public ReadOnly Property Value() As String
    35.         Get
    36.             Return _value
    37.         End Get
    38.     End Property
    39.  
    40.     Public ReadOnly Property Value2() As String
    41.         Get
    42.             Return _value2
    43.         End Get
    44.     End Property
    45.  
    46.     Public ReadOnly Property Value3() As String
    47.         Get
    48.             Return _value3
    49.         End Get
    50.     End Property
    51.  
    52.     'Overrides
    53.     Public Overrides Function ToString() As String
    54.         Return Value
    55.     End Function
    56.  
    57. End Class
    58.  
    59.  
    60. 'Implementing it
    61. Me.ComboBox1.Items.Add(New DataItem(ID, Name))
    62.  
    63. 'Retrieving value
    64.  
    65. Dim ID As Integer
    66.  
    67. For I As Integer = 0 To Me.ComboBox1.Items.Count - 1
    68.     If Me.ComboBox1.Items.Item(i).ToString() = Value Then
    69.         ID = CType(Me.ComboBox1.Items(i), DataItem).ID
    70.         Exit For
    71.     End If
    72. Next
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Location
    UK
    Posts
    96

    Re: [2005] For Each Loop

    Thanks everyone for your help. Much appreciated. I've done it a completely different way now because the logic-defying behaviour was freaking me out a little!

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