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?
-
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.
Joel Coehoorn : Close, but it's best to avoid the .ToList().Joel Coehoorn : Also, I don't think he's returning Strings.Jimmie R. Houts : ah, he changed the question... otherwise I would have done the same as @The Lame Duck -
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;
-
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.
-
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; } }
Joel Coehoorn : Looks like he's selecting "Screens" rather than "ScreenCycles"Serapth : Is it worthwhile pointing out that the original class design is flawed, and you generally shouldnt return IEnumerable? Granted, exactly what you answered is valid to the question asked, but in the end, the advice is fundamentally flawed ( as a direct result to the question ).Joel Mueller : I couldn't disagree more. Generally, you SHOULD return IEnumerable, unless you have a good reason to return a less-generic type. However, storing an IEnumerable that depends on a live database connection without first calling ToArray or ToList on it is probably not such a good idea. -
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 )
-
http://blogs.msdn.com/ericlippert/archive/2009/01/26/why-no-var-on-fields.aspx
0 comments:
Post a Comment