Autor Beitrag
ots_sharp
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Di 22.05.12 09:38 
Hallo,

komme bei einer trivalen Aufgabe einfach nicht zum Ergebnis. Aus einer If-Anweisung möchte einfach nur ein return bekommen.


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
this.CheckBool()


private Boolean CheckBool()
{
 bool res;

 If (zahl)
 {
  // 1. Bedingung:
  res = true
  return res;
 }
else //if (!zahl)..//
 {
  // 2. Bedingung:
  res = false
  return res;
 }
}


Gruß
Stefan
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Di 22.05.12 11:18 
Du solltest das Problem benennen.

Was ist zahl für ein Ding? Bei einem if muss da auch eine boolscher Ausdruck stehen also etwas das wahr oder falsch ergibt.
Wenn zahl ein Integer ist und du der c/c++ Definition der bool Darstellung als Integer folgst dann einfach.

ausblenden C#-Quelltext
1:
2:
3:
4:
private Boolean CheckBool()
{
    return (zahl != 0);
}
daeve
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 116
Erhaltene Danke: 3

Windows (XP Pro, 7 Ultimate x64)
C#,WPF,Java,ASP.Net, VS 2010 Ultimate (x86)
BeitragVerfasst: Mi 23.05.12 00:52 
wenn ich dich richtig verstanden habe wäre das deine lösung..

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
private Boolean CheckBool()
{
 bool res; 

 If (zahl)
 {
  // 1. Bedingung: wenn zahl=true weise res true zu
  res = true
 }
else 
 {
  // 2. Bedingung: wenn zahl=fales weise res false zu
  res = false
 }

return res;

}


aber ich weis nicht was du mit der methode erreichen willst...
Palladin007
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1282
Erhaltene Danke: 182

Windows 11 x64 Pro
C# (Visual Studio Preview)
BeitragVerfasst: Mo 28.05.12 22:20 
Wie Ralf Jansen schon gesagt hat, geht es auch viel einfacher, nämlich indem du zahl, wenn das ein boolischer Ausdruck ist, nicht in eine if-Abfrage legst, sondern direkt ausgeben lässt.

Allerdings kannst du dir die Methode dann auch im Prinzip sparen, denn der der Umfang, wie du die Methode aufrufst, ist im Endeffekt genauso groß, als wenn du den Wert von zahl direkt verwendest.

Wenn das aber kein boolischer Ausdruck ist, musst du die Zahl erst vergleichen. Ungleich 0, oder größer, als sonst was, keine Ahnung.
Aber auch dann kannst du den Vvergleich komplett nehmen und direkt verwenden, brauchst die Methode also nicht.