?
Solved

A field initializer cannot reference the nonstatic field, method, or property

Posted on 2006-05-06
6
Medium Priority
?
2,898 Views
Last Modified: 2008-01-09
Can someone please help with this error.  Below is the Class Member Declaration and Class Method.  

Error:
A field initializer cannot reference the nonstatic field, method, or property 'App.uccPictureViewer.OpenPictureImage()'

Class Member Declaration:
private ThreadStart _openThreadStart = new ThreadStart(OpenPictureImage);

Class Method
 private void OpenPictureImage()
 {
     ...
 }


If I change class method to static I receive 15 additional errors.

The VB Code Converted from is as follows:
Private _openThread As System.Threading.Thread
Private _openThreadStart As New ThreadStart(AddressOf OpenPictureImage)

    Private Sub OpenPictureImage()
       ....
    End Sub
0
Comment
Question by:wenyonk
  • 3
  • 2
6 Comments
 
LVL 13

Expert Comment

by:devsolns
ID: 16623310
Yeah static methods cannot access instance data.  You would need to mark your member object static as well.

private static ThreadStart _openThreadStart = new ThreadStart(OpenPictureImage);

I will tell you I would not make this static.  Anyone you have static methods accessing static member data you need to implement thread synchronization.  When multiple threads call the same static method it pushes different copies onto the stack so everything is fine.  things change when that static method interacts with static members because multiple threads can access that same member.  yeah its no different with instance methods and members but your more likely to run into issues because its shared throughout your app.

i have seen many nasty bugs when folks use static classes/members in asp.net applications and don't realize that each worker thread processing the request is accessing the same piece of data!
0
 
LVL 15

Accepted Solution

by:
Thogek earned 375 total points
ID: 16623677
Have you tried soemthing like:

Class Member Declaration:
    private ThreadStart _openThreadStart;

Within the Class Contructor Method:
        ThreadStart _openThreadStart = new ThreadStart(OpenPictureImage);

Class Method
    private void OpenPictureImage()
    {
        ...
    }
0
 
LVL 15

Expert Comment

by:Thogek
ID: 16623684
The MSDN documentation's short note explaining the compiler message you're currently getting: http://msdn.microsoft.com/library/en-us/cscomp/html/vcerrCompilerErrorCS0236.asp
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:wenyonk
ID: 16624917
Would it be something like this (without the type in the constructor)?

Class Member Declaration:
    private ThreadStart _openThreadStart;

Within the Class Contructor Method:
        _openThreadStart = new ThreadStart(OpenPictureImage);

Class Method
    private void OpenPictureImage()
    {
        ...
    }
0
 
LVL 15

Expert Comment

by:Thogek
ID: 16626611
That looks right, offhand.  (Yeah, if your _openThreadStart variable's a class-level field, you don't want to redeclare it in the constructor. :-)

See if something like that works for ya.
0
 
LVL 13

Assisted Solution

by:devsolns
devsolns earned 375 total points
ID: 16627263
Sorry misread you a little, this works.

class A{

     ThreadStart _st;

     public A() {
          _st = new ThreadStart(Foo);
     }

     private void Foo(){
          //do stuff  
     }
}
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question