Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. HashTable Beginning Question

HashTable Beginning Question

Scheduled Pinned Locked Moved C#
questiondata-structurescryptographylounge
3 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    budidharma
    wrote on last edited by
    #1

    This is my first time working with hashtables. Here's what I'm trying to do, and what I need to do. First, I have a HashTable that's initialized with the capacity of 169 (the number of possible pocket hands in holdem poker). I deal two random cards to a player - and then create a string that looks like this, from the cards: AKo, AKs, KKo, 32s ... etc. It'll always be in that format. Now, I have a hashtable called HandCounter. What I want to do, is check if that string exists as a "key" in the hash table. If it does, then get the value from that key and increment the value that is stored by 1. If it doesn't, then create a new key/value pair with the pocket string as the key and the int value 1. Here's what I thought the code would probably look like: if(HandCounter.ContainsKey(Hand.ToPocketString())) { HandCounter[Hand.ToPocketString()]++; } else { HandCounter.Add(Hand.ToPocketString(), 1); } Though something is wrong here. I'm attempting to use a string as the key, but it says it requires a type of "object" for the key. I'm attempting to use the value as an integer, but it says it uses type "object" as the value as well. Do I need to typecast these?

    B 1 Reply Last reply
    0
    • B budidharma

      This is my first time working with hashtables. Here's what I'm trying to do, and what I need to do. First, I have a HashTable that's initialized with the capacity of 169 (the number of possible pocket hands in holdem poker). I deal two random cards to a player - and then create a string that looks like this, from the cards: AKo, AKs, KKo, 32s ... etc. It'll always be in that format. Now, I have a hashtable called HandCounter. What I want to do, is check if that string exists as a "key" in the hash table. If it does, then get the value from that key and increment the value that is stored by 1. If it doesn't, then create a new key/value pair with the pocket string as the key and the int value 1. Here's what I thought the code would probably look like: if(HandCounter.ContainsKey(Hand.ToPocketString())) { HandCounter[Hand.ToPocketString()]++; } else { HandCounter.Add(Hand.ToPocketString(), 1); } Though something is wrong here. I'm attempting to use a string as the key, but it says it requires a type of "object" for the key. I'm attempting to use the value as an integer, but it says it uses type "object" as the value as well. Do I need to typecast these?

      B Offline
      B Offline
      budidharma
      wrote on last edited by
      #2

      I just got this to work. How do I enumerate through it, displaying the Key Value pairs?

      B 1 Reply Last reply
      0
      • B budidharma

        I just got this to work. How do I enumerate through it, displaying the Key Value pairs?

        B Offline
        B Offline
        budidharma
        wrote on last edited by
        #3

        Alright, i just figured that out as well: foreach(string Key in HandCounter.keys) Console.WriteLine(Key + ": " + HandCounter[Key].ToString()); Very simple, jeez.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups