locked
How do I replace an existing object in a hashtable with a new object? RRS feed

  • Question

  • User-1715607868 posted

    I have a group of objects I set values in and then place in a hashtable with the keys stored in an array.  When something happens and I want to replace the original object stored in the hashtable with a newer version I have no idea what to do. For example:

    arContractsMissingData(arContractsMissingData.Length - 1) = ActiveContract.Item("caContractID").ToString
    htMissingData.Add(arContractsMissingData(arContractsMissingData.Length - 1), objMissingData)

    This adds the original object to the hashtable but after some conditional check a new object with the same fields but different values is created and needs to replace the original object.  I tried setting the values from the new object to the old object like this:

    htMissingData.Item(ActiveContract.Item("caContractID").ToString).ContractNoticeInitialDate = objMissingData.ContractNoticeInitialDate

    This generates an error that option strict on disallows late binding.

    Am I over thinking this?  Can I just remove the old object and add the replacement object?

    Wednesday, July 14, 2010 3:33 PM

Answers

  • User-158764254 posted

    if objMissingData is a complete replacement for the object already in the hashtable, then you could do it like this

    htMissingData.Item(ActiveContract.Item("caContractID").ToString) = objMissingData

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 14, 2010 3:50 PM
  • User-952121411 posted

    I am assuming you are locked into using HashTables with keys, but I strongly recommend looking at using generic object collections to do exactly what you are doing now, but significantly easier.  Everything in the object collection is strongly typed, and by making List (Of MyClass) you are exposing an entire set of methods like 'Insert', 'Add', and 'RemoveAt'.  With RemoveAt, you could easily remove unwanted objects and add them back in.  You can also use LINQ to query the collection and remove or find objects meeting your criteria (i.e. by "caContractID").  I believe you can leverage LINQ to query your current hastable too.

    Take a look at the following to get an idea about what I am talking about for future reference when working with object collections:

    .NET Object Collections Using Generics 101: 

    http://allen-conway-dotnet.blogspot.com/2009/11/net-object-collections-using-generics.html

    List(Of T) Class:

    http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

    LINQ to Objects:

    http://msdn.microsoft.com/en-us/library/bb397919.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 15, 2010 8:31 AM

All replies

  • User-158764254 posted

    if objMissingData is a complete replacement for the object already in the hashtable, then you could do it like this

    htMissingData.Item(ActiveContract.Item("caContractID").ToString) = objMissingData

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 14, 2010 3:50 PM
  • User-952121411 posted

    I am assuming you are locked into using HashTables with keys, but I strongly recommend looking at using generic object collections to do exactly what you are doing now, but significantly easier.  Everything in the object collection is strongly typed, and by making List (Of MyClass) you are exposing an entire set of methods like 'Insert', 'Add', and 'RemoveAt'.  With RemoveAt, you could easily remove unwanted objects and add them back in.  You can also use LINQ to query the collection and remove or find objects meeting your criteria (i.e. by "caContractID").  I believe you can leverage LINQ to query your current hastable too.

    Take a look at the following to get an idea about what I am talking about for future reference when working with object collections:

    .NET Object Collections Using Generics 101: 

    http://allen-conway-dotnet.blogspot.com/2009/11/net-object-collections-using-generics.html

    List(Of T) Class:

    http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

    LINQ to Objects:

    http://msdn.microsoft.com/en-us/library/bb397919.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 15, 2010 8:31 AM