|
-
Feb 22nd, 2007, 09:48 AM
#1
Thread Starter
Lively Member
[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
-
Feb 22nd, 2007, 09:55 AM
#2
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.
-
Feb 22nd, 2007, 10:17 AM
#3
Thread Starter
Lively Member
Re: [2005] For Each Loop
 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!
-
Feb 22nd, 2007, 10:21 AM
#4
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:
Dim ds As Dataset
'Code to Fill Dataset With names and ID's
Me.ComboBox1.DataSource = ds.Tables(0)
Me.ComboBox1.ValueMember = "ID" 'The field name for the id
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
-
Feb 22nd, 2007, 10:25 AM
#5
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
-
Feb 22nd, 2007, 10:27 AM
#6
Re: [2005] For Each Loop
 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.
-
Feb 22nd, 2007, 10:31 AM
#7
Thread Starter
Lively Member
Re: [2005] For Each Loop
 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.
-
Feb 22nd, 2007, 10:33 AM
#8
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" .
-
Feb 22nd, 2007, 10:35 AM
#9
Thread Starter
Lively Member
Re: [2005] For Each Loop
 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:
Dim ds As Dataset
'Code to Fill Dataset With names and ID's
Me.ComboBox1.DataSource = ds.Tables(0)
Me.ComboBox1.ValueMember = "ID" 'The field name for the id
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!
-
Feb 22nd, 2007, 10:44 AM
#10
Thread Starter
Lively Member
Re: [2005] For Each Loop
 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?
-
Feb 22nd, 2007, 10:53 AM
#11
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:
'The Classs
Public Class dataItem
Private _id As String
Private _value As String
Private _value2 As String
Private _value3 As String
'Constructors
Public Sub New(ByVal id As String, ByVal value As String)
_id = id
_value = value
End Sub
Public Sub New(ByVal id As String, ByVal value As String, ByVal value2 As String)
_id = id
_value = value
_value2 = value2
End Sub
Public Sub New(ByVal id As String, ByVal value As String, ByVal value2 As String, ByVal value3 As String)
_id = id
_value = value
_value2 = value2
_value3 = value3
End Sub
'Properties
Public ReadOnly Property ID() As String
Get
Return _id
End Get
End Property
Public ReadOnly Property Value() As String
Get
Return _value
End Get
End Property
Public ReadOnly Property Value2() As String
Get
Return _value2
End Get
End Property
Public ReadOnly Property Value3() As String
Get
Return _value3
End Get
End Property
'Overrides
Public Overrides Function ToString() As String
Return Value
End Function
End Class
'Implementing it
Me.ComboBox1.Items.Add(New DataItem(ID, Name))
'Retrieving value
Dim ID As Integer
For I As Integer = 0 To Me.ComboBox1.Items.Count - 1
If Me.ComboBox1.Items.Item(i).ToString() = Value Then
ID = CType(Me.ComboBox1.Items(i), DataItem).ID
Exit For
End If
Next
-
Feb 22nd, 2007, 11:39 AM
#12
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|