Dzictionary i własna klasa jako klucz

0

hej wszystkim, mam mały problem i nie wiem czy i co robię źle... potrzebuję sobie zrobić Dictionary w którym kluczami będzie moja własna klasa, ale przy wyszukiwaniu wywala mi wyjątek "The given key was not present in the dictionary." i nie wiem jak to obejść
poniżej mały kodzik pokazujący mniej więcej co i jak robię (oczywiście to nie jest ten problem który próbuję rozwiązać :-) , ale tak było szybciej), z góry dzięki za pomoc

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static InfoTable tab;

        private class myKey
        {
            public int cos;
            public myKey(string _cos)
            {
                cos = Int16.Parse(_cos);
            }
        }

        private class InfoTable
        {
            private Dictionary<myKey, string> _tab;

            public InfoTable()
            {
                _tab = new Dictionary<myKey, string>();
            }
            public void write(myKey klucz, string opis)
            {
                _tab.Add(klucz, opis);
            }
            public void read(myKey klucz)
            {
                System.Console.WriteLine(_tab[klucz]); //<-!!!!! tu jest wyjątek
            }
        }

        static void Main(string[] args)
        {
            tab = new InfoTable();
            tab.write(new myKey("1"), "blebleble");
            tab.read(new myKey("1"));
        }
    }
}
0

Spróbuj zamiast linijki w której wywala wyjątek dać:
System.Console.WriteLine(_tab[klucz].myKey);

0

na coś takiego już sam kompilator nie pozwala :|, może jakieś inne propozycje??

0

Proponuje użyć generyczną klasę Dictionary<TKey, TValue> (jeżeli kolekcja będzie mała, Hashtable, jeżeli duża). Klasa używana jako TKey powinna implementować dwie metody: Equals i GetHashCode. Resztę doczytaj w googlu.

Pozdrawiam,
jard

0

Dzięki, tego właśnie nie wiedziałem :-)

jakby komuś się to jeszcze przydało:

        private class MyKey
        {
            public int cos;
            public MyKey(string _cos)
            {
                cos = Int16.Parse(_cos);
            }

            public override int GetHashCode()
            {
                return this.cos.GetHashCode();
            }

            public override bool equals(object obj)
            {
                mykey other = obj as mykey;
                if (other == null)
                    return false;
                return equals(other);
            }

            public bool Equals(MyKey other)
            {
                if (this.cos == other.cos) return (true);
                else return (false);
            }
        }
0

tzn tak, (tam się wielkość liter nie zgadzała):

        private class MyKey
        {
            public int cos;
            public MyKey(string _cos)
            {
                cos = Int16.Parse(_cos);
            }

            public override int GetHashCode()
            {
                return this.cos.GetHashCode();
            }

            public override bool Equals(object obj)
            {
                MyKey other = obj as MyKey;
                if (other == null)
                    return false;
                return Equals(other);
            }

            public bool Equals(MyKey other)
            {
                if (this.cos == other.cos) return (true);
                else return (false);
            }
        }

1 użytkowników online, w tym zalogowanych: 0, gości: 1