【问题标题】:Ternary Statement With DrawTextValuePair带 DrawTextValuePair 的三元语句
【发布时间】:2017-08-28 18:00:28
【问题描述】:

当文本框值为空时,我收到一个错误,因此我试图编写一个三元语句来检查该值是否为空。这是我想出来的:

DrawTextValuePair(e, string.IsNullOrEmpty(m_pcl.pn.Text) ? String.Format("Input: ", m_pcl.pn.Text) : String.Format("Input: "), true, m_leftMargin);

但是在使用这样我得到一个编译错误:

没有给出与“ProfileDocument.DrawTextValuePair(PrintPageEventArgs, string, string, bool, int)”所需的形参“iLeftMargin”相对应的参数

在这种情况下使用三元语句的正确方法是什么?

【问题讨论】:

    标签: c# ternary-operator drawtext


    【解决方案1】:

    方法

    ProfileDocument.DrawTextValuePair(PrintPageEventArgs, string, string, bool, int)
    

    需要 5 个参数,但您只提供了 4 个:

    DrawTextValuePair(
        e, // 1st
        string.IsNullOrEmpty(m_pcl.pn.Text) ? String.Format("Input: ", m_pcl.pn.Text) : String.Format("Input: "), // 2nd
        true, // 3rd
        m_leftMargin // 4th
    );
    

    【讨论】:

    • 哈哈,所以我的三元语句没有任何问题,只是提供了错误数量的参数!
    • @YohanGreenburg 没错!这就是错误消息告诉我们的原因。 :p
    • @Dmitry 无意冒犯,但可以简化代码。第二种字符串格式是多余的。我们不应该保持良好的做法,当我们使用一次字符串别名而不是在任何地方都保持它?我知道字符串只是编码标准的一部分,但仍然......?
    • @KrzysztofLa 我同意,但它与主题有何关系?
    • @KrzysztofLa 我同意,代码审查是开发中非常重要的一部分。这就是为什么在这里总是欢迎代码审查的原因:codereview.stackexchange.com。你可以参加。 :p
    猜你喜欢
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多