stuck newbie: enumerator problem

M

MM

Hi,

I am very new to C# so please feel free to point out bad style/practice
in code below. Specifically however, I can't seem to return an
enumerator - I get a StackOverflowException and don't have any idea why?

Here's brief code:-

using System;
using System.Collections;

namespace Bent.Params
{
/// <summary>
/// This namespace contains the param dictionary and
/// the other neccessary info to generate the NC code file.
/// </summary>
public class BentParams
{
// Holds all the params
private Hashtable param_dict;

public BentParams()
{
// Init to defaults
// Note that most are bullshit, but all capitalised are required.
this.param_dict = new Hashtable();
this.param_dict.Add("W", ""); // Prog no in machine
this.param_dict.Add("WN", ""); // PART NAME
this.param_dict.Add("NP", 0); // Qty?
this.param_dict.Add("T", 0.0F); // THICKNESS
this.param_dict.Add("QN", 0); // MATERIAL ID
this.param_dict.Add("S", 0); // Tensile
this.param_dict.Add("SQ", 0); // Speed position
this.param_dict.Add("PN", 0); // PUNCH NO
this.param_dict.Add("PR", 0.0F);// Punch radius
// and about another 100 odd entries
}

public IDictionaryEnumerator GetEnumerator()
{
try
{
return this.GetEnumerator();
}
catch (Exception e)
{
Console.WriteLine(e);
return null;
}
}

}
}

When I call GetEnumerator it throws the StackOverflowException. Any
ideas? Also is the constructor okay? Basically I want to setup a
dictionary (which always has same keys) and apply a default set of
values which can be changed as needed. Thanks for the help. matthew.
 
M

MM

MM said:
public IDictionaryEnumerator GetEnumerator()
{
try
{
Should be return this.param_dict.GetEnumerator() otherwise I think I'm
trying to access an enumerator to the class which doesn't implement the
required interface - Right? Foiled by inexperience and hasty use of
'intellisense' I guess as this works as I want.
return this.GetEnumerator();
}
catch (Exception e)
{
Console.WriteLine(e);
return null;
}
}

}
}

When I call GetEnumerator it throws the StackOverflowException. Any
ideas? Also is the constructor okay? Basically I want to setup a
dictionary (which always has same keys) and apply a default set of
values which can be changed as needed. Thanks for the help. matthew.
Any other comments/hints about style/practice? Thanks matthew.
 
G

Guest

Your function GetEnumerator() is calling itself inside it's body, so it is an
endless loop which is causing the StackOverFlowException

ie

public void FunctionA()
{
this.FunctionA();
}

it will just crash your system.
 
M

MM

Mark said:
Your function GetEnumerator() is calling itself inside it's body, so it is an
endless loop which is causing the StackOverFlowException

ie

public void FunctionA()
{
this.FunctionA();
}

it will just crash your system.
Hi Mark, This makes perfect sense.

Given:

using System;
using System.Collections;

namespace Bent.Params
{
/// <summary>
/// This namespace contains the param dictionary and
/// the other neccessary info to generate the NC code file.
/// </summary>
public class BentParams
{
// Holds all the params
private Hashtable param_dict;

public BentParams()
{
// Init to defaults
// Note that most are not required, but all capitalised are
required.
this.param_dict = new Hashtable();
this.param_dict.Add("W", ""); // Prog no in machine
this.param_dict.Add("WN", ""); // PART NAME
this.param_dict.Add("NP", 0); // Qty?
this.param_dict.Add("T", 0.0F); // THICKNESS
this.param_dict.Add("QN", 0); // MATERIAL ID
this.param_dict.Add("S", 0); // Tensile
this.param_dict.Add("SQ", 0); // Speed position
this.param_dict.Add("PN", 0); // PUNCH NO
this.param_dict.Add("PR", 0.0F);// Punch radius
// and about another 100 odd entries
}

public IDictionaryEnumerator GetEnumerator()
{
try
{
return this.param_dict.GetEnumerator();
}
catch (Exception e)
{
Console.WriteLine(e);
return null;
}
}

}
}

Basically I want to setup a dictionary (which always has same keys) and
apply a default set of values which can be changed as needed. This is
in-house so no machine variations are needed. Is this the right way to
approach the problem? I'm an engineer (not software) so any guidance is
greatly appreciated. Thanks very much for the help. matthew.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top