Autor Beitrag
bfmeb
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 28



BeitragVerfasst: Di 18.08.09 10:31 
Hallo,

ich hab hier ein ganz simples Array aus Werten mit dem Datentyp Ulong.
ausblenden C#-Quelltext
1:
ulong[] ignoreIds = new ulong[] { "3000101""3000301""3000302""1001""1010""1010010""1010030" };					


Nun würde ich gern false zurückgeben wenn ein Parameter meiner Methode denselben Wer hat wie ein Wert aus meinem Array.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
private bool CheckIgnoreIds(ulong id)
        {
            ulong[] ignoreIds = new ulong[] { "3000101""3000301""3000302""1001""1010""1010010""1010030" };
            if (ignoreIds.ToString().Contains(id))
                return false;
            return true;        
        }


Der Code ist so nicht korrekt, ich weiß blo nicht so recht ob ich vorher in String konvertieren muss, dann Contain() anwende...(jedoch wird true zurückgegeben wenn dann ein Zeichen der Zeichenfolge übereinstimmt)
Ich will zwingend die Contains()-Methode nutzen...brauch ich da dann eine ArrayList?
Sorry für die Anfängerfrage, aber komme grad nicht so recht weiter...

danke vielmals
bfmeb Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 28



BeitragVerfasst: Di 18.08.09 14:00 
Ich habe das jetzt mit einer ArrayList gemacht...
Wieso funktioniert dieser Code nicht? Ich steig einfach nicht dahinter, was mache ich falsch. Auch wenn die variable id genau den Wert annimmt wie er im Array eingetragen ist wird trotzdem true zurückgegeben.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
 
private bool CheckIgnoreIds(ulong id)
        {
            ArrayList ignoreIds = new ArrayList();
            ignoreIds.Add(3000101); ignoreIds.Add(3000101); ignoreIds.Add(3000302); ignoreIds.Add(1001);
            ignoreIds.Add(1010); ignoreIds.Add(1010010); ignoreIds.Add(1010030);
            if (ignoreIds.Contains(id))
                return false;
            return true;
        }


Kann mir jemnand helfen?
Flitzs
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 123
Erhaltene Danke: 7

Win7 x64/86 WinServer 2008 R2 x64
C#/C++/C VS2010
BeitragVerfasst: Di 18.08.09 14:42 
Hallo,

wenn du dir mal ansiehst welchen Typ die Elemente in der ArrayList haben, wirst du feststellen, dass diese als Int32 in der Liste sind.

Daher wäre dein Code so korrekt:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
  private bool CheckIgnoreIds(ulong id)
        {
            ArrayList ignoreIds = new ArrayList();
            ignoreIds.Add(3000101); ignoreIds.Add(3000302); ignoreIds.Add(1001);
            ignoreIds.Add(1010010); ignoreIds.Add(1010030);
            if (ignoreIds.Contains((Int32)id))
                return false;
            return true;
        }

Man könnte auch gleich den Typ des Paramters ändern.

Allerdings solltest du lieber List<T> anstatt der ArrayList nehmen und die doppelten Elemente entfernen:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
 private bool CheckIgnoreIds(ulong id)
        {
            List<ulong> ignoreIds = new List<ulong>() { 30001013000302100110100101010030 };
            return !ignoreIds.Contains(id);            
        }


mfg Flitzs


Zuletzt bearbeitet von Flitzs am Di 18.08.09 15:02, insgesamt 1-mal bearbeitet
bfmeb Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 28



BeitragVerfasst: Di 18.08.09 14:53 
AAAhhhh stimmt..
Och manno wieso bin ich da niciht selber drauf gekommen, sowas dämliches.

Danke VIELMALS für die schnelle Hilfe :)