Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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 13th, 2007, 12:10 PM
Registered User
 
Join Date: Apr 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to stokerfed Send a message via Yahoo to stokerfed
Default Confused about Hashtable and ForEach..

I declared a hashtable of IssueType objects. An IssueType object is initialized with a string NAME and a integer ID. The function "makeXmlSenseOfData()" is supposed to access all the elements in the hashtable, and call a set function within the object, but I'm getting a runtime error...
Quote:
quote:
Message: Specified cast is not valid.
at Project.xmlHandler.makeXmlSenseOfData():line 94
-Which is pointing to the line "foreach(IssueType issue in _issueTypes)"
Code:
_issueTypes.Add("Bug", new Project.IssueType("Bug",1));
            _issueTypes.Add("New Feature", new Project.IssueType("New Feature",2)); 
            _issueTypes.Add("Task", new Project.IssueType("Task",3));


public void makeXmlSenseOfData()
        {
            System.Xml.XmlNodeList issueTypeList = null;
            foreach(IssueType issue in _issueTypes)
            {
                issueTypeList = xmlData.SelectNodes("//type[@id=" + issue.getTypeID() + "]");
                if(issueTypeList.Count != 0)
                {
                    issue.setCount(issueTypeList.Count);
                    issue.isInProject();
                }
            }
        }

Any ideas??
 
Old June 13th, 2007, 12:50 PM
Authorized User
 
Join Date: Feb 2007
Posts: 37
Thanks: 1
Thanked 0 Times in 0 Posts
Default

try
 foreach(string Issue in _issueTypes.keys)

 
Old June 13th, 2007, 12:55 PM
Registered User
 
Join Date: Apr 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to stokerfed Send a message via Yahoo to stokerfed
Default

Thanks for the reply...

That won't work because I need to use the "setCount()" and "isInProject()" functions of the IssueType class in lines:

Code:
issue.setCount(issueTypeList.Count);
issue.isInProject();
Is I change it to string, I get a error because those functions are not defined there..
 
Old June 13th, 2007, 01:07 PM
Registered User
 
Join Date: Apr 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to stokerfed Send a message via Yahoo to stokerfed
Default

Pretty much, I have a hashtable of objects, and I don't know if those objects are still there when I try to reference the functions in them...

Code:
_issueTypes.Add("Bug", new Project.IssueType("Bug",1));
_issueTypes.Add("New Feature", new Project.IssueType("New Feature",2)); _issueTypes.Add("Task",new Project.IssueType("Task",3));
Are these objects still there later on in the program??

I'm using Visual Studio, so when you hit the "." button, all the functions should appear from a selection dropdown.. I type this:
Code:
_issueTypes["Bug"].
and it only shows "Equals" "GetHashCode" "GetType" "ToString" when I'm expecting to see all of the functions from my IssueType class...
 
Old June 13th, 2007, 02:53 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The type exposed by the HashTable indexer is System.Object. "Equals", "GetHashCode", "GetType" & "ToString" are members of System.Object. You need to cast each value within the hash table collection to your type in order to access that type's members.

((Project.IssueType)_issueTypes["Bug"]).<IssueType member>


If you are in .NET 2.0, I'd recommend avoiding the hash table. Instead you could use the generic Dictionary:

      System.Collections.Generic.Dictionary<string, Project.IssueType> _issueTypes;

All the methods involving keys and values of the dictionary will be typed to string and Project.IssueType respectively. When you iterate thru the list, you will be working with the explicit type defined for the dictionary. The indexer will return Project.IssueType so you can directly access the type's members.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Hashtable ajit Java Basics 1 July 11th, 2006 01:25 AM
Hashtable Scott Rider General .NET 7 July 3rd, 2005 02:25 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.