Autor Beitrag
alegria
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 79



BeitragVerfasst: Fr 07.05.10 12:54 
Hey,

also ich brauch sowas wie
ausblenden C#-Quelltext
1:
List<string,int,string>()					

komm da aber nicht weiter...

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
public class myType
        {
            public myType(String strA, itn intA, String strB)            {
                StrA = strA;
                IntA = intA;
                StrB = strB;
            }

            public string StrA { get; private set; }
            public int IntA { get; private set; }
            public string StrB { get; private set; }
        }

und dann mittels List<myType>() bzw. myList.Add(new myType) ist zwar okay, aber ich habe dann nicht mehr diverse Listfunktionen wie contains() etc. Wie kann man aber denn nun (ohne schleife) beispielsweise komplett über alle StrB gucken ob xyz vorhanden ist oder nicht. Oder aber über alle IntA die Summe bilden etc.

Ich kann mir nicht vorstellen das das mit dem eigenen Typ so kompliziert ist - wahrscheinlich ist nur mein Ansatz etwas falsch!?

Danke im Voraus für Eure Tips...
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Fr 07.05.10 13:34 
Hallo,

ich bin mir nicht ganz sicher ob ich dich richtig verstanden habe, aber ich könnte mir vorstellen das es dir folgendes helfen könnte. Wenn du nicht eine Liste von MyType machst, sondern einen MyType der eine Liste von stra, inta und strb hat. Dann wären deine Anforderungen umgesetzt:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
public class MyType
{
   public List<string> StrA { get; private set; }
   public List<string> StrB { get; private set; }
   public List<int> Int { get; private set; }

   public MyType()
   {
      StrA = new List<string>();
      StrB = new List<string>();
      IntA = new List<int>();
   }
}


Und darüber wieder ein Container:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
          List<MyType> container = new List<MyType>();

            MyType itemA = new MyType();
            MyType itemB = new MyType();

            itemA.StrA.Add("blub");

            container.Add(itemA);
            container.Add(itemB);

            var strA = from c in container
                       where c.StrA.Contains("blub")
                       select c;


Gruß
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Fr 07.05.10 13:42 
Obwohl.. das macht keinen Sinn :D

Aber was vermisst du den bei deiner Klasse?
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
    public class MyType
    {
        public string StrA { get; set; }
        public string StrB { get; set; }
        public int IntA { get; set; }
    }


Kann doch alles?

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
            List<MyType> container = new List<MyType>();

            MyType itemA = new MyType();
            MyType itemB = new MyType();

            container.Add(new MyType { IntA = 3, StrA = "blub" });
            container.Add(new MyType { IntA = 1, StrA = "blah" });
            container.Add(new MyType { IntA = 7, StrA = "toll" });

            int sum = 0;
            container.ForEach( t => sum += t.IntA );

            var strA = from c in container
                       where c.StrA.Equals("blub")
                       select c;
alegria Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 79



BeitragVerfasst: Fr 07.05.10 13:59 
cool, cool... Das mit dem expliziten Ansprechen der Variablen durch Deklaration innerhalb der Klammmern kannte ich noch gar nicht (new MyType { IntA = 3, StrA = "blub" })

Hier mal mein Programm mit den Fragen in den Kommentaren...

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
class Program
    {
        static void Main(string[] args)
        {
            List<MyType> container = new List<MyType>();

            // warum geht nicht: 
            //container.Add(new MyType(123,"okay");
            container.Add(new MyType { IntA = 1, StrA = "alpha" });
            container.Add(new MyType { IntA = 5, StrA = "gamma" });
            container.Add(new MyType { IntA = 98, StrA = "omega" });

            // so würde ich das gern machen - geht nur nicht!
            //Console.WriteLine(container.StrA.Contains("gamma").ToString());

            // ist zwar letztlich auch wieder Schleife aber funktioniert wenigstens! :)
            int sum = 0;
            container.ForEach(t => sum += t.IntA);
            Console.WriteLine(sum.ToString());

            Console.ReadLine();
        }

        public class MyType
        {
            public string StrA { get; set; }
            public int IntA { get; set; }
        }
    }
alegria Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 79



BeitragVerfasst: Fr 07.05.10 14:03 
Zitat:
// warum geht nicht:
//container.Add(new MyType(123,"okay");

Okay, das bekommt man mittels Konstruktur hin!

ausblenden C#-Quelltext
1:
2:
3:
4:
public MyType2(string a, int b) {
                StrA = a;
                IntA = b;
            }


Aber wie kann ich meine Liste container SPALTENweise betrachten, auslesen etc. und nicht zeilenweise auf die einzelnen Variablen zugreifen???
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Fr 07.05.10 14:15 
Ja, aber du brauchst eben keine expliziten Konstruktoren mehr (nennt sich Parameters Initialization oder so).
Zitat:
// warum geht nicht:
//container.Add(new MyType(123,"okay");

Du brauchst nur eine geschweifte Klammer und Propertynamen = Value:
ausblenden C#-Quelltext
1:
new MyType{ IntA = 123, StrA = "okay" }					

Zitat:
// so würde ich das gern machen - geht nur nicht!
//Console.WriteLine(container.StrA.Contains("gamma").ToString());

ausblenden C#-Quelltext
1:
2:
3:
4:
            var blub = (from c in container
                       where c.StrA.Equals("blub")
                       select c).FirstOrDefault();
            Console.WriteLine(blub.ToString());

Zitat:
// ist zwar letztlich auch wieder Schleife aber funktioniert wenigstens! :)
int sum = 0;
container.ForEach(t => sum += t.IntA);


ausblenden C#-Quelltext
1:
int summe = container.Sum( t => t.IntA);					


Aber letztendlich ist wohl alles eine Schleife .. liegt wohl am IEnumerable ;)
alegria Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 79



BeitragVerfasst: Fr 07.05.10 16:05 
Danke!