Click here to Skip to main content
15,888,461 members
Home / Discussions / C#
   

C#

 
AnswerRe: c# unique character in char array Pin
Gerry Schmitz12-May-18 2:54
mveGerry Schmitz12-May-18 2:54 
QuestionRe: c# unique character in char array Pin
Richard MacCutchan12-May-18 3:32
mveRichard MacCutchan12-May-18 3:32 
AnswerRe: c# unique character in char array Pin
OriginalGriff12-May-18 4:16
mveOriginalGriff12-May-18 4:16 
AnswerRe: c# unique character in char array Pin
#realJSOP14-May-18 6:43
mve#realJSOP14-May-18 6:43 
GeneralRe: c# unique character in char array Pin
Mycroft Holmes14-May-18 14:13
professionalMycroft Holmes14-May-18 14:13 
GeneralRe: c# unique character in char array Pin
#realJSOP15-May-18 3:46
mve#realJSOP15-May-18 3:46 
QuestionWinforms - Find Value in Collection Containing Instantiations of a Custom Class Pin
DabbingTree11-May-18 8:37
DabbingTree11-May-18 8:37 
AnswerRe: Winforms - Find Value in Collection Containing Instantiations of a Custom Class Pin
Richard Deeming11-May-18 9:04
mveRichard Deeming11-May-18 9:04 
Start by implementing IEquatable<T> and overriding GetHashCode and Equals:
How to: Define Value Equality for a Type (C# Programming Guide) | Microsoft Docs[^]
C#
public class Move : IEquatable<Move>
{
    ...
    
    public override int GetHashCode()
    {
        // Borrowed from: https://stackoverflow.com/a/263416/124386
        // NB: Ideally, the fields should be read-only, to prevent the hash code from changing.
        unchecked
        {
            int result = 17;
            result = result * 23 + StringComparer.Ordinal.GetHashCode(displayName ?? string.Empty);
            result = result * 23 + StringComparer.Ordinal.GetHashCode(element ?? string.Empty);
            result = result * 23 + damage.GetHashCode();
            result = result * 23 + enemyDamageSubtraction.GetHashCode();
            result = result * 23 + minimumLevel.GetHashCode();
            return result;
        }
    }
    
    public override bool Equals(object obj)
    {
        return Equals(obj as Move);
    }
    
    public bool Equals(Move other)
    {
        if (ReferenceEquals(other, null)) return false;
        
        return string.Equals(displayName, other.displayName, StringComparison.Ordinal)
            && string.Equals(element, other.element, StringComparison.Ordinal)
            && damage == other.damage
            && enemyDamageSubtraction == other.enemyDamageSubtraction
            && minimumLevel == other.minimumLevel;
    }
    
    public static bool operator ==(Move left, Move right)
    {
        return Equals(left, right);
    }
    
    public static bool operator !=(Move left, Move right)
    {
        return !Equals(left, right);
    }
}

You can then use the Contains method[^] to see if your list contains an instance with the same values as your temp instance:
C#
Move tempMove = new Move
{
    displayName = "...",
    element = "...",
    damage = ...,
    enemyDamageSubtraction = ...,
    minimumLevel = ...,
};

bool alreadyInList = playerMoves.Contains(tempMove);




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

AnswerRe: Winforms - Find Value in Collection Containing Instantiations of a Custom Class Pin
BillWoodruff11-May-18 23:43
professionalBillWoodruff11-May-18 23:43 
QuestioniTextSharp - Character problem Pin
User 1367511411-May-18 1:10
User 1367511411-May-18 1:10 
AnswerRe: iTextSharp - Character problem Pin
Luc Pattyn13-May-18 23:18
sitebuilderLuc Pattyn13-May-18 23:18 
QuestionA second form from the first form, but both are active Pin
trottl10-May-18 23:18
trottl10-May-18 23:18 
AnswerRe: A second form from the first form, but both are active Pin
OriginalGriff10-May-18 23:25
mveOriginalGriff10-May-18 23:25 
GeneralRe: A second form from the first form, but both are active Pin
trottl10-May-18 23:32
trottl10-May-18 23:32 
GeneralRe: A second form from the first form, but both are active Pin
OriginalGriff10-May-18 23:47
mveOriginalGriff10-May-18 23:47 
AnswerRe: A second form from the first form, but both are active Pin
BillWoodruff12-May-18 0:00
professionalBillWoodruff12-May-18 0:00 
QuestionMultiple clicks on one button scrolls through selection choices Pin
Member 1382104810-May-18 5:19
Member 1382104810-May-18 5:19 
SuggestionRe: Multiple clicks on one button scrolls through selection choices Pin
Richard Deeming10-May-18 5:55
mveRichard Deeming10-May-18 5:55 
AnswerRe: Multiple clicks on one button scrolls through selection choices Pin
OriginalGriff10-May-18 6:06
mveOriginalGriff10-May-18 6:06 
GeneralRe: Multiple clicks on one button scrolls through selection choices Pin
Luc Pattyn10-May-18 6:24
sitebuilderLuc Pattyn10-May-18 6:24 
GeneralRe: Multiple clicks on one button scrolls through selection choices Pin
OriginalGriff10-May-18 6:44
mveOriginalGriff10-May-18 6:44 
GeneralRe: Multiple clicks on one button scrolls through selection choices Pin
Luc Pattyn10-May-18 6:55
sitebuilderLuc Pattyn10-May-18 6:55 
GeneralRe: Multiple clicks on one button scrolls through selection choices Pin
OriginalGriff10-May-18 7:59
mveOriginalGriff10-May-18 7:59 
AnswerRe: Multiple clicks on one button scrolls through selection choices Pin
BillWoodruff10-May-18 10:56
professionalBillWoodruff10-May-18 10:56 
AnswerRe: Multiple clicks on one button scrolls through selection choices Pin
Pete O'Hanlon10-May-18 21:45
mvePete O'Hanlon10-May-18 21:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.