2

I am getting the Error "The type or namespace name 'var' could not be found are you missing a using directive or an assembly reference?)" And yes, I did make sure it's all lowercase. Also, I am using .Net framework 4.5.1 I know var didn't work before 3.0. Below is part of my code that has no errors or trouble compiling if i take out the var statement.

using GalaSoft.MvvmLight;
using System;
using VKCatalyst.Model;

namespace VKCatalyst.ViewModel
{

    public class MainViewModel : ViewModelBase
    {
        private readonly IDataService _dataService;

        var i = 10; 
    }
}

While I see that this has been addressed in the past (I thought it might) I thought it might be a good idea to leave up as many new programmers like myself, may not realize that the reason they 'var' isn't being found is that it is not in a method.

Jarod Kientz
  • 131
  • 1
  • 9
  • What are you trying to do with `var i = 10;`? – Claudio Redi Apr 23 '14 at 19:54
  • @Selman22 I didn't know class fields couldn't be implicitly typed. I'm a new programer who has never not had var as an option. – Jarod Kientz Apr 23 '14 at 19:56
  • @JarodKientz You shouldn't really be using var unless you really have to. – tnw Apr 23 '14 at 19:57
  • @ClaudioRedi I just use `var i = 10;` as an example if that was my only use for it i would just use `int` rather than `var`. – Jarod Kientz Apr 23 '14 at 19:58
  • In my application I will be using to call up one of several potential data types. – Jarod Kientz Apr 23 '14 at 19:59
  • @JarodKientz Marking a question as a duplicate doesn't lead to deletion or reduce searchability. It just ensures that answers to essentially the same issue don't get spread out across multiple questions. – Adi Inbar Apr 23 '14 at 20:27
  • "I thought it might be a good idea to leave up..." - the question is not deleted. It is only closed, and will be available for others to find. When others find it, they can navigate to the previous question and answer. – jww Apr 23 '14 at 20:32

2 Answers2

8

You are trying to put method scope code directly in your class. It needs to be inside of a method or you need to give it an explicit type.

You can't use var for members of a class:

Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var.

Tim
  • 14,999
  • 1
  • 45
  • 68
4

See: var (C# Reference)

Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var.

It seems you are trying to declare and initialize the field at class level, You can't use var at class level.

You may see this article: Why no var on fields? - By Eric Lippert

Habib
  • 219,104
  • 29
  • 407
  • 436
  • Good article link. However, there is a new compiler coming, and with it maybe it is easier to implement implicitly typed fields with initializers. But it does not seem to be on their [list of new language features](https://roslyn.codeplex.com/wikipage?title=Language%20Feature%20Status). – Jeppe Stig Nielsen Apr 23 '14 at 20:30