-
Jan 3rd, 2014, 05:58 AM
#1
Thread Starter
Junior Member
Dictionaries?
Can someone tell me why the following code is always returning false? I have to populate a dictionary but if the key is already in the dictionary I should not. A list called MagazineTitles contains details of all the keys in the dictionary and I am trying to use a for each loop and if statement to determine the keys prior to insertion but even if the key is not in the dictionary or list I keep getting false returned and the item is not inserted.
Code:
Public Function addMagazine(ByVal aMagazine As EMagazine) As Boolean
'Preconditions: none
'Postconditions: If the title of aMagazine is not contained in MagazineTitles
'then aMagazine is added to the dictionary of magazines, with its title as
'key, and True is returned. Otherwise False is returned.
'Complete the code here
Dim result As Boolean
For Each title As String In MagazineTitles
If title = MainForm.nameTextBox.Text Then
result = False
Else
fMagazines.Add(MainForm.nameTextBox.Text, aMagazine)
result = True
End If
Next
Return result
End Function
-
Jan 3rd, 2014, 06:06 AM
#2
Re: Dictionaries?
The result will be the result of the last test, you are not exiting the loop when you have the 'result = True' part. Any reason why you are not actually using a Dictionary class and let it do the work for you?
-
Jan 3rd, 2014, 06:44 AM
#3
Thread Starter
Junior Member
Re: Dictionaries?
But surely if the dictionary is empty at first it should be returning 'true' and inserting?
-
Jan 3rd, 2014, 06:49 AM
#4
Re: Dictionaries?
This line:
Code:
For Each title As String In MagazineTitles
will not execute if MagazineTitles is empty.
I suggest you add an "If" statement to deal with that
-
Jan 3rd, 2014, 06:57 AM
#5
Re: Dictionaries?
Originally Posted by chris57828
I have to populate a dictionary but if the key is already in the dictionary I should not. A list called MagazineTitles contains details of all the keys in the dictionary and I am trying to use a for each loop and if statement to determine the keys prior to insertion
Are you aware that the Dictionary(Of TKey, TValue) class contains a method called ContainsKey() that takes a TKey and returns a Boolean?
-
Jan 3rd, 2014, 07:01 AM
#6
Re: Dictionaries?
Also note the Remarks section in the documentation for the Add method. You can use the Item property, which will overwrite the existing value should the key already exist, rather than throwing an exception as Add will do. If that is an acceptable thing for your code to do (e.g. you are checking because you pass the same object multiple times to your addMagazine method so you would be overwriting the object with itself) then that would avoid the need to do any checking at all.
-
Jan 3rd, 2014, 07:02 AM
#7
Re: Dictionaries?
Or alternatively dictionary.keys.contains(string)
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Jan 3rd, 2014, 08:03 AM
#8
Re: Dictionaries?
Originally Posted by chris57828
But surely if the dictionary is empty at first it should be returning 'true' and inserting?
No... it sets a variable to True, inserts the record, then goes on to the next item in the list, where it DOESN'T match, so it sets the variable to FALSE... and keeps going... then AFTER the loop, you finally return the variable, which will ALWAYS be false.
Your best bet would be to use the dictionary as they others have suggested... one thing to note, you insert into your dictionary, but you don't insert into your list... so how does it get populated? Even still, there are ways to check a dictionary for an existing key...
-tg
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
|