在我的控制方法中显示错误消息 [英] To make an error message in my control method

查看:57
本文介绍了在我的控制方法中显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的类中有一个称为Address
的方法

I have this method in my class called Address


public bool CheckData()

{

   if( Address != null)

   {

      city = string.Empty;

      zipCode = string.Empty;

      country = string.Empty;

      street = string.Empty

 

      return false;

   }

   else

   {

      return true;

   }
}



这是一种CheckData方法,用于对用户提供的地址信息的有效性进行某种类型的控制.该程序可能要求(例如)用于街道和城市的输入框不能为空.如果控件成功,该方法应返回true,否则返回false,否则我有一个strErrMessage字段,当控件失败时,我想包含错误消息.如果我做错了,请纠正我.


现在,我想做的是使用一个名为strErrMessage的字段来包含控件失败时的错误消息.

我真的无法做到这一点,有人知道然后请帮助我.感谢



this is a CheckData method performing some type of a control for the validity of the address information given by the user. The program may require that (for example) the input boxes intended for street and city are not empty. The method should return true if the control is successful and false otherwise I have a field strErrMessage which i want to contain the error message when the control fails. If i did this incorrect please correct me.


Now what i want to do is to use a field called strErrMessage to contain the error message when the control fails.

I cant really se how to do it, does anybody know then please help me. Thanks

推荐答案

一堆有效性检查,如下所示:

A bunch of validity checking like so:

public bool CheckData(Address address)
{
    if (address != null)
    {
        if (address.City != "" &&
            address.Street != "")
        {
           try
           { 
               //do stuff
               return true;
           }
           catch (Exception ex)
           {
               strErrMessage = ex.Message;
               // or use your own custom error message
               return false;
           }
        } 
        else
        {
            strErrMessage = "So and so cannot be left blank, etc...";
            return false;
        }
    }
    else
    {
        strErrMessage = "Address cannot be null!";
        return false;
    }
}


这似乎很简单.我不明白为什么您要让变量为空...
您想在checkdata失败时向客户端显示ErrorMessage.
This seems simple. I do not understand why you make your variables empty...
You want to display an ErrorMessage to clients when checkdata fails.
// Private field to store the error message.
private String _errMessage;
// Public property to show it to clients.
public String ErrorMessage { get{ return _errMessage; } }

public bool CheckData()
{
   // First clear your error message.
   _errMessage = String.Empty;
   if( Address != null)
   {
      // Keep a boolean to check if the Address if valid.
      bool valid = true;

      if (city == string.Empty)
         { valid = false; _errMessage = "City cannot be empty{0}"; }
      if (zipCode == string.Empty)
         { valid = false; _errMessage = "Zipcode cannot be empty{0}"; }        
      if (country == string.Empty)
         { valid = false; _errMessage = "Country cannot be empty{0}"; }
      if (street == string.Empty)
         { valid = false; _errMessage = "Street cannot be empty{0}"; }

      if (!valid)
      {
         // Remove the last {0} from the string and replace other {0}'s with new lines.
         _errMessage = String.Format(_errMessage.SubString(0, msg.Length - 3), Environment.NewLine);
      }
      return valid;
   }
   else
   {
      // Return false here. The validation failed since there is no Address at all!
      _errMessage = "There is no address!";
      return false;
   }
}


现在,在调用代码时,您可以执行以下操作:


Now in you calling code you could do something like:

if (!myObj.CheckData)
{
   MessageBox.Show(myObj.ErrorMessage);
}
else
{
   MessageBox.Show("Check succeeded!");
}


希望这可以帮助! :)


Hope this helps! :)


这篇关于在我的控制方法中显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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