SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Scripting.Dictionary – get value from it without for each loop

    Hello all

    The below statement prints all of the makeId’s within a Scripting.Dictionary. This code works just fine.

    Code:
    for each makeList in activeMakesObj.MakeList.Items
       Response.Write( makeList.makeId )
    next
    My issue is that I want to get just one value (the first value) from the Scripting.Dictionary. I should not have to use a loop to get that value but I am having issues getting a value without using this loop.

    Can someone help me write this code so I am only getting the first item in the Scripting.Dictionary?

    If this helps, my Scripting.Dictionary key is the primary key from the table (So it is not always 0,1,2,3, etc). Then the value is an object (I named this object cMaintMake).

    Here is part of the code I use to populate the Dictionary object. All of this code works if I use the for each loop above but I would like to get data out of it without using the loop… how could I write a response.write statement that gets the first value in the Dictionary?

    Code:
    'Purpose: Loops thru the array results set and builds the object list
          '         UBound( ArrayName, 2) - returns the number of rows in the array
             For i = 0 To UBound( rsArray, 2 )
                Set myMake = New cMaintMake 
           
                myMake.success = "Y"
                myMake.mode = "Update"
                myMake.makeId = rsArray( cMakeId, i )
                myMake.lastMntDate = rsArray( cLastMntDate, i )
                myMake.active = rsArray( cMakeActive, i )
                myMake.makeDesc = Replace( rsArray( cMakeDesc, i ), "'", "^%^" )
                myMake.makeServerFileName = rsArray( cMakeServerFileName, i )
                myMake.useMakeLogo = rsArray( cUseMakeLogo, i )
                myMake.makeSortOrder = rsArray( cMakeSortOrder, i )
             
                m_cMaintMake.Add myMake.makeId, myMake
             Next
    Here is what cMaintMake looks like…

    Code:
    <%
    Class cMaintMake
    
    '---------------------------------------
    'Purpose: Private class member variables
    '---------------------------------------
       
       Private m_Success
       Private m_Mode
       Private m_MakeId
       Private m_LastMntDate
       Private m_Active
       Private m_MakeDesc
       Private m_MakeServerFileName
       Private m_UseMakeLogo
       Private m_MakeSortOrder
       
    '----------------------------------------   
    'Purpose: Get the success query indicator
    '----------------------------------------
       Public Property Get success()
          success = m_Success    
       End Property      
    
    '----------------------------------------   
    'Purpose: Set the success query indicator
    '----------------------------------------   
       Public Property Let success( p_Success )
          m_Success = p_Success
       End Property  
    
    '---------------------------   
    'Purpose: Get the Mode value
    '---------------------------
       Public Property Get mode()
          mode = m_Mode    
       End Property      
    
    '---------------------------   
    'Purpose: Set the Mode value
    '---------------------------   
       Public Property Let mode( p_Mode )
          m_Mode = p_Mode
       End Property 
    
    '-----------------------------   
    'Purpose: Get the MakeId value
    '-----------------------------
       Public Property Get makeId()
          makeId = m_MakeId    
       End Property
    
    '-----------------------------   
    'Purpose: Set the MakeId value
    '-----------------------------   
       Public Property Let makeId( p_MakeId )
          m_MakeId = p_MakeId
       End Property   
    
    '----------------------------------   
    'Purpose: Get the LastMntDate value
    '----------------------------------
       Public Property Get lastMntDate()
          lastMntDate = m_LastMntDate    
       End Property
    
    '----------------------------------   
    'Purpose: Set the LastMntDate value
    '----------------------------------   
       Public Property Let lastMntDate( p_LastMntDate )
          m_LastMntDate = p_LastMntDate
       End Property  
    
    '------------------------------
    'Purpose: Get the Active value
    '------------------------------
       Public Property Get active()
          active = m_Active    
       End Property
    
    '------------------------------ 
    'Purpose: Set the Active value
    '------------------------------   
       Public Property Let active( p_Active )
          m_Active = p_Active
       End Property
    
    '-------------------------------
    'Purpose: Get the MakeDesc value
    '-------------------------------
       Public Property Get makeDesc()
          makeDesc = m_MakeDesc    
       End Property
    
    '-------------------------------   
    'Purpose: Set the MakeDesc value
    '-------------------------------   
       Public Property Let makeDesc( p_MakeDesc )
          dim replaceTick
          
          replaceTick = Replace( p_MakeDesc, "^%^", "'" ) 
          
          m_MakeDesc = p_MakeDesc
       End Property
    
    '-----------------------------------------
    'Purpose: Get the MakeServerFileName value
    '-----------------------------------------
       Public Property Get makeServerFileName()
          makeServerFileName = m_MakeServerFileName    
       End Property
    
    '----------------------------------------- 
    'Purpose: Set the MakeServerFileName value
    '-----------------------------------------  
       Public Property Let makeServerFileName( p_MakeServerFileName )
          m_MakeServerFileName = p_MakeServerFileName
       End Property
    
    '------------------------------------
    'Purpose: Get the UseMakeLogo value
    '------------------------------------
       Public Property Get useMakeLogo()
          useMakeLogo = m_UseMakeLogo    
       End Property
    
    '---------------------------------- 
    'Purpose: Set the UseMakeLogo value
    '----------------------------------  
       Public Property Let useMakeLogo( p_UseMakeLogo )
          m_UseMakeLogo = p_UseMakeLogo
       End Property
    
    '------------------------------------
    'Purpose: Get the MakeSortOrder value
    '------------------------------------
       Public Property Get makeSortOrder()
          makeSortOrder = m_MakeSortOrder    
       End Property
    
    '------------------------------------  
    'Purpose: Set the MakeSortOrder value
    '------------------------------------   
       Public Property Let makeSortOrder( p_MakeSortOrder )
          m_MakeSortOrder = p_MakeSortOrder
       End Property
       
       
    End Class
    %>
    Thanks in advance for your time.

  2. #2
    SitePoint Zealot trekmp's Avatar
    Join Date
    Dec 2005
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, try this 0 is to get the first item from the dictionary:


    Code:
    myDD=makeList.Keys
    
    Response.Write myDD(0) 'To return the key value
    Response.Write makeList(myDD(0)) 'To return the value
    Hope this helps
    Why do we exist? What is our purpose?
    >H2O Developments
    >USS Endeavour
    >Hosted@Servage

  3. #3
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face Solution Half Worked

    Thanks for the post.

    Your suggestion half worked… let me explain and maybe you can help me figure out what I have done incorrectly.

    This code displays the Key value but I can not get the value for that key to display.

    Code:
    dim testKey
    testKey = activeMakesObj.MakeList.Keys
    
    Response.Write("key = " & makeArray(0) & "<br>" ) )
    Just to make sure I am understanding what is occurring here and I will try to include all of the code so that it makes sense…

    I create a variable called activeMakesObj in my code…

    Code:
    dim activeMakesObj
    I then instantiate this object with this code…

    Code:
    Set activeMakesObj = New cMaintMakeMultiple
    So at this point activeMakesObj contains the class cMaintMakeMultiple

    Here is that class…

    Code:
    <%
    Class cMaintMakeMultiple
    
    '---------------------------------------
    'Purpose: Private class member variables
    '---------------------------------------
       
       Private m_Success
       Private m_cMaintMake
    
    '------------------------------------------------------------------
    'Purpose: Initialize (automatically ran when class is instantiated) 
    '------------------------------------------------------------------
       Sub Class_Initialize() 
     
          Set m_cMaintMake = Server.CreateObject ("Scripting.Dictionary")
     
       End Sub 
     
    '-----------------
    'Purpose: Clean up 
    '----------------- 
       Sub Class_Terminate()
       
          Set m_cMaintMake = Nothing
       
       End Sub   
    
    '----------------------------------------   
    'Purpose: Get the success query indicator
    '----------------------------------------
       Public Property Get success()
          success = m_Success    
       End Property      
    
    '----------------------------------------   
    'Purpose: Set the success query indicator
    '----------------------------------------   
       Public Property Let success( p_Success )
          m_Success = p_Success
       End Property 
       
    '----------------------------------   
    'Purpose: Get the cMaintMake object
    '----------------------------------    
       Public Property Get MakeList()   
       
          Set MakeList = m_cMaintMake
       
       End Property
       
    '-------------------------------------------
    'Purpose: Selects all Active Make Categories
    '-------------------------------------------
       Public Function SelectAllMake()
       
          Dim strSQL 
          
          strSQL = "SELECT * " & _  
                   "FROM tblMake " & _
                   "ORDER BY MakeSortOrder;"
                   
          LoadData( strSQL )
          
       End Function
     
     '-------------------------------------------
    'Purpose: Selects all Active Make Categories
    '-------------------------------------------
       Public Function SelectAllActiveMake()
       
          Dim strSQL 
          
          strSQL = "SELECT * " & _  
                   "FROM tblMake " & _
                   "WHERE makeActive = 'Y' " & _
                   "ORDER BY MakeSortOrder;"
                   
          LoadData( strSQL )
          
       End Function
     
    '---------------------------------------------   
    'Purpose: Executes SQL and populates variables
    '---------------------------------------------   
       Private Function LoadData( p_strSQL )
       
          Dim ODB, rsArray, myMake, i 
       
       'Purpose: SQL record set is returned in an array.  These numbers indicate 
       '         where in the array the field is located
          const cMakeId = 0
          const cLastMntDate = 1
          const cMakeActive = 2
          const cMakeDesc = 3
          const cMakeServerFileName = 4
          const cUseMakeLogo = 5
          const cMakeSortOrder = 6
          
       'Purpose: Open Database
          Set ODB = New DB
       
       'Purpose: Submits the SQL query
          rsArray = ODB.getRS( p_strSQL )
       
       'Purpose: Populates the values returned from the SQL recordset
          If IsArray(rsArray) Then
             
          'Purpose: Loops thru the array results set and builds the object list
          '         UBound( ArrayName, 2) - returns the number of rows in the array
             For i = 0 To UBound( rsArray, 2 )
                Set myMake = New cMaintMake 
           
                myMake.success = "Y"
                myMake.mode = "Update"
                myMake.makeId = rsArray( cMakeId, i )
                myMake.lastMntDate = rsArray( cLastMntDate, i )
                myMake.active = rsArray( cMakeActive, i )
                myMake.makeDesc = Replace( rsArray( cMakeDesc, i ), "'", "^%^" )
                myMake.makeServerFileName = rsArray( cMakeServerFileName, i )
                myMake.useMakeLogo = rsArray( cUseMakeLogo, i )
                myMake.makeSortOrder = rsArray( cMakeSortOrder, i )
             
                m_cMaintMake.Add myMake.makeId, myMake
             Next
             
             Me.success = "Y"
          Else
             Me.success = "N" 
          End If
          
       'Purpose: Kill database object
          Set ODB = Nothing
       
       End Function
       
    '----------------------------------------   
    'Purpose: Updates the makeSortOrder field
    '----------------------------------------
       Public Function UpdateMakeSortOrder( makeId, newSortOrder )
    
          Dim strSQL
         
          strSQL = "UPDATE tblMake " & _
                   "SET MakeSortOrder = '" & newSortOrder & "', " & _
                       "LastMntDate = current_timestamp " & _
                   "WHERE MakeId = '" & makeId & "'"
                   
          ExecuteSQL( strSQL )    
       
       End Function
       
    '----------------------------------   
    'Purpose: Executes an SQL statement
    '----------------------------------
       Private Function ExecuteSQL( p_strSQL )
          
          Dim ODB, rsArray
          
       'Purpose: Open Database
          Set ODB = New DB  
          
       'Purpose: Submits the SQL query
          rsArray = ODB.execDB( p_strSQL )
          
       'Purpose: Kill database object
          Set ODB = Nothing
          
       End Function
       
    End Class
    %>
    I then populate the private class variables with this code…

    Code:
    Call activeMakesObj.SelectAllActiveMake()
    So now I have the activeMakeObj object, with its private variables populated. One of its private variables is m_cMaintMake which is a Scripting.Dictionary. I populate this Scripting Dictionary with a key (which is the key from the table) and a 2nd class (cMaintMake) as the value. cMaintMake code is display in my 1st post.

    So to get the keys of this scripting directory, I wrote the below code and it returned the key value for the 1st item in the Scritping Dictionary.

    Code:
    dim makeArray
    makeArray = activeMakesObj.MakeList.Keys
    
    Response.Write( "key = " & makeArray(0) & "<br>" )
    Now I want to get the value in the Scripting.Dictionary(remember, the value is a class - cMaintMake). So I thought I would code something like the below example but it does not work.

    Code:
    Response.Write( "value = " & activeMakesObj.MakeList.Item( makeArray(0) ) )
    I also tried this but it did not work either.

    Code:
    dim test1
    test1 = activeMakesObj.MakeList( makeArray(0) )
    
    Response.Write( "test = " & test1 )
    This gave me the error “Wrong number of arguments or invalid property assignment”. So that made me think that I needed a new Get Property in cMaintMakeMultiple class.

    Something like…

    Code:
    Public Property Get MakeList1( i )   
       
       Set MakeList = m_cMaintMake( i )
       
    End Property
    But this does not work and I get the same error… “Wrong number of arguments or invalid property assignment”.

    So basically after this really long post, I was hoping you could help me get the value for the key I just returned in the Scripting.Dictionary which is in the cMaintMakeMultiple class.

    Again, if I use this code, it loops through everything and does display the makeId but I would like to display this value without having to use a loop.

    Code:
    for each makeList in activeMakesObj.MakeList.Items
       Response.Write( makeList.makeId )
    next
    Thanks in advance for your time!!!

  4. #4
    SitePoint Zealot trekmp's Avatar
    Join Date
    Dec 2005
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried:

    Code:
    makeArray = activeMakesObj.MakeList.Keys
    Response.Write( "value = " & activeMakesObj.MakeList(makeArray(0)))
    Why do we exist? What is our purpose?
    >H2O Developments
    >USS Endeavour
    >Hosted@Servage

  5. #5
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the post but this does not work either.

    Here is the error I get.

    Wrong number of arguments or invalid property assignment:
    'MakeList'

    To me seeing the message "Wrong number of arguments" means it is looking for a method but could not find it since the arguments where not the same.

    Could this be indicating the MakeList() method in class cMaintMakeMultiple?

    I did add this 2nd method below in class cMaintMakeMultiple in an attempt to match the arguments. Here is the call...

    Code:
    Response.Write( "value = " & activeMakesObj.MakeList1( 1 ) )
    Here is the method...

    Code:
    Public Property Get MakeList1( i )   
       
        dim makeArray1
        makeArray1 = m_cMaintMake.Keys
    
        Response.write( "i = " & i )
        'Response.end
    
         Set MakeList = m_cMaintMake( makeArray1( 0 ) )
       
    End Property
    This does not work but I get a different error. Here is the error.

    Object doesn't support this property or method

    The error is pointing to line

    Code:
    Response.Write( "value = " & activeMakesObj.MakeList1( 1 ) )
    So this looks like I am getting into the method but but to be honest, I am not sure what is wrong.

    Any other thoughts you have would be appreciated.
    Thanks.

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your property name is MakeList1, and in that property you are setting MakeList to some value, hence the error.

  7. #7
    SitePoint Guru
    Join Date
    Nov 2005
    Location
    Midwest
    Posts
    777
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry... I made a mistake with this post in the fact that I do have the return value named MakeList1.

    Just in case, here is the code again.

    Code:
    Public Property Get MakeList1( i )   
       
       dim makeArray1
       makeArray1 = m_cMaintMake.Keys
          
       Response.write( "i = " & i )
       'Response.end
          
       Set MakeList1 = m_cMaintMake( makeArray1( 0 ) )
       
    End Property
    I still think the weird thing is the error line number gives me this line of code as the error.

    Error:
    Object doesn't support this property or method

    Line of Code:
    Response.Write( "value = " & activeMakesObj.MakeList1( 1 ) )


Bookmarks

Posting Permissions

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