Question

I have a class like this

    public class foo
    {

        private void getThread()
        {
         var AllThreads = from sc in db.ScreenCycles
                          join s in db.Screens on sc.ScreenID equals s.ScreenID
                          select s;
        }
    }

I want to make the AllThreads variable a class variable instead of a method variable. Like this...

public class foo
{
    var AllThreads;
    private void getThread()
    {
       AllThreads = from sc in db.ScreenCycles
                          join s in db.Screens on sc.ScreenID equals s.ScreenID
                          select s;
    }
}

How ever it wont let me declare a class variable of type var.

How to I achieve this?

Was it helpful?

Solution

To preserve your original code, try this

public class foo

    IEnumerable<ScreenCycles> AllThreads;
    private void getThread()
    {
       AllThreads = from sc in db.ScreenCycles
                          join s in db.Screens on sc.ScreenID equals s.ScreenID
                          select s;
    }

}

OTHER TIPS

You could do it like this:

public class foo {    
  IEnumerable<string> AllThreads;    

  private void getThread() {
     AllThreads = (from sc in db.ScreenCycles
                      join s in db.Screens on sc.ScreenID equals s.ScreenID
                      select s.Screen1 + " " + sc.Thread);
  }
}

Updated per Joel's suggestion.

var can only be used as a local declaration. If you want to use the type returned from a LINQ expression you must have build an object.

The initial var is defining an anonymous class. In order to do what you what you have to define the class.

But...you could probably just do this:

List<string> AllThreads;

You can't use var at the class level. You need to provide an explicit type if it's not going to be initialized right away.

Assuming "s" is of a type name "Screen":

public class foo
{
    IEnumerable<Screen> AllThreads;
    private void getThread()
    {
       AllThreads = from sc in db.ScreenCycles
                          join s in db.Screens on sc.ScreenID equals s.ScreenID
                          select s;
    }
}

As many of said, you can't use var. Refactor your code to use Linq types.

public class foo 
{
    ScreenCycle[] allThreads;
    private void getThread() 
    { 
        allThreads = (from sc in db.ScreenCycles join s in db.Screens on sc.ScreenID 
                     equals s.ScreenID select s).ToArray(); 
    } 
}

OK, this isn't an answer, just a relative newbie to stackoverflow question, which this thread illustrates quite well.

The code markup tools on this site are seriously... um, unique. What I dont understand in this particular case is, why are all our comment codeblocks very very long? Is it something the OP did? I tired various edits on my earlier example, and for some reason the textbox my code was in was much longer than its contents. ( IE 8 )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top