比较两个自定义类

时间:2017-02-22 10:41:43

标签: c# equality

我正在尝试比较我创建的两个对象 在其他一些帖子中,该类需要从<div class="accordion-contentBuy" ng-show="accordion==1"> <form name='ownerDetailsForm' ng-show='screenOneStatus'> <div class=""> <div class='section group' layout="row"> <div flex="50" style="width: 100%;"> <md-input-container class="md-block"> <div class=""> <!--ng-class="{ 'formError': ownerDetailsForm.OwnerfirstName.$invalid}">--> <label class="buyTextPadding" for="inputNormal">First name</label> <input class="mdInputContainer_input" name='OwnerfirstName' ng-model='proposerDetails.firstName' autocomplete='off' maxlength='35' minlength='1' ng-pattern='/^[a-zA-Z ]*$/' required /> </div> </md-input-container> </div> <div flex="50" style="width: 100%;"> <md-input-container class="md-block"> <div class=""> <label class="buyTextPadding" for="inputNormal">Last name</label> <input class="mdInputContainer_input" name='OwnerlastName' ng-model='proposerDetails.lastName' autocomplete='off' maxlength='35' minlength='1' ng-pattern='/^[a-zA-Z ]*$/' required /> </div> </div> </div> <div class='section group' layout="row"> <div flex="50" style="width: 100%;"> <md-input-container class="md-block"> <div> <label class="buyTextPadding" for="inputNormal">Email ID</label> <input class="mdInputContainer_input" ng-pattern='/^[a-zA-Z]+[a-zA-Z0-9._]+@[a-zA-Z0-9]+.[a-z.]{2,5}$/' autocomplete='off' maxlength='35' name='emailId' ng-model='proposerDetails.emailId' required /> </div> </md-input-container> </div> <div flex="50" style="width: 100%;"> <md-input-container class="md-block"> <div> <label class="buyTextPadding" for="inputNormal">Mobile Number</label> <input class="mdInputContainer_input" name='mobileNumber' ng-model='proposerDetails.mobileNumber' autocomplete='off' ng-pattern='/[0-9]{10}/' maxlength='10' minlength='10' required /> </div> </md-input-container> </div> </div> <div class='section group' layout="row"> <div flex="50" style="width: 100%;"> <md-input-container class="md-block"> <label class="buyTextPadding">{{globalLabel.applicationLabels.life.genderType}}</label> <md-select name="type" ng-model="proposerDetails.gender" class="mdInputContainer_input" ng-change="changeGender()" required> <md-option ng-value="gender.label" ng-repeat="gender in genderType">{{gender.label}}</md-option> </md-select> </md-input-container> </div> <div flex="50" style="width: 100%;"> <md-input-container class="md-block" ng-class="{ 'formError': ownerDetailsForm.maritalStatus.$invalid}"> <label class="buynowSelect">Marital Status</label> <md-select name="type" class="mdInputContainer_input" ng-model="proposerInfo.maritalStatus" ng-change="changeMaritalStatus()" required> <md-option ng-value="maritalstatus" ng-repeat="maritalstatus in maritalStatusType"> {{maritalstatus.name}}</md-option> </md-select> </md-input-container> </div> </div> <div class='section group' layout="row"> <div flex="50" style="width: 100%;" ng-if='panCardStaus'> <md-input-container class="md-block"> <div> <label class="buyTextPadding" for="inputNormal">PAN Card Number</label> <input class="mdInputContainer_input" name='PAN' ng-model='proposerDetails.panNumber' autocomplete='off' maxlength='10' minlength='10' ng-change="changePancard()" ng-pattern='/[A-Za-z]{5}[0-9]{4}[A-Za-z]{1}/' style='text-transform: uppercase;' required /> </div> </md-input-container> </div> <div flex="50" style="width: 100%;" ng-if='!panCardStaus'> <md-input-container class="md-block"> <div> <label class="buyTextPadding" for="inputNormal">PAN Card Number </label> <input class="mdInputContainer_input" name='PAN' ng-model='proposerDetails.panNumber' autocomplete='off' maxlength='10' minlength='10' ng-change="changePancard()" ng-pattern='/[A-Za-z]{5}[0-9]{4}[A-Za-z]{1}/' style='text-transform: uppercase;' /> </div> </md-input-container> </div> <div flex="50" style="width: 100%;"> <md-input-container class="md-block"> <div class=""> <label class="buyTextPadding" for="inputNormal">Aadhar Number </label> <input class="mdInputContainer_input" name='AADHAR' ng-model='proposerDetails.aadharNumber' autocomplete='off' maxlength='12' minlength='12' style='text-transform: uppercase;' /> </div> </md-input-container> </div> </div> <div class='section group' layout="row"> <div flex="50" style="width: 100%;"> <md-input-container class="md-block"> <div> <label class="buyTextPadding" for="inputNormal">Enter Communication Address</label> <input class="mdInputContainer_input" details="chosenPlaceDetails" placeholder="Enter Communication Address" name='address' ng-model='proposerDetails.address' googleplace ng-change="resetCommunicationAddress()" autocomplete='off' maxlength='250' minlength='1' required /> </div> </md-input-container> </div> <div flex="50" style="width: 100%;"> <md-input-container class="md-block"> <div> <label class="buyTextPadding" for="inputNormal">Enter Pincode <a style="color: #f7a100;" ng-click='togglePin()'>[Change]</a> </label> <input class="mdInputContainer_input" name='Pincode' ng-model='proposerDetails.pincode' ng-pattern='/[0-9]{6}/' autocomplete='off' readonly /> </div> </md-input-container> </div> </div> <div class='section group' layout="row"> <div flex="50" style="width: 100%;"> <md-input-container class="md-block"> <div> <label class="buyTextPadding" for="inputNormal">Enter City</label> <input class="mdInputContainer_input" name='City' ng-model='proposerDetails.city' autocomplete='off' readonly /> </div> </md-input-container> </div> <div flex="50" style="width: 100%;"> <md-input-container class="md-block"> <div> <label class="buyTextPadding" for="inputNormal">Enter State</label> <input class="mdInputContainer_input" name='State' ng-model='proposerDetails.state' autocomplete='off' readonly /> </div> </md-input-container> </div> </div> <div ng-bind-html="ownerDetailsTemplate" bind-html-compile></div> <div class='buyOwnerBtn'> <md-input-container class="md-block"> <md-button ng-disabled='ownerDetailsForm.$invalid' class='md-raised md-primary' ng-click="submitPersonalDetails()">Proceed To Nomination Details</md-button> </md-input-container> </div> </div> </form> </div>接口继承,然后覆盖IEquatable<T>函数。当我使用Equals比较班级的实例时,它总是会返回cellStyle1.Equals(cellStyle2) ...

我已经尝试过它似乎不起作用。这是我试过的:

false

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您调用# dummy data df1 <- read.table(text = "x y z a 1 1 b 3 NA NA 5 0 f NA 1", header = TRUE) # result apply(combn(colnames(df1), 2), 2, function(i){ res <- df1[, i] res[complete.cases(res), ] }) # [[1]] # x y # 1 a 1 # 2 b 3 # # [[2]] # x z # 1 a 1 # 4 f 1 # # [[3]] # y z # 1 1 1 # 3 5 0 方法继承自Equals而非您的实施方法。您应该覆盖基本System.Object方法,最好使用隐式接口实现(而不是显式),因此如果编译器可以解析此调用,则会直接调用您的方法(EqualscellStyle1都是在编译时编译cellStyle2。)

答案 1 :(得分:1)

您正在显式实现IEquatable<Cell_Style>,因此您需要在调用之前强制转换为该接口:

((IEquatable<Cell_Style>)cellStyle1).Equals(cellStyle2)

更简单的方法是将实施公开:

public bool Equals(Cell_Style other) { ... }

如果Equals的类型为cellStyle2,则会选择Cell_Style的正确重载。您还应覆盖object.Equals以致电Equals(Cell_Style)并实施GetHashCode