简体   繁体   中英

assigning string to a property of string type C#

I can better explain my problem by code
here it is

strind abc="12345678<9";
row1ViewModel data = new row1ViewModel();
data.identityType = abc[0].ToString();
data.passportType = abc[1].ToString();
data.issuingOrg = abc.Substring(2, 3);
var  actual = "";
data.lastName = actual;
//data.lastName = actual;
if (abc[5] == '<')
{
    actual = "Not specified";
}
else
{
    string tempq = abc.Substring(5);
    int index = tempq.IndexOf('<');
    actual = abc.Substring(5, index);
}
//data.GetType().GetProperty(data.lastName).GetValue(actual,null)

Here I need to set my property (data.lastname of string type) to a string actual value. But how?

You need to move your

 data.lastName = actual;

to after your else loop, if im reading your logic correct.

You have first assigned empty string property:

var  actual = "";
     data.lastName = actual; 

and then update the value of actual, but the property still has "" as it's value You should just move

data.lastName = actual; 

to the end of the code:

         strind abc="12345678<9";
     row1ViewModel data = new row1ViewModel();
   data.identityType = abc[0].ToString();
      data.passportType = abc[1].ToString();
      data.issuingOrg = abc.Substring(2, 3);
      var  actual = "";          
     //data.lastName = actual;
      if (abc[5] == '<')
      {
          actual = "Not specified"; 
     } 
     else 
     { 
         string tempq = abc.Substring(5);
          int index = tempq.IndexOf('<'); 
         actual = abc.Substring(5, index); 
       } 
     //data.GetType().GetProperty(data.lastName).GetValue(actual,null)`  
     data.lastName = actual; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM