Click here to Skip to main content
14,659,318 members
Rate this:
Please Sign up or sign in to 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

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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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
   
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...
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 2

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:
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
   
v2
Comments
Member 10579673 6-Jul-14 13:04pm
   
Thank You very much —SA
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100