示例代码如下:
public struct SpiderResult { public string robotName; public string RobotName { get { return robotName; } set { robotName = value; } } public int num; public int totalNum; } public class TestClass { public SpiderResult spider = new SpiderResult(); public SpiderResult Spider { get { return spider; } set { spider = value; } } }
调用如下:
public partial class Form1 : Form { public Form1() { TestClass testclass = new TestClass(); testclass.Spider.RobotName = "Baidu";//编译出错 } }
//编译错误
错误 CS1612: 无法修改“SpiderAnalysis.TestClass.Spider”的返回值,因为它不是变量
解决方法:
方法一:
把struct替换成class
方法二:
如果非要用struct不可的话,需重新生成一个所用到的struct,即设置一个中间变量:
public partial class Form1 : Form { public Form1() { TestClass testclass = new TestClass(); SpiderResult tempSpider = new SpiderResult(); tempSpider.robotName = "Baidu"; testclass.Spider = tempSpider; } }
本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:


