Konvertierungen mit System.Convert
Autor: Dr. Holger Schwichtenberg
' ============================ ' .NET-Code-Beispiel in Visual Basic .NET ' Konvertierungen mit System.Convert ' (C) Holger@Schwichtenberg.de ' ============================ Sub konvertieren() Dim s As String Dim dt As Date Dim b As Boolean Dim i As Int32 Dim d As Double ' Double -> String say("Double -> String") d = 12.34 say(d) s = System.Convert.ToString(d) say(s) ' String -> Double say("String -> Double") d = System.Convert.ToDouble(s) say(d) ' Boolean -> String say("Boolean -> String") b = True s = System.Convert.ToString(b) say(s) ' String -> Boolean say("String -> Boolean") b = System.Convert.ToBoolean("False") ' nur "True" oder "False" say(b) ' Integer -> Boolean say("Integer -> Boolean (-1)") i = -1 b = System.Convert.ToBoolean(i) say(b) ' liefert True say("Integer -> Boolean (0)") i = 0 b = System.Convert.ToBoolean(i) say(b) ' liefert True say("Integer -> Boolean (2)") i = 2 b = System.Convert.ToBoolean(i) say(b) ' liefert True ' String -> Date say("String -> Date") s = "8/1/1972" dt = System.Convert.ToDateTime(s) say(dt) ' liefert 8.1.1972 s = "1.8.1972" dt = System.Convert.ToDateTime(s) say(dt) ' liefert 1.8.1972 ' Date -> String say("Date -> String") dt = #8/1/1972# say(dt) ' liefert 1.8.1972 s = System.Convert.ToString(dt) say(dt) ' liefert 1.8.1972 dt = #1/8/1972# say(dt) ' liefert 8.1.1972 s = System.Convert.ToString(dt) say(dt) ' liefert 8.1.19 End Sub
using System; using System.Collections; using FCL_Buch; namespace FCLBuch.System { public class Samples_Datentypen { public void konvertieren() { // Konvertierungen mit System.Convert // Double -> String FclOutput.PrintOut("Double -> String"); double d = 12.34; FclOutput.PrintOut(d.ToString()); string s = System.Convert.ToString(d); FclOutput.PrintOut(s); // String -> Double FclOutput.PrintOut("String -> Double"); d = System.Convert.ToDouble(s); FclOutput.PrintOut(d.ToString()); // Boolean -> String FclOutput.PrintOut("Boolean -> String"); bool b = true; s = System.Convert.ToString(b.ToString()); FclOutput.PrintOut(s); // String -> Boolean FclOutput.PrintOut("String -> Boolean"); b = System.Convert.ToBoolean("false"); // nur "True" oder "False" FclOutput.PrintOut(b.ToString()); // Integer -> Boolean FclOutput.PrintOut("Integer -> Boolean (-1)"); int i = -1; b = System.Convert.ToBoolean(i); FclOutput.PrintOut(b.ToString()); // liefert True FclOutput.PrintOut("Integer -> Boolean (0)"); i = 0; b = System.Convert.ToBoolean(i); FclOutput.PrintOut(b.ToString()); // liefert True FclOutput.PrintOut("Integer -> Boolean (2)"); i = 2; b = System.Convert.ToBoolean(i); FclOutput.PrintOut(b.ToString()); // liefert True // String -> Date FclOutput.PrintOut("String -> Date"); s = "08/01/1972"; DateTime dt = System.Convert.ToDateTime(s); FclOutput.PrintOut(dt.ToShortDateString()); // liefert 8.1.1972 s = "01.08.1972"; dt = System.Convert.ToDateTime(s); FclOutput.PrintOut(dt.ToShortDateString()); // liefert 1.8.1972 } } }
Liste aller Codebeispiele Definition '.NET Framework Class Library' Verfügbarkeit der Klasse 'System.Convert' Übersicht über den FCL-Namensraum 'System' .NET & Visual Studio Community Portal