Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IEnumerator probelm

Status
Not open for further replies.

JBravo

Programmer
Mar 21, 2002
12
IE
Hi All,
I'm having a problem with the following code. I get a compiler error
//C:\CSharpStuff\ProcessList\ProcessList\Class1.cs(64): Cannot implicitly convert type 'ProcessListNamespace.structProcessInfo' to 'ProcessListNamespace.ProcessList'
Does anyone know how to make the class properly emumerable?
Thanks,
J
Code:
using System;
using System.Collections;


namespace ProcessListNamespace
{
	public struct structProcessInfo
	{
		public string stringName;
		public string stringProcessID;
		public string stringParentProcessID;
		public string stringUserName;
	}



	class ProcessList 
	{
		private Hashtable ProcessListTable = new Hashtable();

		public void AddToList(structProcessInfo ProcessInfo)
		{
			ProcessListTable.Add(ProcessListTable.Count + 1, ProcessInfo);
		}

		public void AddToList(string Name, string ProcessID, string PProcessID, string UserName)
		{
			structProcessInfo temp;
			temp.stringName = Name;
			temp.stringParentProcessID = PProcessID;
			temp.stringUserName = UserName;
		}

		public void ClearList()
		{
			ProcessListTable.Clear();
		}

		public IEnumerator  GetEnumerator()
		{
			return ProcessListTable.GetEnumerator();
		}







		static void Main(string[] args)
		{
			structProcessInfo qwe;

			qwe.stringName = "Proces4561";
			qwe.stringParentProcessID = "1";
			qwe.stringProcessID = "345";
			qwe.stringUserName = "John";

			ProcessList mypl = new ProcessList();
			mypl.AddToList(qwe);

			foreach(DictionaryEntry myEntry in mypl)
			{
Compiler Error-------->	mypl = (structProcessInfo)myEntry.Value;
				Console.WriteLine("{0} \t {1}", myEntry.Key, qwe.stringName);
			}
		}
	}
}
 
The problem is that you can not assign a structProcessInfo type to a ProcessList type. To fix this you can change as follows:

structProcessInfo temp;

foreach(DictionaryEntry myEntry in mypl)
{
temp = (structProcessInfo) myEntry.Value;
Console.WriteLine("{0} \t {1}", myEntry.Key, temp.stringName);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top