[英]How to set property of variable
这是我的功能,它将所有项目放置在数据库中。
public bool getUserProjects(ref List<erpAssets>userProjects)
{
string arguments = "{\"sessionId\":\"" + m_LoggedInUser.sessionId + "\"," +
"\"assetType\":\"" + PROJECT_ASSET_TYPE_NAME + "\"" +
"}";
string response = UrlParser(METHOD_GET_ASSETS, MODULE_ADMINISTRATION, arguments); //calling the function urlParse to get the response from that page
erpAPIResponse basicResponse = JsonConvert.DeserializeObject<erpAPIResponse>(response);
if (basicResponse.success.Equals("yes"))
{
try
{
erpAssets[] Projects = JsonConvert.DeserializeObject<erpAssets[]>(basicResponse.arguments);
userProjects.AddRange(Projects);
}
catch (Exception e)
{
}
}
else return false;
return true; // sending the response back to client
}
我的erpAsset类如下:
class erpAssets
{
public string assetId { get; set; }
public string assetSerialNo { get; set; }
public string serialNo { get; set; }
public string assetDescription { get; set; }
public string assetType { get; set; }
public string parentId { get; set; }
public string assetIsTrakable { get; set; }
public bool isTrackable { get; set; }
public bool isMovable { get; set; }
public string assetInheritsRegion { get; set; }
public string inheritsRegion { get; set; }
public string assetModel { get; set; }
public string model { get; set; }
public erpPoint[] assetRegion { get; set; }
}
我的erpPoint类如下:
class erpPoint
{
public double X { get; set; }
public double Y { get; set; }
}
现在我面临的问题是,当assetRegion为null时,我得到以下异常
将值“”转换为类型“ erp.erpPoint []”时出错。
可以正常工作的scenario1:{“ assetId”:“ 43711”,“ assetSerialNo”:“ Sector43”,“ assetDescription”:“”,“ assetVersion”:“”,“ assetIsMovable”:“ f”,“ assetType”:“项目”,“ assetModel”:“项目”,“ parentId”:“ 32537”,“ assetIsTrackable”:“ f”,“ assetInheritsRegion”:“ f”,“ assetRegion”:[{“ X”:-122.69103924537,“ Y “:49.105749366835},{” X“:-122.69103924537,” Y“:49.119046702041},{” X“:-122.68010753619,” Y“:49.119046702041},{” X“:-122.68010753619,” Y“:49.105749366835}] }
这很好。
方案2:
{“ assetId”:“ 64374”,“ assetSerialNo”:“ FeedLot”,“ assetDescription”:“”,“ assetVersion”:“”,“ assetIsMovable”:“ t”,“ assetType”:“项目”,“ assetModel” :“ Project”,“ parentId”:“ 64374”,“ assetIsTrackable”:“ t”,“ assetInheritsRegion”:“ f”,“ assetRegion”:“”}
这是我越来越例外的地方。 assetregion为null,现在我在设置其值时遇到异常
您有两种选择:
采用
{ get; set; }
并且您无法指定主体。
或者,您必须为getter和setter声明一个主体。
另请注意, double
不能为null。 使用double?
代替。
private double? x;
public double? X
{
get { return this.x; }
set
{
if (value != null)
{
this.x = value;
}
}
}
如果要创建自定义设置程序,还必须提供一个自定义获取程序。 通常,这意味着您需要自己创建支持字段。
class a
{
private double x;
public double X
{
get
{
return this.x;
}
set
{
if (value != null) // warning, see below
{
this.x = value;
}
}
}
}
当然,还存在一个问题,就像所有值类型*一样, double
不能为空,整个方法可能根本没有必要。 如果要确保X
永远不会设置为空值,只需将其声明为double
,而不必担心。 这样就足够了:
class a
{
public double X { get; set; }
}
* Nullable<T>
除外
您需要使用详细语法:
private double? x;
public double? X
{
get { return x; }
set {
if (value != null){
x = value;
}
}
}
更新资料
正确指出double
不能为null的类型将其更改为可为null的类型。
您可以使用自动属性或现场支持的特性。 你不能混。 在您的情况下,这意味着
public double? DifferentPorpName
{
get {return this.X;}
...
}
就设置而言,我会将其更改为以下内容,这对我来说更具声明性:
set
{
double x = value;
if (value != null) x = value;
else x = this.X;
this.X = x;
}
您的get
需要正确声明或完全省略
我忘了问你,如何将null传递给double属性? 如果尝试运行,肯定会在运行时收到错误消息。
糟糕,其他人已经注意到了:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.