Visual Studio 2015 suddenly don't want to create new variables in classes [closed]

放肆的年华 提交于 2020-01-30 12:05:20

问题


I'm struggling with the issue almost whole day, I didn't find any answer in the internet. I know this fix is just to shift one small thing, however I have not idea which one. Uninstall (using TotalUninstaller) of visual studio (community 2015) didn't fix mine problem. That's it for the word of introduction ...

I can't define any object/variable in classes. Does not matter is it in new project or existing.

Example Snippet of a class in existing project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AGDS
{
    public class Similarity
    {
        public string index;
        public double distance;
        public double similarityRate;
        public Similarity(string index, double distance)
        {
            this.index = index;
            this.distance = distance;
        }
        // NEW PART WHICH HAS BEEN ADDED
        int b;
        b = 100;
        // END
    }
}

When I try to build I receive following errors

Error   CS1519  Invalid token '=' in class, struct, or interface member declaration AGDS
Error   CS0103  The name 'b' does not exist in the current context. AGDS

Moreover in any project in Program.cs class I'm able to define. I have strange felling that installation of local SQL server could broke something ... Has anyone got idea what can be wrong and how to fix it?


回答1:


Is it possible to declare variables then assign values to those variables outside of a function?

Try to do this:

public class Similarity
{
    public string index;
    public double distance;
    public double similarityRate;
    public Similarity(string index, double distance)
    {
        this.index = index;
        this.distance = distance;
    }

    int b = 100;  //new code
}



回答2:


You are able to set a default value for the b member like this: int b = 100;. However, after the member declaration, you won't be able to assign a value to b member outside a function. When you want to assign a value to a member, you should determine when it should be done. So if you want to set the b value when a instance of Similarity is created, you can assign it in the class constructor:

int b;
public string index;
public double distance;
public double similarityRate;
public Similarity(string index, double distance)
{
    this.index = index;
    this.distance = distance;
    b = 100;
}

On a side note, if you want to declare b as a public property, the right way to do this would be public int b {get; set;}. Since C# 6, you can also set a default value directly to the a property: public int b {get; set;} = 100;




回答3:


Error   CS1519  Invalid token '=' in class, struct, or interface member declaration AGDS
Error   CS0103  The name 'b' does not exist in the current context. AGDS

These two messages are a bit vague, but they are correct. In the code example, it is attempting to act like code within a method or function. Put that same code into a method or function and it will work.

If you intended b to be a field or property... You can shrink the declaration and assignment to one line. For a property use Getters and Settters.



来源:https://stackoverflow.com/questions/37380001/visual-studio-2015-suddenly-dont-want-to-create-new-variables-in-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!