小编use*_*542的帖子

如何处理NullReference异常c#

我正在尝试处理NullReference异常,但我很困惑如何处理它.这是我的示例代码,其中引发了NullReference异常:

 private Customer GetCustomer(string unformatedTaxId)
        {               
                return loan.GetCustomerByTaxId(new TaxId(unformatedTaxId));                
        }
Run Code Online (Sandbox Code Playgroud)

现在我用以下方法处理这个问题

 public void ProcessApplicantAddress(ApplicantAddress line)
        {
            try
            {
                Customer customer = GetCustomer(line.TaxId);
                //if (customer == null)
                //{
                //    eventListener.HandleEvent(Severity.Informational, line.GetType().Name, String.Format("Could not find the customer corresponding to the taxId '{0}' Applicant address will not be imported.", new TaxId(line.TaxId).Masked));
                //    return;
                //}
                Address address = new Address();
                address.AddressLine1 = line.StreetAddress;
                address.City = line.City;
                address.State = State.TryFindById<State>(line.State);
                address.Zip = ZipPlusFour(line.Zip, line.ZipCodePlusFour);
                }
            catch(NullReferenceException e)
            {
                //eventListener.HandleEvent(Severity.Informational, line.GetType().Name, String.Format("Could not find the customer …
Run Code Online (Sandbox Code Playgroud)

c# exception-handling exception nullreferenceexception

6
推荐指数
1
解决办法
3万
查看次数

一个属性,其中包含mvc c中的模型错误列表

在具有列表(最多3个)的控制器中,与密码检查相关的错误消息将存储在名为Password的属性中.

IEnumerable<PasswordMessages> passwordMessage = LoanTrackerServices.CheckPasswordRequirements(model.NewPassword, model.EmailId);
if ( passwordMessage.Count() > 0 )
{
    foreach (PasswordMessages pm in passwordMessage)
    {
        ModelState.AddModelError("Password",( pm.Message));
    }
    LoginPageModel loginModel = new LoginPageModel();
    return View("Index", new HomePageModel() { Register = model, Login = loginModel });
}
Run Code Online (Sandbox Code Playgroud)

但在我看来,我无法弄清楚如何获取所有这些(最多3个)错误消息.现在只显示列表中的第一条消息.这是我的代码

for (int i = 0; i < ViewData.ModelState["Password"].Errors.Count; i++)
{
     @Html.ValidationMessage("Password")
}
Run Code Online (Sandbox Code Playgroud)

如何获取密码中存储的所有错误消息?

c# model-view-controller asp.net-mvc

2
推荐指数
1
解决办法
843
查看次数

我们可以通过使用基类对象来访问派生类属性吗?

我们可以通过使用基类对象来访问派生类属性吗?

我有一个基类

 public abstract class FnmaRecord
    {
        public int Id { get; set; }            
        public abstract String Prefix
        {
            get;
        }
    }
Run Code Online (Sandbox Code Playgroud)

并且有许多派生类继承了FnmaRecord类

 public class TransactionHeader : FnmaRecord
    {
        public override string Prefix { get {return "TH";} }

        public static readonly string TransactionId = "T100099-002";

        public String TransactionControlNumber { get; set; }

    }


public class TitleHolder : FnmaRecord
    {
        public override string Prefix { get {return "02C";} }

        public string TitleHolderName { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

现在我可以在另一个类中使用FnmaRecord的实例来访问TitleHolder中的属性吗?

c# inheritance

1
推荐指数
1
解决办法
6690
查看次数