C#错误CS0201:只有赋值、调用、递增、递减和新对象表达式可以作为语句使用
Posted
技术标签:
【中文标题】C#错误CS0201:只有赋值、调用、递增、递减和新对象表达式可以作为语句使用【英文标题】:C# error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement 【发布时间】:2013-09-13 02:38:51 【问题描述】:这是我收到错误的行:
_activeType == typeof(Reticle);
这是Reticle
类的定义方式。有些属性和方法可能没有多大意义,但我认为类的定义应该是关键问题:
public class Reticle : Shape
#region Fields
private readonly Path _path;
private bool _isClosed;
private PointCollection _points;
#endregion Fields
#region Constructors
public Reticle() // constructor
var geometry = new PathGeometry();
geometry.Figures.Add(new PathFigure());
_path = new Path Data = geometry ;
Points = new PointCollection();
#endregion Constructors
#region Properties
/// <summary>
/// Gets or sets a value that specifies the arc roundness.
/// </summary>
public Geometry Data
get
return _path.Data;
/// <summary>
/// Gets or sets a value that specifies if the polygon will be closed or not.
/// </summary>
public bool IsClosed
get
return _isClosed;
set
_isClosed = value;
/// <summary>
/// Gets or sets a collection that contains the points of the polygon.
/// </summary>
public PointCollection Points
get return _points;
set
_points = value;
protected override Geometry DefiningGeometry
get
return _path.Data;
#endregion Properties
#region Methods
private static Point CreateRectangle(Point p1, Point p2, double distance, bool firstPoint)
double segmentLength = Math.Sqrt(Math.Pow((p2.X - p1.X), 2) + Math.Pow((p2.Y - p1.Y), 2));
//The distance cannot be greater than half of the length of the segment
if (distance > (segmentLength / 2))
distance = segmentLength / 2;
double rap = firstPoint ? distance / segmentLength : (segmentLength - distance) / segmentLength;
return new Point(p1.X + (rap * (p2.X - p1.X)), p1.Y + (rap * (p2.Y - p1.Y)));
private static Point CreateCrosshair(Point p1, Point p2, double distancePercent, bool firstPoint)
double rap = firstPoint ? distancePercent / 100 : (100 - distancePercent) / 100;
return new Point(p1.X + (rap * (p2.X - p1.X)), p1.Y + (rap * (p2.Y - p1.Y)));
#endregion Methods
#region Other
#endregion Other
我是 C# 新手,我不知道为什么会发生错误。有人知道我应该如何纠正错误吗?
【问题讨论】:
什么是_activeType? _activeType 是 Type 类的对象吗? 私有类型_activeType; 【参考方案1】:我想你是说
_activeType = typeof(Reticle);
不是双重的==
【讨论】:
【参考方案2】:_activeType = typeof(Reticle);
== 是比较运算符(用于比较)
【讨论】:
以上是关于C#错误CS0201:只有赋值、调用、递增、递减和新对象表达式可以作为语句使用的主要内容,如果未能解决你的问题,请参考以下文章
CS0201:只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句
unity error CS0201: 只有赋值、调用、递增、递减、等待和新对象表达式可以作为语句使用
在C#三元运算符给出错误:只有赋值,调用,递增,递减和新对象表达式可用作语句