473,385 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

"a field initializer cannot reference the nonstatic method xxx" error

TS
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();
}

thanks for the response.
Nov 16 '05 #1
4 10193
Tyr setting the variable in the constructor.

"TS" <ma*********@311.com> wrote in message
news:#M**************@TK2MSFTNGP10.phx.gbl...
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();
}

thanks for the response.

Nov 16 '05 #2
Thanks for Peter's quick response.

Hi TS,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you received a compiler error when
trying to assign the return value of a method to a field as its initial
value. If there is any misunderstanding, please feel free to let me know.

As far as I know, it is a language specification of C# that we cannot
assign initial value to a field with the return value of a method, a
property or a field. So if you wish to achieve this, please try to set the
value in the constructor of the class.

HTH. If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #3
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
Nov 16 '05 #4
TS
thanks all

"TS" <ma*********@311.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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();
}

thanks for the response.

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Olaf | last post by:
I have a frameset page witch contains the myFuc() function. The function is accessed from a page in one of the frames in the frameset. An example is shown below. <input...
2
by: SmittyBroham | last post by:
Hello, I have a function that loops through 2 select lists and records the values of any hi-lighted options a user would have selected. It then sets 2 corresponding "hidden" form elements to the...
2
by: Jozef | last post by:
Hello, I have a friend in the UK and another in Denmark that I sent an Access XP database to. I used the Package and Deployment wizard to create this file. The only reference that I added was...
16
by: Paul S. Natanson | last post by:
What is a Null Reference error and how do I fix it? My newly installed VB.Net2003 gives me a "Microsoft Development Environment" error message box EVERY time I try to run/start ANY project -...
1
by: Paul S. Natanson | last post by:
What is a Null Reference error and how do I fix it? My newly installed VB.Net2003 gives me a "Microsoft Development Environment" error message box EVERY time I try to run/start ANY project -...
2
by: Jeff | last post by:
I'm getting an Object Reference error before I even run my app, and I'm not sure where to look to find the cause. I'd appreciate your help. When I open my Windows Application project, the...
6
by: mihailsmilev | last post by:
Hello, let me first describe the situation: I am developing an application using Qt Designer 3.3.5 on OpenSuSE Linux for my mp3 player. So I need to get the id3 tags from the mp3 files, and I've...
3
by: shailendrapalsingh | last post by:
hi all i am trying to read excel file through my program. but when i compile i get "Application is ambiguous reference'. error. i am including using Excel;
4
by: peashoe | last post by:
I get the following error: ADODB.Field error '80020009' Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record. This is my code: ' Get...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.