Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,164 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,693 people online right now. Registration is fast and FREE... Join Now!




why the value is null in this case?

 
Reply to this topicStart new topic

why the value is null in this case?

lordelf2004
post 10 Oct, 2008 - 10:29 AM
Post #1


New D.I.C Head

*
Joined: 10 Oct, 2008
Posts: 19


I try to use C# to illustrate HashTable algorithm. My problem is: when I assign "hashtable[0] = p;" in method InsertNode-class LinkList, the properties of p is assigned to HashTable[0] correctly. But when I debug, I see the value of HashTable[0] is null in DisplayDictionary(){ PrintList(hashtable[0]); } method - class Dictionary inherit from LinkList class. and that's reason why I cannot display the value of HashTable[0] into screen.

Please tell me why the value of HashTable[0] is null when I call DisplayDictionary() method. Thank you in advance.
This is my code:

CODE
  
class Node
    {
        public string word;
        public string mean;
        public Node next;
        public Node()
        {
            next = null;
        }
        
        public Node GetNode(String word, String mean)
        {
            Node p;
            p = new Node();
            p.word = string.Copy(word);
            p.mean = string.Copy(mean);
            return p;
        }
        
        public int HF(string word)
        {
            char[] firstchar = word.ToUpper().ToCharArray(0, 1);
            int value = firstchar[0];
            return ((value - 65) % 26);
        }
    }
    class LinkList : Node
    {
        public Node[] hashtable = new Node[26];
        
        public LinkList() { }
        public void InsertNode(Node p)
        {
           // int i = HF(p.word);
           // int i = 0;
            p.next = hashtable[0];
            hashtable[0] = p;
        }
      
        public void PrintList(Node list)
        {
            Node temp = list;
            while (temp != null)
            {
                Console.WriteLine("word:{0}", temp.word);
                Console.WriteLine("mean:{0}", temp.mean);
                temp = temp.next;
            }
        }
    }
    class Dictionary : LinkList
    {
        public Dictionary() { }
        public void MakeDictionary()
        {
            string word, mean;
            word = "a";
            mean = "is a";
            //Node p = new Node();
            LinkList linklist = new LinkList();
            Node p = linklist.GetNode(word, mean);
            linklist.InsertNode(p);  // null
        }
        public void DisplayDictionary()
        {
           // for (int i = 0; i < 26; i++)
                PrintList(hashtable[0]);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary dict = new Dictionary();
            dict.MakeDictionary();
            dict.DisplayDictionary();
        }
    }
}


User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 10 Oct, 2008 - 10:53 AM
Post #2


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 501



Thanked 44 times

Dream Kudos: 25
My Contributions


Try
csharp
public void MakeDictionary()
{
string word, mean;
word = "a";
mean = "is a";
Node p = GetNode(word, mean);
base.InsertNode(p);
}
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 10 Oct, 2008 - 12:59 PM
Post #3


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


Please don't double post. I'm deleting your other thread.
User is offlineProfile CardPM

Go to the top of the page

lordelf2004
post 10 Oct, 2008 - 06:21 PM
Post #4


New D.I.C Head

*
Joined: 10 Oct, 2008
Posts: 19

Thanks JackOfAllTrades very much. but if I want to create object LinkList type to use its methods correctly (not using "base" anymore). Where can I declare it?
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 02:29PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month