为什么我的财产没有进行检查?

时间:2015-06-25 11:37:43

标签: c# .net properties setter

我的构造函数从表单中获取信息。这个名字'以' aName' (来自构造函数),然后必须使用prop' NameCheck'进行检查。但是如果我编译它,它会返回一个空字符串。有什么想法吗?

代码:

----分类----

//Fields
private List<Create> Characters;
private string name;
private int health;
private int mSpeed;
private Role role;
private Speciality speciality;

//Properties
public string NameCheck
{
    get {
        return name;
    }
    set
    {
        if (string.IsNullOrEmpty(name))
        {
            name = "Name not specified";
        }
        else 
        {
            name = value;
        }
    }
}

//Constructor

public Create(string aName, int aHealth, int aMSpeed, Role aRole, Speciality aSpeciality)
{
    this.name = aName;
    this.health = aHealth;
    this.mSpeed = aMSpeed;
    this.role = aRole;
    this.speciality = aSpeciality;

    Characters = new List<Create>();
}

----表格----

Create character = new Create(tbName.Text, health, mspeed, aLane, aSpecial);

Characters.Add(character);

cbSummary.Items.Add(character.ToString());

PS:cbSummary是一个组合框。

修改

public override string ToString()
{
    return "Name: " + NameCheck + " - Health: "  + health + " -  MovementSpeed: " + mSpeed + " - Role: " + role + " - Speciality: " + speciality;
}

3 个答案:

答案 0 :(得分:2)

您应该在构造函数

中设置a->m1(); // A::m1 a->m2(); // B::m2 a->m3(); // A::m3 而不是this.NameCheck
this.name

您也应该在属性设置器中检查public Create(string aName, int aHealth, int aMSpeed, Role aRole, Speciality aSpeciality) { this.NameCheck = aName; 是否为空,或者为空{而不是value

name

答案 1 :(得分:1)

你的财产中有一个小错字:

public string NameCheck
{
    get {
        return name;
    }
    set
    {
        // You need to check value, not name.
        if (string.IsNullOrEmpty(value))
        {
            name = "Name not specified";
        }
        else 
        {
            name = value;
        }
    }
}

答案 2 :(得分:1)

在构造函数中,您应该使用NameCheck属性而不是name字段:

public Create(string aName, int aHealth, int aMSpeed, Role aRole, Speciality aSpeciality)
{
    this.NameCheck = aName;
    this.health = aHealth;
    this.mSpeed = aMSpeed;
    this.role = aRole;
    this.speciality = aSpeciality;

    Characters = new List<Create>();
}

同样,Paddy说您在set NameCheck public string NameCheck { get { return name; } set { if (string.IsNullOrEmpty(value)) { name = "Name not specified"; } else { name = value; } } } 正在对<?php require_once("includes/database.php");?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Ask Coupon | Promotional codes,Coupons,Deals and many more</title> <link rel="stylesheet" href="css/hover.css"> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="jquery/jquery-2.1.4.js"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script src="js/bootstrap.min.js"></script> <script> $( document ).ready(function() { $("[rel='tooltip']").tooltip(); $('.thumbnail').hover( function(){ $(this).find('.caption').slideDown(250); //.fadeIn(250) }, function(){ $(this).find('.caption').slideUp(250); //.fadeOut(205) } ); }); </script> </head> <body> <?php $query = "select store_name,no_of_coupons,caption from featured_stores"; $result = mysql_query($query);?> <?php if (!$result) die ("Database access failed: " . mysql_error());?> <div class="container"> <div class="row"> <?php while($row = mysql_fetch_assoc($result)) {?> <div class="col-lg-2 col-sm-6 col-xs-6 col-md-3"> <div class="thumbnail"> <div class="caption"> <h4><?php echo $row["no_of_coupons"]?></h4> <p><?php echo $row["caption"]?></p> </div> <img src="img/<?php echo $row["store_name"]?>.jpeg" alt="..."> </div> </div> <?php }?> </div> </div><!-- /.container --> <?php require_once("includes/database_closing.php"); ?> </body> </html>正文进行错误检查:

  data[complete.cases(data),]
相关问题