Autor Beitrag
Help_Me
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 31



BeitragVerfasst: Mi 28.04.10 11:16 
Hallo!
Mich wundert es, dass wenn ich beim unteren Code die Konvertierung weg lasse, ich eine Meldung bekomme,
dass "object nicht in double konvertiert werden kann". Dabei wird doch davor geprüft, ob es sich um
ein double handelt.

ausblenden C#-Quelltext
1:
2:
3:
4:
if (obj is double)
{
    aktuellesDatum[j] = DateTime.FromOADate(Convert.ToDouble(obj));
}


Wie müsste den der Code ohne Konvertierung richtig lauten? Ich dachte ja  obj as double
aber das funktioniert auch nicht.

Grüße
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Mi 28.04.10 11:24 
Leider verbirgst du von welchem Datentyp obj ist. Da es obj heißt vermute ich mal es ist ein object.

Wenn du die Abfrage auf is double machst weißt du zwar, dass es ein double ist.. der Kompilier aber nicht. Deshalb musst du den cast (Convert) machen.

As macht im Prinzip auch ein cast nur, dass es bei einem falschen Datentyp keine Fehlermeldung wirft, sondern den Typ null zurück gibt:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
double objAsDouble = obj as double;

if (objAsDouble != null)
{
   aktuellesDatum[j] = DateTIme.FromOADate(objAsDouble);
}

bzw.
ausblenden C#-Quelltext
1:
2:
3:
4:
if (obj is double)
{
   aktuellesDatum[j] = DateTime.FromOADate((double) obj);
}


Gruß
Help_Me Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 31



BeitragVerfasst: Mi 28.04.10 12:04 
Super...vielen Dank!