如果字符串为空,则跳过行

本文关键字:字符串 如果 | 更新日期: 2023-09-27 18:10:58

如果字符串为空,我想跳过一行。

我试着这样做:

public void ReadPostedData()
{
    Sender = HttpContext.Current.Request.Form["MailFormSubmitter_sender"];
    Subject = HttpContext.Current.Request.Form["MailFormSubmitter_subject"];        
    Message = HttpContext.Current.Request.Form["Message"];
    PdfMessage = HttpContext.Current.Request.Form["PdfMessage"];
    string IsCheckPDfMailSubmit = HttpContext.Current.Request.Form["IsCheckedPDFMailForm"];
    if (IsCheckPDfMailSubmit == null || IsCheckPDfMailSubmit.Length == 0)
        //string.IsNullOrEmpty(IsCheckPDfMailSubmit);
    return IsCheckPDfMailSubmit == null;
    IsCheckedPDFMailForm = IsCheckPDfMailSubmit.Equals("True", StringComparison.OrdinalIgnoreCase); 
    string v = HttpContext.Current.Request.Form["MailFormSubmitter_includeoverviewpdf"];
    IncludeOverviewPdf = v.Equals("true,false", StringComparison.Ordinal) ? true : false;
    // http://forums.asp.net/t/1314753.aspx
    ApplicantMailNodeSelectors[0] = HttpContext.Current.Request.Form["MailFormSubmitter_applicantemailaddress1"];
    ApplicantMailNodeSelectors[1] = HttpContext.Current.Request.Form["MailFormSubmitter_applicantemailaddress2"];
    ApplicantMailNodeSelectors[2] = HttpContext.Current.Request.Form["MailFormSubmitter_applicantemailaddress3"];
}

是关于这一行的:

if (IsCheckPDfMailSubmit == null || IsCheckPDfMailSubmit.Length == 0)

如果为空,不要执行这一行:

IsCheckedPDFMailForm = IsCheckPDfMailSubmit.Equals("True", StringComparison.OrdinalIgnoreCase); 

我试着这样做:

return IsCheckPDfMailSubmit == null;

但是我得到以下错误:

错误6由于'Seneca.SfsLib.Connectors.MailFormSubmitterModel.ReadPostedData()'返回void,因此return关键字不能后跟对象表达式

那么如何跳过呢?

如果字符串为空,则跳过行

如果您想根据if语句中的条件退出函数,则当方法将void作为返回类型时,仅使用return;:

if (IsCheckPDfMailSubmit == null || IsCheckPDfMailSubmit.Length == 0) {
  return;
}

或者,不是在函数中间退出,而是可以反转条件并将其余代码放入if语句中:

if (!(IsCheckPDfMailSubmit == null || IsCheckPDfMailSubmit.Length == 0)) {
  // ...
  // rest of the code in the method
  // ...
}

如果您只是想跳过下面的行,但继续使用方法中的其余代码,则反转条件并将该行放入if语句中:

if (!(IsCheckPDfMailSubmit == null || IsCheckPDfMailSubmit.Length == 0)) {
  IsCheckedPDFMailForm = IsCheckPDfMailSubmit.Equals("True", StringComparison.OrdinalIgnoreCase); 
}

你的方法是void类型,因此你不能返回任何值。表达式return IsCheckPDfMailSubmit == null;试图返回一个bool值是不正确的,因此你会得到一个编译器错误。

你可以用几种方法来修复它,一种方法是改变你的if条件来检查备用情况。

public void ReadPostedData()
{
    Sender = HttpContext.Current.Request.Form["MailFormSubmitter_sender"];
    Subject = HttpContext.Current.Request.Form["MailFormSubmitter_subject"];        
    Message = HttpContext.Current.Request.Form["Message"];
    PdfMessage = HttpContext.Current.Request.Form["PdfMessage"];
    string IsCheckPDfMailSubmit = HttpContext.Current.Request.Form["IsCheckedPDFMailForm"];
    if (IsCheckPDfMailSubmit != null && IsCheckPDfMailSubmit.Length > 0)
    // A better option is to use string.IsNullOrEmpty:
    // if (!string.IsNullOrEmpty(IsCheckPDfMailSubmit))
    {
    IsCheckedPDFMailForm = IsCheckPDfMailSubmit.Equals("True", StringComparison.OrdinalIgnoreCase); 


    string v = HttpContext.Current.Request.Form["MailFormSubmitter_includeoverviewpdf"];
    IncludeOverviewPdf = v.Equals("true,false", StringComparison.Ordinal) ? true : false;
    // http://forums.asp.net/t/1314753.aspx
    ApplicantMailNodeSelectors[0] = HttpContext.Current.Request.Form["MailFormSubmitter_applicantemailaddress1"];
    ApplicantMailNodeSelectors[1] = HttpContext.Current.Request.Form["MailFormSubmitter_applicantemailaddress2"];
    ApplicantMailNodeSelectors[2] = HttpContext.Current.Request.Form["MailFormSubmitter_applicantemailaddress3"];
   }
}
public void ReadPostedData()
{
    Sender = HttpContext.Current.Request.Form["MailFormSubmitter_sender"];
    Subject = HttpContext.Current.Request.Form["MailFormSubmitter_subject"];        
    Message = HttpContext.Current.Request.Form["Message"];
    PdfMessage = HttpContext.Current.Request.Form["PdfMessage"];
    string IsCheckPDfMailSubmit = HttpContext.Current.Request.Form["IsCheckedPDFMailForm"];
    if (IsCheckPDfMailSubmit != null) {
        if(IsCheckPDfMailSubmit.Length > 0){
            IsCheckedPDFMailForm = IsCheckPDfMailSubmit.Equals("True", StringComparison.OrdinalIgnoreCase); 
        }
    }


    string v = HttpContext.Current.Request.Form["MailFormSubmitter_includeoverviewpdf"];
    IncludeOverviewPdf = v.Equals("true,false", StringComparison.Ordinal) ? true : false;
// http://forums.asp.net/t/1314753.aspx
    ApplicantMailNodeSelectors[0] = HttpContext.Current.Request.Form["MailFormSubmitter_applicantemailaddress1"];
    ApplicantMailNodeSelectors[1] = HttpContext.Current.Request.Form["MailFormSubmitter_applicantemailaddress2"];
    ApplicantMailNodeSelectors[2] = HttpContext.Current.Request.Form["MailFormSubmitter_applicantemailaddress3"];
}