Dictionaries?-VBForums
Results 1 to 8 of 8

Thread: Dictionaries?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2013
    Posts
    25

    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

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

    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?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2013
    Posts
    25

    Re: Dictionaries?

    But surely if the dictionary is empty at first it should be returning 'true' and inserting?

  4. #4
    Frenzied Member
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,044

    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

  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,394

    Re: Dictionaries?

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

  6. #6
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,394

    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.

  7. #7
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    2,197

    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.
    The 4:1 Principal. If you reprimand once, praise four times otherwise you are just a boss and not a teacher.
    ________________________________________________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  8. #8
    PowerPoster
    Join Date
    May 2002
    Posts
    25,462

    Re: Dictionaries?

    Quote Originally Posted by chris57828 View Post
    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
    * 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??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.