Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys, I need your help. I am converting from java to c# and I have run into a roadblock.
I am looking for a c# equivalent of the java Enumeration class. I have gone through the C# Enumeration documentation and I wasn't able to find something that mirror the java version, especially when it came to too methods that I need and these are the Enumeration. hasMoreElements() and the Enumeration.nextElement().
Java
protected boolean populateResultSetDataToRRX(Hashtable objStructureHT,String strObjectNam ,String strMultiUserCheck)
{
  try
  {
      Enumeration objEnum = null;
      for(objEnum=objStructureHT.keys(); objEnum.hasMoreElements();)
      {
        String strFieldName = (String)objEnum.nextElement();
        String strBindFieldName=extractBindField(strFieldName).toUpperCase();
        String strDataType = (String)objStructureHT.get(strFieldName);
        String strFieldValue="";
        if(strDataType.equals("date"))
        {
	 if(objResultSet.getTimestamp(strBindFieldName)==null)
	   strFieldValue="";
	 else
	   strFieldValue = objResultSet.getTimestamp(strBindFieldName).toString();
        }else {
	   strFieldValue = objResultSet.getString(strBindFieldName);
        }
        if(!populateRRXField(strObjectName,strFieldName,strFieldValue,strDataType))
        {
	  return false;
        }
      }// end of for objEnum hasNoMoreElements
      if (strMultiUserCheck.equalsIgnoreCase("Token"))
      {
        objRRX.addField(strObjectName,"Token", objResultSet.getString("Token"));
      }
        objRRX.addField(strObjectName,"selected","N");
        objRRX.assignErrorStatus(strObjectName, strStatus, "SUCCESS");
	return true;
  } 
   catch(Exception e)
   {
      System.out.println("Exception in populateResultSetDataToRRX " + e.getMessage());
      return false;
   }
}// end of function populateResultSetDataToRRX


So using this code for some context, looking at the forloop, the .hasmoreElement() and looking at the first line after the forloop, the .nextElement();


Thanks for your help.
Posted
Updated 15-Oct-13 5:13am
v2
Comments
Bernhard Hiller 16-Oct-13 2:48am    
I do not know Java's specialties with for loops, maybe I am wrong. It looks to me like enumerating thru all the Keys of the HashTable, i.e. in C# I write:

foreach(string strFieldName in objStructureHT.Keys)

and do not care about Java's Enumeration class.

 
Share this answer
 
Comments
rudolph098 15-Oct-13 11:14am    
I am not sure it helps me, I added code to the original question for a bit of context. thank you.
Richard MacCutchan 15-Oct-13 11:39am    
The two methods you refer to are called on the objEnum object which is returned from the Hashtable.keys method, so you need to compare with the C# hash table to see how that matches up to what you are trying to do. Hashtable.GetEnumerator may help.
rudolph098 15-Oct-13 11:47am    
That's why I am stuck at the Enumeration objEnum, not the Hashtable.
Richard MacCutchan 15-Oct-13 11:53am    
Well you need to look at the method I gave you the link for to see how you can iterate through those values to do whatever your program requires of them. You could also check the other HashTable methods to see if they provide the data in a more usable form for your needs.
rudolph098 15-Oct-13 11:58am    
Ok thank you.
The real problem is that .NET enumerations do not enumerate, in contrast to Java and some other technologies. Even though Solution 1 provides a work-around, it is not comprehensive. Some problems starts when you use the same integer value for different enumeration members, another problem is ordering while traversing.

I have created a comprehensive workaround provided in my CodeProject article: Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^].

Please see. I also show some useful enumeration techniques which don't require my code.

—SA
 
Share this answer
 
Comments
rudolph098 15-Oct-13 12:43pm    
Thank you.
Sergey Alexandrovich Kryukov 15-Oct-13 13:08pm    
You are welcome. Will you accept the answer (or both answers) formally (green "Accept" button)?
—SA
Here's your code. Might have some mistakes as it is converted using a converter.
C#
using System;
using System.Collections;

protected internal virtual bool populateResultSetDataToRRX(Hashtable objStructureHT, string strObjectNam, string strMultiUserCheck)
{
  try
  {
	  Enumeration objEnum = null;
	  for(objEnum=objStructureHT.Keys; objEnum.hasMoreElements();)
	  {
		string strFieldName = (string)objEnum.nextElement();
		string strBindFieldName =extractBindField(strFieldName).ToUpper();
		string strDataType = (string)objStructureHT[strFieldName];
		string strFieldValue ="";
		if(strDataType.Equals("date"))
		{
	 if(objResultSet.getTimestamp(strBindFieldName)==null)
	   strFieldValue="";
	 else
	   strFieldValue = objResultSet.getTimestamp(strBindFieldName).ToString();
		}
		else
		{
	   strFieldValue = objResultSet.getString(strBindFieldName);
		}
		if(!populateRRXField(strObjectName,strFieldName,strFieldValue,strDataType))
		{
	  return false;
		}
	  } // end of for objEnum hasNoMoreElements
	  if (strMultiUserCheck.equalsIgnoreCase("Token"))
	  {
		objRRX.addField(strObjectName,"Token", objResultSet.getString("Token"));
	  }
		objRRX.addField(strObjectName,"selected","N");
		objRRX.assignErrorStatus(strObjectName, strStatus, "SUCCESS");
	return true;
  }
   catch(Exception e)
   {
	  Console.WriteLine("Exception in populateResultSetDataToRRX " + e.getMessage());
	  return false;
   }
} // end of function populateResultSetDataToRRX
 
Share this answer
 
Comments
rudolph098 16-Oct-13 8:57am    
Thanks for the attempt, but this is why I don't use converter because they don't know if the language that they are converting to has the methods or not.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900