Wrox Programmer Forums
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 18th, 2005, 09:48 AM
Authorized User
 
Join Date: Jan 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default Hashtable

How do I extend a Hashtable to have two object values? Or is there another object that would work?

 
Old June 19th, 2005, 09:06 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

How about creating a class that holds your two values. Then the HashTable can hold one value that is an instance of your two-value class.

If you are set on creating a two-value HashTable, then perhaps you could extend the standard HashTable, inclue a private field of a regular HashTable that holds your second values. Then you override the inherited HashTable's various methods and wire together the inherited instance and the interal instance to link to two values. Of course, this raises the question of "how do I specify which value I'm getting back?" You'll have to create additional methods with a parameter to indicate which of the two values to get.

Personally I think it would be much easier to just use the first suggestion:

((MyTwoValueClass)aHashTable("somekey")).Value1
((MyTwoValueClass)aHashTable("somekey")).Value2

-Peter
 
Old June 19th, 2005, 10:09 AM
Authorized User
 
Join Date: Jan 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The first one looks like the direction I need to go. After constructing the code below, I tried to construct what you described but haven't(obviously) fully understood ((MyTwoValueClass)aHashTable("somekey")).Value1. ((t2)t1(1)). doesn't display a list of my class one variables. Where am I going wrong?


  private void Form1_Load(object sender, System.EventArgs e){
    Hashtable t1 = new Hashtable();
    container t2 = new container();
    t2.Parsed(true);
    t2.Url("www.somewhere.com");
    t1.Add(1,t2);
  }
}

public class container{
  private string URL;
  private bool PARSED;
  public container(){}

  public string Url{
    get{
      return URL;
    }set{
      URL=value;
    }
  }
  public bool Parsed{
    get{
      return PARSED;
    }set{
      PARSED=value;
    }
  }
}


 
Old June 19th, 2005, 07:25 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You are on the right track. The problem I see is the way you are using the properties. Try this:

    Hashtable t1 = new Hashtable();
    container t2 = new container();
    t2.Parsed = true;
    t2.Url = "www.somewhere.com";
    t1.Add(1,t2);

The syntax I suggested is the C# casting syntax:

(<Class to convert to>)<variable to convert>;

What I described is the way you would get at the particular properties from an instance of your class as stored in a hash table (because the hash table stores everything as objects).

((container)HashTable[...]).Url;

-Peter
 
Old June 19th, 2005, 08:09 PM
Authorized User
 
Join Date: Jan 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks! I ended up with:

t1.Add(1,new container());
((container)t1[1]).Url="www.somewhere.com";
((container)t1[1]).Parsed=true;




 
Old June 20th, 2005, 08:07 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

An even better way (so that you don't have to have two lines that convert the same object off the hash table) would be to add a smarter constructor to your class, then call then when you create the object.

First add this constructor to your container class:

    public container(string url, bool parsed){
        this.URL = url;
        this.PARSED = parsed;
    }

Then you can create a new instance and add it to the hash table like this:

    t1.Add(1,new container("www.somewhere.com", true));

This will save you a lot of lines when you get into creating more instances.

-Peter
 
Old June 22nd, 2005, 11:02 PM
Authorized User
 
Join Date: Jan 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks! That cut down on some clutter.

 
Old July 3rd, 2005, 02:25 AM
Authorized User
 
Join Date: Jan 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How can I use ContainsValue to see if a value exists in this hashtable?






Similar Threads
Thread Thread Starter Forum Replies Last Post
Hashtable ajit Java Basics 1 July 11th, 2006 01:25 AM
File to Hashtable safin C# 1 February 14th, 2006 05:39 AM
Hashtable mahulda General .NET 2 August 2nd, 2004 07:49 AM
Hashtable in C# sachin-csharp .NET Framework 2.0 0 July 28th, 2004 01:45 AM
about hashtable csc820203 C# 1 July 11th, 2004 07:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.