Results 1 to 11 of 11

Thread: Collections or Hashtables

  1. #1

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437

    Collections or Hashtables

    From my DLL, I want to return some value in this format ...

    test("Error",0)
    test("Run",1)

    and some more ....
    which one should I use ... Collections or Hashtables ?

    Also, right now I am returning hashtable from my DLL to form, and at the form side, I have declared a hashtable to take this return value in, but its always showing it as nothing for some reason, it returns the value from the DLL but on form side it becomes nothing automatically ????

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Time to see some code.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    what code ? I just want to be able to return some keys and values associated with them ... I tried using array of System.Collections.DictionaryEntry, it worked. Just do not know which one is the preffered choice and why ?

  4. #4
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    The choice really depends on how you wish to access the data.

    If it is to be more of a lookup table, ie you will be interrogating the data mainly by it's "key" value, or id you kust want to look at individual records based on the key value then I would suggest the hashtable.

    If you mainly wish to retreive all the data every time with a for each...loop (for example) then the simple collection will be fine.

  5. #5

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    Finally, I used Hashtables, work fine.

  6. #6

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by Wokawidget
    What about a dataset?
    When should u use either a collection, hashtable or a dataset?

    Woof
    That's a little overboard. DataSets would be most suited when working with a database or an XML data source.

    I guess a hashtable is just like a collection, excepting that it is faster to search in.

  8. #8

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    Originally posted by Wokawidget
    What about a dataset?
    When should u use either a collection, hashtable or a dataset?

    Woof
    Yes, mendhak is right, dataset will be more like a overhead in this situation. Hashtables are just like collections, only they are better (I think) as far as speed is concerned.

  9. #9
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    So you would use these in the UI and not in the backend to pass data back to the UI???

    Eh?

    When running .NET and I come to a break point in my code, why can't I see the form1? As soon as it goes into the .NET IDE then the active form goes all white and I can't see anything on it.
    Is this correct? Bad .NET IDE *slap*

  10. #10

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    Originally posted by Wokawidget
    So you would use these in the UI and not in the backend to pass data back to the UI???

    Eh?

    When running .NET and I come to a break point in my code, why can't I see the form1? As soon as it goes into the .NET IDE then the active form goes all white and I can't see anything on it.
    Is this correct? Bad .NET IDE *slap*
    I have written a generic DLL component for Data Access with SQL Server based on Microsoft Standards and best practices. Now, there is this function called ExecuteStoredProcedure. This is a overloaded SP and can return either DataSet or DataTable for the results of a SP. But if SP has some output parameters too, then I need a way to return those Output paramters/values to UI too. They can not be part of DataSet or DataTable.

    So I did something like this ...

    ExecSP(ObjectToReturn as Object, blah blah) as Hashtable

    if typename(ObjectToReturn ) is Dataset then return dataset
    if typename(ObjectToReturn ) is Datatable then return Datatable

    for each output parameter and if there is an @RETURN_VALUE parameter from SP too, I built a hashtable, add all those things to hashtable with there name, add one more item called "Error" pass it 0 if there was successfully execution of SP otherwise pass 1. This way my UI can not only get the SP data but also output parameters (if there were any).

  11. #11

    Thread Starter
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    Here's one method code ..

    VB Code:
    1. Public Overloads Function ExecuteStoredProcedure(ByVal connection As SqlConnection, ByVal storedProcedureName As String, _
    2.                                                      ByRef returnObject As Object, ByVal sqlParameters() As SqlParameter, _
    3.                                                      Optional ByVal includeReturnValue As Boolean = True, _
    4.                                                      Optional ByVal mustCloseConnection As Boolean = False) As Hashtable
    5.         Dim i, j As Int16
    6.         Dim dataAdapter As New SqlDataAdapter
    7.         Dim returnValues As New Hashtable(New CaseInsensitiveHashCodeProvider, New CaseInsensitiveComparer)
    8.  
    9.         Try
    10.             OpenConnection(connection)
    11.  
    12.             Dim command As New SqlCommand(storedProcedureName, connection)
    13.             command.CommandType = CommandType.StoredProcedure
    14.  
    15.             If Not (sqlParameters Is Nothing) Then
    16.                 For i = 0 To CType(sqlParameters.Length - 1, Short)
    17.                     command.Parameters.Add(sqlParameters(i))
    18.                 Next
    19.             End If
    20.  
    21.             If includeReturnValue Then
    22.                 Dim returnValue As New SqlParameter("@RETURN_VALUE", SqlDbType.BigInt)
    23.                 returnValue.Direction = ParameterDirection.ReturnValue
    24.                 command.Parameters.Add(returnValue)
    25.             End If
    26.  
    27.             dataAdapter.SelectCommand = command
    28.  
    29.             If TypeName(returnObject).ToUpper = ("DataSet").ToUpper Then
    30.                 dataAdapter.Fill(CType(returnObject, DataSet))
    31.             ElseIf TypeName(returnObject).ToUpper = ("DataTable").ToUpper Then
    32.                 dataAdapter.Fill(CType(returnObject, DataTable))
    33.             Else
    34.                 command.ExecuteNonQuery()
    35.                 returnObject = Nothing
    36.             End If
    37.  
    38.             returnValues.Add("Error", 0)
    39.  
    40.             If includeReturnValue Then
    41.                 returnValues.Add("@RETURN_VALUE", command.Parameters("@RETURN_VALUE").Value)
    42.             End If
    43.  
    44.             Dim sqlParameter As SqlParameter
    45.  
    46.             For Each sqlParameter In command.Parameters
    47.                 If sqlParameter.Direction = ParameterDirection.InputOutput Or sqlParameter.Direction = ParameterDirection.Output Then
    48.                     returnValues.Add(sqlParameter.ParameterName.ToString, sqlParameter.Value)
    49.                 End If
    50.             Next
    51.  
    52.             Return (returnValues)
    53.  
    54.         Catch sqlex As SqlException
    55.  
    56.             returnValues.Add("Error", 1)
    57.             LogException(sqlex)
    58.  
    59.         Catch ex As Exception
    60.  
    61.             returnValues.Add("Error", 1)
    62.             LogException(ex)
    63.  
    64.         Finally
    65.             dataAdapter.Dispose()
    66.             If mustCloseConnection Then CloseConnection(connection)
    67.         End Try
    68.     End Function

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