TS <ma*********@311.com> wrote:
I am trying to create a page template. I am trying to set the value of a
field to the return value from a method. What do I have to do to get m_form
set to the value returned from a method? (the compiler won't let me do
this:)
// Form in the page
public HtmlForm m_form = FindFormTag();
public HtmlForm FindFormTag()
{
foreach(Control c in this.Controls)
{if(c.GetType() == typeof(HtmlForm))
{
return (HtmlForm) c;
}
}
return new HtmlForm();
}
The reason it won't let you do this is because when the initializer
takes place, your object is only partially constructed. In particular,
the Controls collection may well not have been initialized at all. I
suggest you set m_form from the constructor.
I also suggest you use
if (c is HtmlForm)
unless you're *deliberately* preventing it from finding instances of
classes derived from HtmlForm.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too