C# 赋值的左侧必须是变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19843350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:58:36  来源:igfitidea点击:

left hand side of an assignment must be a variable

c#asp.netlinq

提问by farhang67

Trying to put an integer data from database(Linq to sql) into a label getting this error exception:

尝试将数据库(Linq 到 sql)中的整数数据放入标签中,出现此错误异常:

left-hand side of an assignment must be a variable property or indexer

赋值的左侧必须是变量属性或索引器

Code:

代码:

protected void Page_Load(object sender, EventArgs e)
{
   DataClassesDataContext data = new DataClassesDataContext();

   var visit = (from v in data.SeeSites where v.Date == todaydate select v).FirstOrDefault();
   int seennow = visit.See; // On This line I can put data in seenow variable, no problem

   Convert.ToInt64(lblSeeNow.Text) = visit.See;   // exception error appears here
}

采纳答案by Yuriy Galanter

Try:

尝试:

if (visit.See != null) {
    lblSeeNow.Text = visit.See.ToString();
}

You cannot assign something toa function result. In your case lblSeeNow.Textis of type String hence usage of ToString(); method of your Int value.

你不能分配的东西一个函数的结果。在您的情况下lblSeeNow.Text是 String 类型,因此使用 ToString(); 你的 Int 值的方法。

回答by Satpal

You need to use

你需要使用

 lblSeeNow.Text = visit.See.ToString(); 

回答by Smeegs

Convert.ToInt64(lblSeeNow.Text) = visit.See;

As you mentioned, this is the issue.

正如你提到的,这就是问题所在。

Convert.ToInt64is a method. But you're trying to save a value to it.

Convert.ToInt64是一种方法。但是您正在尝试为它保存一个值。

You can't.

你不能。

Just do this

就这样做

lblSeeNow.Text = visit.See.ToString();

回答by Esteban Elverdin

You should convert the integer to string, also add a check for being sure that visit is not null

您应该将整数转换为字符串,并添加检查以确保访问不为空

lblSeeNow.Text = visit != null ? visit.See.ToString() : string.Empty

回答by JBrooks

I think you want

我想你想要

lblSeeNow.Text = visit.See.ToString();

You can't assign anything to

你不能分配任何东西

Convert.ToInt64(lblSeeNow.Text)

because it evaluates to a number.

因为它的计算结果是一个数字。

回答by Becuzz

Convert.ToInt64(lblSeeNow.Text) isn't a variable. It takes the value in lblSeeNow.Text and converts it to a long. There isn't a variable to store stuff in anymore.

Convert.ToInt64(lblSeeNow.Text) 不是变量。它采用 lblSeeNow.Text 中的值并将其转换为 long。没有一个变量来存储东西了。

You probably want this:

你可能想要这个:

lblSeeeNow.Text = visit.See.ToString();