Franva
Franva

Reputation: 7067

Odd error: local variables cannot be used before being defined. But I did defined it

public partial class introduction : System.Web.UI.Page
    {
        static BusinessService bs = null;
        private static List<PCUser> coaches = new List<PCUser>();
.............

 public void BindRepeater()
        {
            if (coaches == null || coaches.Count == 0)
            { 

            }

The compiler complains of the coaches in the BindRepeater.

Error message: Local variables cannot be used before being defined.

But if I use the coaches like this :

introduction.coaches 

Then the error will go.

Interestingly, I can use bs directly without referring the introduction.

Can anyone tell me the reason for this error?

For me, I think bs and coaches are at the same access level, thus both of them should be able to be accessed directly by referring their names.

PS: Another question, will the content of the variable coaches still exist after the whole web page is refreshed? Coz I have to use it to do the pagination.

Upvotes: 2

Views: 170

Answers (1)

YoryeNathan
YoryeNathan

Reputation: 14502

Maybe there is another variable named coaches, which took priority in your scope.

I suggest using different names, and having ReSharper to warn you about name collisions.

Upvotes: 1

Related Questions