Click here to Skip to main content
15,886,723 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am using VS2008.
I want to assign a null value to var datatype. How to do it? Here is my problem.
[Solution 2 explains why you cannot do it — SA]
C#
class cl
{
buildmenu()
{
if(condition=1)
var vv=some linq query                          -----A
else
if(condition=2)
var vv=some linq query                          -----B
}

...
...
...
foreach (var vx in vv)                           ----C
{

}}

Now if you see A and B, they are local and i cannot use it location C(since A and B were defined locally).
I also cant define var vv globally since it is not allowed.

What should i do ???? I want to assign a null value to var datatype so that whatever result comes out from A or B, could be used at C. [Solution 4 explains what to do — SA]

Quick help will be appreciated.

Regards.
Posted
Updated 5-Jan-12 5:12am
v6
Comments
Sergey Alexandrovich Kryukov 4-Jan-12 3:23am    
Why told you var is a data type? It is not a type.
--SA
Amir Mahfoozi 4-Jan-12 4:12am    
The foreach cannot be out of all functions so define a parameter for its function and pass vv to it. all linq results are implementing IEnumerable so you can define its parameter as IEnumerable and also add another parameter that says the type of vv items.

There is not such thing as var type. This is not a type at all. This is a keyword used for type inference, see http://en.wikipedia.org/wiki/Type_inference[^], http://msdn.microsoft.com/en-us/library/ms364047%28v=vs.80%29.aspx[^].


Using this keyword is strictly equivalent to explicit type name of the type which would be inferred by a compiler from the context if this is possible. As null is ambiguous (any nullable of reference type is assignment-compatible with null), inference is impossible, so var can not be used.

It is important to understand that using var does not mean going beyond strict typing. When a type is inferred, is treated like any other statically known type.



[EDIT] I've realized that you have a related problem to be solved anyway. You correctly explain it at the end of the text of the question. Please see Solution 4.

—SA
 
Share this answer
 
v3
Comments
Amir Mahfoozi 4-Jan-12 4:07am    
+5
Sergey Alexandrovich Kryukov 4-Jan-12 12:01pm    
Thank you, Amir.
--SA
Sergey Alexandrovich Kryukov 5-Jan-12 11:56am    
Unfortunately, I did not pay attention for the real problem OP has (see at the end of the question) and failed to suggest what to do. So, I added another answer to fix it, please see.
--SA
Espen Harlinn 7-May-12 6:06am    
Good reply :-D
Sergey Alexandrovich Kryukov 7-May-12 12:17pm    
Thank you, Espen.
--SA
shikhar gilhotra wrote:
I also can't define var vv globally since it is not allowed. What should I do?


Apparently, in my answer I've explained what you cannot do and why, see Solution 2. I did not give you and idea on what to do. I'll fix this problem now.

Let's consider the very first Microsoft sample explaining the use of LINQ,
http://msdn.microsoft.com/en-us/library/bb397906.aspx[^].

Here is that sample, written using var:
C#
static void Main(string[] args) {
    int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
    var numQuery =
        from num in numbers
        where (num % 2) == 0
        select num;
    foreach (int num in numQuery)
        Console.Write("{0,1} ", num);
} //Main


Now, you want to make an if branch with different queries. You correctly explain why you cannot do it with var. Let's be logical. It means that if you want to use the similar code logic and structure, you will need to exclude var by using explicit type. Here is how:
C#
static void Sample(bool condition) {
    int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
    // here is the declaration in question:
    // you don't need to assign null to numQuery
    // because you don't use unassigned value of it anyway, but could be
    // IEnumerable<int> numQuery = null;
    IEnumerable<int> numQuery;
    if (condition)
        numQuery =
            from num in numbers
            where (num % 2) == 0
            select num;
    else
        numQuery =
            from num in numbers
            where (num > 4)
            select num;
    foreach (int num in numQuery)
        Console.Write("{0,1} ", num);
} //Sample


It is important to understand, that the run-time type is different from the compile-time type declared in the code sample shown above. You can use the nearest common base type which is assigned-compatible with the operand of your foreach container.

The problem is reduced to the problem of selection of appropriate query type for the explicit declaration. The rules are very well explained here: http://msdn.microsoft.com/en-us/library/bb397924.aspx[^].

Good luck,
—SA
 
Share this answer
 
v8
You can use Nullable types in .NET. Ref: http://msdn.microsoft.com/en-us/library/2cf62fcy%28v=vs.80%29.aspx[^]
 
Share this answer
 
Comments
shikhar gilhotra 4-Jan-12 3:17am    
didnt get little help ;-(
Sergey Alexandrovich Kryukov 4-Jan-12 3:30am    
...because this answer is completely irrelevant. Please ignore it to avoid confusion.
--SA

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