Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am making a windows form app.

there are two error while debug

Error 1 A field initializer cannot reference the non-static field, method, or property 'test.Form1.SplitInLine(string)'


Error 2 A field initializer cannot reference the non-static field, method, or property 'test.Form1.txtRem

here is the code

C#
string SplitInLine(string forSplit)
        {
            string[] strSplit = forSplit.Split(new char[] { '#' });
            string forReturn = "";
            for (int i = 0; i < strSplit.Length; i++)
                forReturn += (i + 1).ToString() + "." + strSplit[i] + "\n";
                return forReturn;
        }
        string remotersInLine = SplitInLine(txtRem.Text);
Posted
Updated 6-Jul-14 0:55am
v2

From C# specification:
Quote:
A variable initializer for an instance field cannot reference the instance being created.
It means you can't initialize one instance member* using an other instance member, as it does not available at that time...

*property or method
 
Share this answer
 
Comments
Member 10579673 6-Jul-14 7:06am    
whats wrong with my code?
Kornfeld Eliyahu Peter 6-Jul-14 7:25am    
remotersInLine is an instance member and can not be initialized using an other instance member, in your case SplitInLine. Try moving the initialization to the constructor...
Sergey Alexandrovich Kryukov 6-Jul-14 12:55pm    
Peter,

I am pretty much sure you understand how it all works correctly, but your explanation is really, really unclear.
What does it mean "not available at that time"? If something is defined, it is "available". If you said "initialized", it would be clear. That's why OP probably have no idea what you are talking about. And you did not have any idea how to do such things correctly.

I even started with down-voting your answer, but, trying to write about the motivation of my down-vote realize that it would be ridiculous, as in the comment you correctly explained what to do. It's just the post itself is extremely unclear, so I'll up-vote it.

I tried to explain it more clearly, please see.

—SA
The message is quite clear: you just cannot do it. This feature generally prevents you from initialization of the field with something which might not be yet ready. In your case, SplitInLine is the instance method. It means that it has access to the instance through the (implicit) method parameter "this". Through "this" reference, you can access all other instant members. But how would you know if some other instance you use in this method is already initialized? This is not always possible. Imaging the case when two fields tried to use each other.

So, instead, you should do something like this:
C#
class MyClass {

    internal MyClass() {
        //...
        remotersInLine = SplitInLine(txtRem.Text);
    }

    string SplitInLine(string forSplit) {/* ... */}

    string remotersInLine; // should not be initialized before constructon

    //...

}


Can you see the point now?

—SA
 
Share this answer
 
v2
Comments
Member 10579673 6-Jul-14 13:04pm    
Thank You very much —SA
Sergey Alexandrovich Kryukov 6-Jul-14 13:16pm    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900