Announcement

Collapse
No announcement yet.

DateTime-Member kann null sein - wie setz ich das um?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • DateTime-Member kann null sein - wie setz ich das um?

    Hallo zusammen,
    ich bin hier ein bisschen verwirrt und kriegs einfach net hin.

    Ich habe ein Objekt Kunde. Das Objekt stellt quasi ein Abbild einer Tabelle auf dem DB-Server MSSQL7 dar.
    Jetzt hab ich das z.B. das Feld Geburtstag.

    Mein C#-Objekt hat jetz ein Member

    private DateTime _geburtstag;

    Die Property les und schreib ich so:

    public Datetime Geburtstag
    {
    get {return _geburtstag;}
    set {_geburtstag=value;}
    }

    Das der Compiler da meckert ist mir schon klar, wenn ich keinen Eintrag übergebe bzw. null übergebe.
    Aber wie und an welcher Stelle mach ich das.
    Danke für jeden Tipp.
    Katja

  • #2
    Hallo,
    tja - wenn man das typisierte DataSet verwenden würde, hätte Visual Studio den Quelltext für die Behandlung des Sonderfalls DBNull automatisch generiert :-)

    Angenommen, die Tabelle sieht in der MS SQL Server-Datenbank so aus:

    <div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"><p style="margin: 0px;"><span style="color: blue;">USE </span>tempdb</p><p style="margin: 0px;">GO</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;"><span style="color: blue;">CREATE TABLE </span>Kunde</p><p style="margin: 0px;">(</p><p style="margin: 0px;">&nbsp; id&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">INT </span>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <span style="color: blue;">NOT NULL IDENTITY PRIMARY KEY</span>,</p><p style="margin: 0px;">&nbsp; geburtstag <span style="color: blue;">SMALLDATETIME NULL</span></p><p style="margin: 0px;">)</p><p style="margin: 0px;">GO</p></div>

    In diesem Fall generiert Visual Studio für diese Mini-Tabelle 787 Programmzeilen, darunter die folgende Initialisierung der Kunden-Klasse:

    <div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; [System.Diagnostics.<span style="color: teal;">DebuggerNonUserCodeAttribute</span>()]</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">void</span> InitClass() {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.columnid = <span style="color: blue;">new</span> System.Data.<span style="color: teal;">DataColumn</span>(<span style="color: maroon;">"id"</span>, <span style="color: blue;">typeof</span>(<span style="color: blue;">int</span>), <span style="color: blue;">null</span>, System.Data.<span style="color: teal;">MappingType</span>.Element);</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">base</span>.Columns.Add(<span style="color: blue;">this</span>.columnid);</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.columngeburtstag = <span style="color: blue;">new</span> System.Data.<span style="color: teal;">DataColumn</span>(<span style="color: maroon;">"geburtstag"</span>, <span style="color: blue;">typeof</span>(System.<span style="color: teal;">DateTime</span>), <span style="color: blue;">null</span>, System.Data.<span style="color: teal;">MappingType</span>.Element);</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">base</span>.Columns.Add(<span style="color: blue;">this</span>.columngeburtstag);</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.Constraints.Add(<span style="color: blue;">new</span> System.Data.<span style="color: teal;">UniqueConstraint</span>(<span style="color: maroon;">"Constraint1"</span>, <span style="color: blue;">new</span> System.Data.<span style="color: teal;">DataColumn</span>[] {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.columnid}, <span style="color: blue;">true</span>));</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.columnid.AutoIncrement = <span style="color: blue;">true</span>;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.columnid.AllowDBNull = <span style="color: blue;">false</span>;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.columnid.ReadOnly = <span style="color: blue;">true</span>;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.columnid.Unique = <span style="color: blue;">true</span>;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p></div>

    Außerdem werden 2 Methoden generiert, die den NULL-Sonderfall auslesen bzw. schreiben können:

    <div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; [System.Diagnostics.<span style="color: teal;">DebuggerNonUserCodeAttribute</span>()]</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">bool</span> IsgeburtstagNull() {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>.IsNull(<span style="color: blue;">this</span>.tableKunde.geburtstagColumn);</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; [System.Diagnostics.<span style="color: teal;">DebuggerNonUserCodeAttribute</span>()]</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> SetgeburtstagNull() {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>[<span style="color: blue;">this</span>.tableKunde.geburtstagColumn] = System.<span style="color: teal;">Convert</span>.DBNull;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p></div>

    Wenn man die Datenbanktabelle in eine von Hand geschriebene Klasse verpacken will, muss man alle die Schritte nachholen, die Visual Studio automatisch macht.

    ---

    Zurück zur eigentlichen Frage. Anstelle von <i>private DateTime _geburtstag;</i> muss die Membervariable vom generischen Typ <b>Nullable&lt;T&gt;</b> sein, damit der NULL-Sonderfall in dieser Variable gespeichert werden kann. Die Sprache C# sieht <b>?</b> als Shortcut für Nullable&lt;T&gt; vor. Das folgende Beispiel demonstriert dies (das Fragezeichen nach DateTime ist bei der Deklaration der Member-Variablen entscheidend):

    <div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: teal;">DateTime</span><b>?</b> aDT;</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">void</span> button1_Click(<span style="color: blue;">object</span> sender, <span style="color: teal;">EventArgs</span> e)</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; aDT = <span style="color: teal;">DateTime</span>.Today;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (!aDT.HasValue)</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: teal;">MessageBox</span>.Show(<span style="color: maroon;">"Test 1: Null"</span>);</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">// Null in der DateTime-Membervariablen ablegen</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; aDT = <span style="color: blue;">null</span>;</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (!aDT.HasValue)</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: teal;">MessageBox</span>.Show(<span style="color: maroon;">"Test 2: Null"</span>);</p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p></div&gt

    Comment

    Working...
    X