Click here to Skip to main content
15,886,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to initilize var varible in c#. in that variable i stored list of values.

What I have tried:

I tried in google but getting error while assigning value.

[Edit] Code added from comment
C#
var results;
if (true)
   results = (from a in table_name select a).ToList();
else
   results = (from b in table_name select b).ToList();

[/Edit]
Posted
Updated 12-Apr-16 6:31am
v2
Comments
phil.o 12-Apr-16 7:02am    
Please show us your actual code. Basically, you initialize a "var" variable exactly the same way as another variable.
Member 12172426 12-Apr-16 7:17am    
I initialized like below
var results;
if (true)
results = (from a in table_name select a).ToList();
else
results = (from b in table_name select b).ToList();
phil.o 12-Apr-16 7:21am    
Please consider two things:
- don't put code in comments; the "What I have tried" section is here exactly for this purpose.
- when you are talking about an error, be sure to always describe this error, along with an Exception name/error message/etc...
I'll put the code back in your question for you. Next time please be careful to put all the relevant details in your question.
Please push the "Improve question" button and state the exact error you get.
Suvabrata Roy 12-Apr-16 7:05am    
put the code and error message...
Member 12172426 12-Apr-16 7:17am    
I initialized like below
var results;
if (true)
results = (from a in table_name select a).ToList();
else
results = (from b in table_name select b).ToList();

You can't do it like that: the declaration of the var variable has to include the information that the system requires to recognise the datatype. var ion C is not a dynamic declaration, it's a shortcut declaration to a fixed type which allows for variables referencing anonymous types. Your code could allow the variable type to be dynamically declared as "type of a" or "type of b" according to the value of the condition at run time.
C# won't allow that: the type of a var variable must be known at compile time.
 
Share this answer
 
If you are going to use var then you must assign a value to the variable in the declaration.
E.g.
C#
var o = new myObject();

If you do not do that then you must use the explicit declaration
C#
myObject o;
...
o = GetmyObject();
 
Share this answer
 
As other mentionned, when car is used, its type must be known at the point of declaration.

The easiest way to fix you problem assuming that both lists returns same object type would be to write:
C#
var results = condition ?
   (from a in table_name select a).ToList() :
   (from b in table_name select b).ToList();


Otherwise, if you want code similar to yours, then declare the variable type:
C#
List<TableRowType> results;
 
Share this answer
 

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