Click here to Skip to main content
15,906,106 members
Please Sign up or sign in to vote.
3.14/5 (7 votes)
See more:
Is it possible to split a string in properties.

I write the core for this as like bellow
C#
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;
          }
      }


But it give an exception "Object reference is not set to an instance of object" at LineItem.Name
Posted
Updated 16-May-11 0:21am
v4
Comments
Sergey Alexandrovich Kryukov 16-May-11 6:33am    
You did not explain for format of input string...
--SA
ajitha.pusapati 16-May-11 6:39am    
FoBillLineItem is a table name column haveing the values like 'luxry @ 5%' or 'Service @ 6%'.
In previous i send as it is by using "public string Name { get; set; }" its working now i want not display tax details. i.e. 'luxry @ 5%' will display 'luxry'

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
C#
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;
}
 
Share this answer
 
v4
Comments
ajitha.pusapati 16-May-11 6:33am    
LineItem.Name does not have any value.
but discount property working
Sergey Alexandrovich Kryukov 16-May-11 6:34am    
OK, here is the reason of the exception.
--SA
Sergey Alexandrovich Kryukov 16-May-11 6:36am    
You know what? Just stop it. Go run under debugger and locate the line where the exception is thrown, examine values of variable at this moment. If you still did not find a problem, add this info to your question... use "Improve Question"...
Sergey Alexandrovich Kryukov 16-May-11 6:40am    
So what? LineItem.Name is one thing, LineItem.Discount is another.
Read my lips:
Debugger!!!!!!!!!!!!!!!!
--SA
Kim Togo 16-May-11 6:40am    
See updated answer.
string[] n = LineItem.Name.Split('@');


please check LineItem may be null,

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900