using System;
using System.Collections.Generic;
//This is my class for obtaining certain values, putting them in a List<T>
//and then use the List to do certain operations
public class SRvaluesChecker
{
double currentPriceValue;
int totalVal, bullVal , bearVal , lineVal;
//The List is of a custom object type
List<MyValues> SRvalues = new List<MyValues>();
for(int a = 0; a <= 1000; a++)
{
//Values are assigned to currentValue, totalVal,
//bullVal , bearVal and lineVal
}
//Check if the List has a particular value
if(SRvalues.Exists(x => x.SRprice == currentPriceValue) == false)
{
//Do something
}
}
//Here is the object I created so that I can store information of different
//data types in the list
public class MyValues : IEquatable<MyValues>
{
public override string ToString()
{
return "SRprice: " + SRprice + " total count: " + totalCount +
" bullish count: " + bullishCount + " bearish count: " + bearishCount +
" line type: " + lineType;
}
public MyValues(double SRprice, int totalCount, int bullishCount , int bearishCount, int lineType)
{
this.SRprice = SRprice;
this.totalCount = totalCount;
this.bullishCount = bullishCount;
this.bearishCount = bearishCount;
this.lineType = lineType;
}
public double SRprice { get; set; }
public int totalCount { get; set; }
public int bullishCount { get; set; }
public int bearishCount { get; set; }
public int lineType { get; set; }
public override bool Equals(object obj)
{
if (obj == null) return false;
MyValues objAsPart = obj as MyValues;
if (objAsPart == null) return false;
else return Equals(objAsPart);
}
//This currently only checks for one parameter (SRprice) but
//I want to code for the others as well
public bool Equals(MyValues other)
{
if (other == null) return false;
return (this.SRprice.Equals(other.SRprice));
}
//Will override GetHashCode() and == and != operators later.
}
Comme vous pouvez le voir dans le code ci-dessus, j'ai un objet personnalisé avec 5 paramètres et je veux être en mesure de vérifier l'un d'eux dans la classe principale à savoirComment passer outre à IEquatable de manière à utiliser Contenir et Exists méthodes dans une liste <T> avec différents types
if(SRvalues.Exists(x => x.SRprice == currentPriceValue))...
if(SRvalues.Exists(x => x.totalCount == totalVal))...
if(SRvalues.Exists(x => x.bullishCount == bullVal))...
if(SRvalues.Exists(x => x.bearishCount == bearVal))...
if(SRvalues.Exists(x => x.lineType == lineVal))...
mais je ne suis pas sûr de savoir comment s'y prendre pour la méthode de codage Equals afin de distinguer ce paramètre, il est que je vérifie pour. Toute aide serait très appréciée.