在Propertis中拆分字符串 [英] Spliting of strings in Propertis

查看:79
本文介绍了在Propertis中拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在属性中分割字符串.

我像下面这样写它的核心

Is it possible to split a string in properties.

I write the core for this as like bellow

public Facility facility { get; set; }

public FOBillLineItem LineItem { get; set; }
public Facility facility { get; set; }

public string Name
       {
           get
           {
               facility = CoreManager.DC.Facilities.FirstOrDefault();
               if (facility.TaxMethodpersent == 1)
               {
                   string[] n = LineItem.Name.Split('@');
                   return n[0];
               }
               else
               {
                   return LineItem.Name;
               }
           }
       }
      public decimal Discount
      {
          get
          {
              if (LineItem != null)
                  return LineItem.Discount;
              else
                  return 0;
          }
          set
          {
              LineItem.Discount=value;
          }
      }



但是它在LineItem.Name



But it give an exception "Object reference is not set to an instance of object" at LineItem.Name

推荐答案

是的.您可以.
但是您必须检查LineItem和LineItem.Name是否为null以及.Spilt是否给出任何结果.
因此,您将得到对象引用未设置为对象的实例".

试试这个代码
Yes you can.
But you have to check LineItem and LineItem.Name is null and if .Spilt gives any results.
It is therefore you will get "Object reference is not set to an instance of object".

Try this code
if (LineItem != null && !string.IsNullOrEmpty(LineItem.Name))
{
  string[] n = LineItem.Name.Split("@");
  return n.Length > 0 ? n[0] : string.Empty; // Check if string array is empty, if not return first row else return empty string.
}
else
{
  return string.Empty;
}


string[] n = LineItem.Name.Split(''@'');



请检查LineItem可能为空,

在应用拆分操作之前,应在哪里分配LineItem的值?



please check LineItem may be null,

Where do you assign the value of LineItem before applying split operation?


这篇关于在Propertis中拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆