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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting properties in a contructor 3

Status
Not open for further replies.

intrex

IS-IT--Management
Apr 14, 2002
70
US
I am getting an error message that is throwing me for a loop. Basiclly I am setting the value of a bool property in the constructor of a class (the same class that the property is in). As soon as I try to set the value I get
Exception of type System.StackOverflowException was thrown.

The constructor is overloaded. Below are the overloads.

#region overloaded constructors

public GetLabelParams(string StoredProcedure, string connections)
{
spname = StoredProcedure;

HasError = false;

//set the datatable to null because no parameters are passed with this
//constructor
dtParamTypes = new DataTable();

//set connnection name to enum sent from calling code
connectionname = connections;

//connect to the database and stored procedure specified.
try
{
connection = ConfigurationSettings.AppSettings[connections.ToString()];
}
catch(Exception ex)
{
error = error + "The " + connectionname + " information was not found in the web.config file <br> Server error <br>" +
ex.Message;
HasError = true;
}

}

//overloaded constructor for receiving a Datatable of parameters
//and types for each parameter. The Datatable has 4 columns.
//parameter names, parameter values, parameter type, and value
public GetLabelParams(string StoredProcedure, string connections, DataTable ParamsAndTypes)
{
spname = StoredProcedure;

connectionname = connections;

//set the has errors property to false on class instantiation
HasError = false;

//connect to the database and stored procedure specified.
try
{
connection = ConfigurationSettings.AppSettings[connections.ToString()];
}
catch(Exception ex)
{
error = error + "The " + connectionname + " information was not found in the web.config file <br> Server error <br>" +
ex.Message;
HasError = true;
}

dtParamTypes = new DataTable();
dtParamTypes = ParamsAndTypes;

}

#endregion

As soon as I set the HasError property to false the error is thrown. Here are my properties.

#region error properties

/// <summary>
/// This is the error property called by the main code.
/// </summary>

public bool HasError
{
get
{
return HasError;
}
set
{
HasError = value;
}
}

public string errors
{
get
{
return error;
}

}

#endregion

This seems so straight forward I am not sure what the problem is. I have tried to delete the dlls and recompile. I have tried renaming the property. I have tried refrencing the property by this.HasError. Any suggestions would be great.
 
Public get/set accessor methods are used to set private variables.

You have public bool HasError getting and setting itself, which is a problem, I think.

You need to declare a private variable, call it _HasError or hasError, or whatever naming convention you use to distinguish private from public. The public method would set the private variable:

Code:
private bool hasError;
public bool HasError
{
   get
   {
     return hasError;
   }
   set 
   {
      hasError = value;
   }
}



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
The solution is already posted but I would like to add some explanations.
Code:
private bool HasError;
public bool HasError // Compile error because HasError is already defined
{
	get{return HasError;}
	set{HasError=value;}
}
The above code does not compile.
Code:
public bool HasError 
{
	get{return HasError;}
	set{HasError=value;}
}


HasError = true; // line somewhere in code
The above code will produce a StackOverflow because of the infinite recursion.
When 'line somewhere' get executed,then the statement

Code:
set{HasError=value;}
get executed. Evaluating {HasError = value;} calls again {HasError=value} in order assign value to HasError and so on.
This happend because the only way to change HasError is by executing {HasError=value;}.
Calling the function inside of the same function is a recursion. That is not an error but there are techniques to accomplish that but not this set/get method.
The next example is interesting one.
Code:
using System;
using System.Data;
public class Form1 : System.Windows.Forms.Form
{
	
	private DataSet DataSet(string sConnection)
		{
	
			DataSet ds = new DataSet();
			ds.Tables.Add(new DataTable("abcd"));
			return ds;
		}
	private void button1_Click(object sender, System.EventArgs e)
		{
		     
			DataSet ds1 = DataSet("abcd");
		}
}
All above code is working well.
The first method's name is DataSet (!!!) , takes a string parameter and returns a DataSet object.
Nothing is wrong but it is not recommended.
There is no recursion here because DataSet ds = new DataSet(); is in fact:
System.Data.DataSet ds = new System.Data.DataSet();
But, in the button1_Click the DataSet() method is called (the object here is 'this' e.g. Form1 object) :
Code:
DataSet ds1 = DataSet("abcd"); // e.g. DataSet ds1 = this.DataSet("abcd");

-obislavu-
 
To add to obislavu's and tgreer's excellent posts:

When creating private member variables, there's no official standard for naming them, but there are a couple of conventions:
Code:
   // Use leading underscores
   private bool _HasError;

   // Use leading leading "m_"
   private bool m_HasError;

   // Use camel-casing for private member and
   // pascal-casing for property
   private bool hasError;
   public bool HasError
   {
      get { return hasError; }
      set { hasError = value; }
   }

Which one do I like? I like the leading underscore style, but the "m_" is pretty good too. I don't like separating them by only the case of the leading letter -- too easy to have your finger slip off the shift key and get recursion.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks for all of the feedback. I will be trying all of this in my code monday morning. I have seen lots of the leading underscore commenting and never really understood what the convention was. The recursion issues makes complete sense.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top