Autor Beitrag
kostonstyle
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 94



BeitragVerfasst: Do 28.01.10 12:45 
hallo miteinander
ich habe eine Frage zu List<string>, und zwar möchte ich den Index ermitteln, wo einen bestimmenten Wert beinhaltet.
die liste sieht so aus...
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
    [13]  "  </date>"  string
    [14]  "  <runTime>"  string
    [15]  "   <timeElapsed>1313</timeElapsed>"  string
    [16]  "   <timerRunning>90</timerRunning>"  string
    [17]  "   <timeToStop>120</timeToStop>"  string
    [18]  "   <numOfPackages>516</numOfPackages>"  string
    [19]  "  </runTime>"  string
    [20]  "  <user name = \"Administrator\"/>"  string
    [21]  "  <loadCellSettings name = \"STANDARD\"/>"  string
    [22]  "  <refreshDesktop>Yes</refreshDesktop>"  string
    [23]  "  <labelMonitoring>3</labelMonitoring>"  string
    [24]  " </pluParams>"  string
    [25]  " <printObjects>"  string
    [26]  "  <printObject ID = \"1\" type = \"numberField\">"  string

nun möchte ich den Index von <printObjects> herrauslesen mit Funktion FindIndex.
Wie bringe das fertig?

Gruss kostonstyle


Moderiert von user profile iconChristian S.: Topic aus C# - Die Sprache verschoben am Do 28.01.2010 um 12:12
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Do 28.01.10 13:29 
Hallo,

eine Liste hat ja schon die Methode find. Musst du nur noch implementieren wie es vergleichen soll.

Zum Beispiel mit einem Inline-Delagte.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
List<string> t = new List<string>(); ;
            int index = t.FindIndex(delegatestring item)
            {
               return item.Trim().Equals("<printObjects>"); 
            });


Aber es sieht aus als wäre der Inhalt von der Liste ein XML File. Ließt ein Xml ein mit ReadAllLines oder so???

Um eine XML-Datei zu verarbeiten gibt es viel viel bessere Methoden.

Gruß Daniel
kostonstyle Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 94



BeitragVerfasst: Do 28.01.10 14:05 
ja, es ist ein XML File, ich erzähle lieber nicht die ganze Geschichte, die Firma möchte es so..
ich muss mich halt anpassen...
kostonstyle Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 94



BeitragVerfasst: Do 28.01.10 14:21 
@danielf
erhalte eine Exception "NullReferenceException"
Danke kostonstyle
kostonstyle Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 94



BeitragVerfasst: Do 28.01.10 14:33 
entschuldige habe mich verirrt. es funktioniert.....
noch eine Frage
ausblenden C#-Quelltext
1:
2:
3:
4:
int index = this.file_value_full.FindIndex(delegate(string item)
                {
                    return item.Trim().Equals("<printObjects>");
                });

das Predicate hier ist ein Delegate, mit Delegate kann man anynome Funktion erzeugen bzw. später zuweisen.
Die Definition von Predicate sieht so aus delegate bool Predicate<T>(T obj), habe aus dem Buch :-).
Meine Frage, warum wird hier über Index geloopt? Macht das die FindIndex Funktion?


Zuletzt bearbeitet von kostonstyle am Do 28.01.10 14:44, insgesamt 1-mal bearbeitet
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Do 28.01.10 14:43 
Mit exakt diesem Beispiel und Code?

Debug doch mal rein ... was er bei Zeile 25 von equals meint und schau dir die Strings an.
kostonstyle Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 94



BeitragVerfasst: Do 28.01.10 15:32 
@danielf
dein code war richtig, ich muss mir mehr mühe geben genauer zu arbeiten....
Vielen dank für deine Mühe...