<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Any C# coders? I need some help</title>
		<link>http://www.allegro.cc/forums/view/617240</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 01 Feb 2018 23:48:38 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m way, way, way too tired, and can&#39;t figure out why this part of my code keeps throwing the following exception at Line 21: </p><pre>System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=TANZ_Database</pre><p>

</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">using</span> System<span class="k2">;</span>
<span class="number">  2</span><span class="k1">using</span> System.Collections.Generic<span class="k2">;</span>
<span class="number">  3</span>
<span class="number">  4</span><span class="k1">namespace</span> TANZ_Database
<span class="number">  5</span><span class="k2">{</span>
<span class="number">  6</span>    <span class="k1">enum</span> Types <span class="k2">{</span>Int, String, List, Date<span class="k2">}</span><span class="k2">;</span>
<span class="number">  7</span>
<span class="number">  8</span>    <span class="k1">class</span> Field
<span class="number">  9</span>    <span class="k2">{</span>
<span class="number"> 10</span>        <span class="k1">public</span> String Name<span class="k2">;</span>
<span class="number"> 11</span>        <span class="k1">public</span> Types Type<span class="k2">;</span>
<span class="number"> 12</span>        <span class="k1">public</span> List<span class="k3">&lt;</span>string&gt; Values<span class="k2">;</span>
<span class="number"> 13</span>    <span class="k2">}</span>
<span class="number"> 14</span>
<span class="number"> 15</span>    <span class="k1">class</span> DataField
<span class="number"> 16</span>    <span class="k2">{</span>
<span class="number"> 17</span>        <span class="k1">public</span> <span class="k1">static</span> Field<span class="k2">[</span><span class="k2">]</span> fields <span class="k3">=</span> <span class="k1">new</span> Field<span class="k2">[</span><span class="n">13</span><span class="k2">]</span><span class="k2">;</span>
<span class="number"> 18</span>
<span class="number"> 19</span>        <span class="k1">public</span> <span class="k1">void</span> PopulateDataFields<span class="k2">(</span><span class="k2">)</span>
<span class="number"> 20</span>        <span class="k2">{</span>
<span class="number"> 21</span>            fields<span class="k2">[</span><span class="n">0</span><span class="k2">]</span>.Name <span class="k3">=</span> <span class="s">"Membership Types"</span><span class="k2">;</span>
<span class="number"> 22</span>            fields<span class="k2">[</span><span class="n">0</span><span class="k2">]</span>.Type <span class="k3">=</span> Types.List<span class="k2">;</span>
<span class="number"> 23</span>            fields<span class="k2">[</span><span class="n">0</span><span class="k2">]</span>.Values.Add<span class="k2">(</span><span class="s">"Unpaid"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 24</span>            fields<span class="k2">[</span><span class="n">0</span><span class="k2">]</span>.Values.Add<span class="k2">(</span><span class="s">"Paid"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 25</span>            fields<span class="k2">[</span><span class="n">0</span><span class="k2">]</span>.Values.Add<span class="k2">(</span><span class="s">"Lifetime"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 26</span>
<span class="number"> 27</span>            fields<span class="k2">[</span><span class="n">1</span><span class="k2">]</span>.Name <span class="k3">=</span> <span class="s">"First Name[s]"</span><span class="k2">;</span>
<span class="number"> 28</span>            fields<span class="k2">[</span><span class="n">1</span><span class="k2">]</span>.Type <span class="k3">=</span> Types.String<span class="k2">;</span>
<span class="number"> 29</span>
<span class="number"> 30</span>            fields<span class="k2">[</span><span class="n">2</span><span class="k2">]</span>.Name <span class="k3">=</span> <span class="s">"Surname"</span><span class="k2">;</span>
<span class="number"> 31</span>            fields<span class="k2">[</span><span class="n">2</span><span class="k2">]</span>.Type <span class="k3">=</span> Types.String<span class="k2">;</span>
<span class="number"> 32</span>
<span class="number"> 33</span>            fields<span class="k2">[</span><span class="n">3</span><span class="k2">]</span>.Name <span class="k3">=</span> <span class="s">"Address"</span><span class="k2">;</span>
<span class="number"> 34</span>            fields<span class="k2">[</span><span class="n">3</span><span class="k2">]</span>.Type <span class="k3">=</span> Types.String<span class="k2">;</span>
<span class="number"> 35</span>
<span class="number"> 36</span>            fields<span class="k2">[</span><span class="n">4</span><span class="k2">]</span>.Name <span class="k3">=</span> <span class="s">"Post Code"</span><span class="k2">;</span>
<span class="number"> 37</span>            fields<span class="k2">[</span><span class="n">4</span><span class="k2">]</span>.Type <span class="k3">=</span> Types.Int<span class="k2">;</span>
<span class="number"> 38</span>
<span class="number"> 39</span>            fields<span class="k2">[</span><span class="n">5</span><span class="k2">]</span>.Name <span class="k3">=</span> <span class="s">"Date of Birth"</span><span class="k2">;</span>
<span class="number"> 40</span>            fields<span class="k2">[</span><span class="n">5</span><span class="k2">]</span>.Type <span class="k3">=</span> Types.Date<span class="k2">;</span>
<span class="number"> 41</span>
<span class="number"> 42</span>        <span class="k2">}</span>
<span class="number"> 43</span>    <span class="k2">}</span>
<span class="number"> 44</span><span class="k2">}</span>
</div></div><p>

Any assistance would be great.  I&#39;ll check back in after I get some sleep.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Fri, 26 Jan 2018 17:39:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I may be wrong, but I think C# is like Java in this respect. You can declare an array of objects, but you actually have to loop over those objects and create them.</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">public</span> <span class="k1">static</span> Field<span class="k2">[</span><span class="k2">]</span> fields <span class="k3">=</span> <span class="k1">new</span> Field<span class="k2">[</span><span class="n">13</span><span class="k2">]</span><span class="k2">;</span>

<span class="k1">for</span> <span class="k2">(</span><span class="k1">int</span> i <span class="k3">=</span> <span class="n">0</span> <span class="k2">;</span> i <span class="k3">&lt;</span> <span class="n">13</span> <span class="k2">;</span> <span class="k3">+</span><span class="k3">+</span>i<span class="k2">)</span> <span class="k2">{</span>
   fields<span class="k2">[</span>i<span class="k2">]</span> <span class="k3">=</span> <span class="k1">new</span> Field<span class="k2">(</span>...<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 26 Jan 2018 18:30:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>yes an array in c# is just an array of pointers for reference types
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jmasterx)</author>
		<pubDate>Fri, 26 Jan 2018 18:57:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This is where C really shines. Array initialization is one of the best things about C.</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> ABC <span class="k2">{</span>
   <span class="k1">const</span> <span class="k1">char</span><span class="k3">*</span> a<span class="k2">;</span>
   <span class="k1">int</span> b<span class="k2">;</span>
   <span class="k1">float</span> c<span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>

ABC array<span class="k2">[</span><span class="n">2</span><span class="k2">]</span> <span class="k3">=</span> <span class="k2">{</span> <span class="k2">{</span><span class="s">"Hello"</span> , <span class="n">1</span> , <span class="n">0</span>.<span class="n">5f</span><span class="k2">}</span> , <span class="k2">{</span><span class="s">"World"</span> , <span class="n">2</span> , <span class="n">1</span>.<span class="n">5f</span><span class="k2">}</span> <span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>

That is one of the features of C I use the most.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 26 Jan 2018 19:05:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You can do that in D. <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Fri, 26 Jan 2018 20:24:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks guys, that was it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Fri, 26 Jan 2018 22:56:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It is possible to initialize arrays and things in C#
</p><div class="source-code snippet"><div class="inner"><pre>        <span class="k1">public</span> <span class="k1">static</span> Field<span class="k2">[</span><span class="k2">]</span> fields <span class="k3">=</span> <span class="k1">new</span> Field<span class="k2">[</span><span class="k2">]</span> <span class="k2">{</span>
            <span class="k1">new</span> Field <span class="k2">{</span>Name <span class="k3">=</span> <span class="s">"Membership Types"</span>,
                Type <span class="k3">=</span> Types.List,
                Values <span class="k3">=</span> <span class="k1">new</span> List<span class="k3">&lt;</span>string&gt; <span class="k2">{</span> <span class="s">"Unpaid"</span>, <span class="s">"Paid"</span>, <span class="s">"Lifetime"</span> <span class="k2">}</span> <span class="k2">}</span>,
            <span class="k1">new</span> Field <span class="k2">{</span>Name<span class="k3">=</span><span class="s">"First Name[s]"</span>, Type<span class="k3">=</span>Types.String<span class="k2">}</span>,
            <span class="k1">new</span> Field <span class="k2">{</span>Name<span class="k3">=</span><span class="s">"Surname"</span>, Type<span class="k3">=</span>Types.String<span class="k2">}</span>,
            <span class="k1">new</span> Field <span class="k2">{</span>Name<span class="k3">=</span><span class="s">"Post Code"</span>, Type<span class="k3">=</span>Types.String<span class="k2">}</span>,
            <span class="k1">new</span> Field <span class="k2">{</span>Name<span class="k3">=</span><span class="s">"Date of Birth"</span>, Type<span class="k3">=</span>Types.Date<span class="k2">}</span>,
        <span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>
Possibly if you wrote a constructor for <span class="source-code">Field</span> it might be made less unwieldy.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Hull)</author>
		<pubDate>Fri, 26 Jan 2018 23:32:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Since you were all so helpful, here&#39;s another little problem I&#39;ve run into. I have another function as follows:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">private</span> <span class="k1">void</span> RadioSpecificFilter_CheckedChanged<span class="k2">(</span>object sender, EventArgs e<span class="k2">)</span>
<span class="number">  2</span><span class="k2">{</span>
<span class="number">  3</span>    <span class="k1">if</span> <span class="k2">(</span>radioSpecificFilter.Checked <span class="k3">=</span><span class="k3">=</span> <span class="k1">true</span><span class="k2">)</span>
<span class="number">  4</span>    <span class="k2">{</span>
<span class="number">  5</span>        radioNoFilter.Checked <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number">  6</span>        radioRangeFilter.Checked <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number">  7</span>
<span class="number">  8</span>        foreach <span class="k2">(</span>Field f in DataField.fields<span class="k2">)</span>
<span class="number">  9</span>        <span class="k2">{</span>
<span class="number"> 10</span>            comboboxFieldNames.Items.Add<span class="k2">(</span>f.Name<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span>            <span class="c">//MessageBox.Show(f.Name);</span>
<span class="number"> 12</span>        <span class="k2">}</span>
<span class="number"> 13</span>    <span class="k2">}</span>
<span class="number"> 14</span><span class="k2">}</span>
</div></div><p>

It generates an exception at line 10, stating that f.Name is null. However, if I comment that line out and uncomment line 11, the program runs fine.  </p><p>Any takers on this one?</p><p>edit:</p><p>Changing the DataField class definition to Peter&#39;s way of initiating the class fixed this problem.  It&#39;s a shame you can&#39;t update threads to add extra credits.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Fri, 26 Jan 2018 23:54:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617240/1034842#target">LennyLen</a> said:</div><div class="quote"><p>
It&#39;s a shame you can&#39;t update threads to add extra credits. 
</p></div></div><p>
I&#39;ll take a Lifetime membership to whatever club you&#39;re writing code for <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Hull)</author>
		<pubDate>Sat, 27 Jan 2018 04:10:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617240/1034846#target">Peter Hull</a> said:</div><div class="quote"><p> I&#39;ll take a Lifetime membership to whatever club you&#39;re writing code for</p></div></div><p>It&#39;s a non-profit organization that helps people with mental health issues. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>They&#39;re actually pretty cool.  I could have produced what they need in a day just using Access, but I said I&#39;d write it for free using C#/.net as a learning exercise if they didn&#39;t mind it taking a but longer, and they replied that they&#39;re in no hurry and that they&#39;d still pay me anyway.</p><p>It&#39;ll definitely be better than what they&#39;re using now, which is a multi-page Excel spreadsheet.  I&#39;m still storing the data in an Access database, so that if they want extra features at a later date and I&#39;m not available, all their data is in a well known format.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Sat, 27 Jan 2018 07:26:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The second problem you reported: I assume the exception is being thrown from inside the Add function, with a check along the lines of<br /><span class="source-code"><span class="k1">if</span> <span class="k2">(</span>value <span class="k3">=</span><span class="k3">=</span> null<span class="k2">)</span> <span class="k1">throw</span> <span class="k1">new</span> NullReferenceException<span class="k2">(</span><span class="s">"value"</span><span class="k2">)</span><span class="k2">;</span></span><br />Meanwhile, MessageBox.Show probably just says<br /><span class="source-code"><span class="k1">if</span> <span class="k2">(</span>value <span class="k3">=</span><span class="k3">=</span> null<span class="k2">)</span> value <span class="k3">=</span> <span class="s">""</span><span class="k2">;</span></span></p><p>With regard to the first problem, you may be able to simplify things by declaring your data type as a &#39;struct&#39; instead of a &#39;class&#39;. This means it will be passed around as a value instead of a reference, which comes with all its own subtleties, most notably this one:
</p><div class="source-code snippet"><div class="inner"><pre>var value <span class="k3">=</span> myArray<span class="k2">[</span>i<span class="k2">]</span><span class="k2">;</span>
value.x<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span> <span class="c">//modify some field of the struct</span>
myArray<span class="k2">[</span>i<span class="k2">]</span> <span class="k3">=</span> value<span class="k2">;</span> <span class="c">//this is necessary because 'value' was a copy</span>
</pre></div></div><p>
But the advantage is, when you make an array of them, they exist already. Think of it this way: a struct when converted to C will never have * or &amp;, but a class will always have it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bruce Perry)</author>
		<pubDate>Mon, 29 Jan 2018 16:24:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617240/1034907#target">Bruce Perry</a> said:</div><div class="quote"><p> The second problem you reported: I assume the exception is being thrown from inside the Add function</p></div></div><p>That wouldn&#39;t explain why the watchlist was reporting that f.name had a value of null though, would it?  The traceback also didn&#39;t include the .add function.</p><p>That bit about structs is useful info. Thanks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Mon, 29 Jan 2018 16:31:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617240/1034908#target">LennyLen</a> said:</div><div class="quote"><p> That wouldn&#39;t explain why the watchlist was reporting that f.name had a value of null though, would it?
</p></div></div><p>Actually yes, that&#39;s exactly what I would expect. You&#39;re allowed to use f.name because f is not null. You&#39;re also allowed to pass null to a function.
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p> The traceback also didn&#39;t include the .add function.
</p></div></div><p>Perhaps it&#39;s a release build, or perhaps there&#39;s something about system libraries being omitted which I&#39;m not entirely familiar with...?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bruce Perry)</author>
		<pubDate>Wed, 31 Jan 2018 20:42:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617240/1034957#target">Bruce Perry</a> said:</div><div class="quote"><p> Actually yes, that&#39;s exactly what I would expect. You&#39;re allowed to use f.name because f is not null.</p></div></div><p>What I meant was that when I used the message box, it didn&#39;t display null, it displayed the name correctly, which is shouldn&#39;t? if it was set to null.</p><p>It&#39;s all working now though, which is the important part. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Thu, 01 Feb 2018 02:30:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="https://www.allegro.cc/forums/thread/617240/1034833#target">Edgar Reynaldo</a> said:</div><div class="quote"><p>
I may be wrong, but I think C# is like Java in this respect. You can declare an array of objects, but you actually have to loop over those objects and create them.</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">public</span> <span class="k1">static</span> Field<span class="k2">[</span><span class="k2">]</span> fields <span class="k3">=</span> <span class="k1">new</span> Field<span class="k2">[</span><span class="n">13</span><span class="k2">]</span><span class="k2">;</span>

<span class="k1">for</span> <span class="k2">(</span><span class="k1">int</span> i <span class="k3">=</span> <span class="n">0</span> <span class="k2">;</span> i <span class="k3">&amp;</span>lt<span class="k2">;</span> <span class="n">13</span> <span class="k2">;</span> <span class="k3">+</span><span class="k3">+</span>i<span class="k2">)</span> <span class="k2">{</span>
   fields<span class="k2">[</span>i<span class="k2">]</span> <span class="k3">=</span> <span class="k1">new</span> Field<span class="k2">(</span>...<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div></div><p>

Just a note, this is only true if <span class="source-code">Field</span> is a &quot;reference type&quot; (i.e., an instance of a <span class="source-code"><span class="k1">class</span></span>). As Bruce alluded to, a &quot;value type&quot; (primitives, structs) work differently. Reference types are implicitly passed by reference (i.e., similar to pointers). Value types are implicitly passed by value (copied). If you create an array of integers in C# you do not have to allocate memory for each element of the array. Similarly, if you create an array of structs you don&#39;t need to allocate memory for each of them. They will be implicitly allocated. A value type variable cannot be null. Only reference types can be null because the value null means a &quot;null reference&quot;. When you allocate an array of reference types you are allocating the memory for the reference itself, and that is initialized to <span class="source-code">null</span> (sort of like allocating memory for a pointer versus the data that it points to). You need to allocate the objects themselves because there&#39;s no guarantee you ever wanted an object (depending on the program, having nulls in the array may be normal and expected).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Thu, 01 Feb 2018 05:13:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617240/1034964#target">bamccaig</a> said:</div><div class="quote"><p> Reference types are implicitly passed by reference (i.e., similar to pointers). Value types are implicitly passed by value (copied). If you create an array of integers in C# you do not have to allocate memory for each element of the array. Similarly, if you create an array of structs you don&#39;t need to allocate memory for each of them. They will be implicitly allocated. A value type variable cannot be null. Only reference types can be null because the value null means a &quot;null reference&quot;. When you allocate an array of reference types you are allocating the memory for the reference itself, and that is initialized to null (sort of like allocating memory for a pointer versus the data that it points to). You need to allocate the objects themselves because there&#39;s no guarantee you ever wanted an object (depending on the program, having nulls in the array may be normal and expected).</p></div></div><p>That&#39;s a nice explanation.  It&#39;s been 25 years now since I studied Computer Science.  I&#39;m a little rusty around the edges. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>edit:</p><p>Upon reading up on structs, it appears that part of what you said isn&#39;t correct.  A struct is a value type, but it can be null -</p><div class="quote_container"><div class="title">https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/structs said:</div><div class="quote"><p>A struct can be used as a nullable type and can be assigned a null value.</p></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Thu, 01 Feb 2018 05:36:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="https://www.allegro.cc/forums/thread/617240/1034965#target">LennyLen</a> said:</div><div class="quote"><p>
Upon reading up on structs, it appears that part of what you said isn&#39;t correct.  A struct is a value type, but it can be null -</p><div class="quote_container"><div class="title">https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/structs said:</div><div class="quote"><p>A struct can be used as a nullable type and can be assigned a null value.</p></div></div><p>
</p></div></div><p>

That&#39;s a very misleading statement.</p><div class="source-code"><div class="toolbar"><span class="name">Nullable.cs</span><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">using</span> System<span class="k2">;</span>
<span class="number">  2</span>
<span class="number">  3</span><span class="k1">namespace</span> Application
<span class="number">  4</span><span class="k2">{</span>
<span class="number">  5</span>    <span class="k1">class</span> Program
<span class="number">  6</span>    <span class="k2">{</span>
<span class="number">  7</span>        <span class="k1">static</span> <span class="k1">int</span> Main<span class="k2">(</span>string<span class="k2">[</span><span class="k2">]</span> args<span class="k2">)</span>
<span class="number">  8</span>        <span class="k2">{</span>
<span class="number">  9</span>            Foo foo <span class="k3">=</span> null<span class="k2">;</span>
<span class="number"> 10</span>
<span class="number"> 11</span>            <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 12</span>        <span class="k2">}</span>
<span class="number"> 13</span>    <span class="k2">}</span>
<span class="number"> 14</span>
<span class="number"> 15</span>    <span class="k1">struct</span> Foo
<span class="number"> 16</span>    <span class="k2">{</span>
<span class="number"> 17</span>    <span class="k2">}</span>
<span class="number"> 18</span><span class="k2">}</span>
</div></div><p>
I&#39;m in Linux right now so I&#39;m using Mono to compile, but Microsoft&#39;s compilers should also yield an error:
</p><pre class="terminal">mono-csc Nullable.cs
Nullable.cs(9,23): error CS0037: Cannot convert null to `Application.Foo&#39; because it is a value type
Compilation failed: 1 error(s), 0 warnings</pre><p>

What that statement might mean is that you can wrap structs (and other value types) up into a `Nullable&lt;T&gt;` class instance to make them nullable. This type is built right into the compiler. <span class="source-code">X?</span> is the same as `Nullable&lt;X&gt;`.</p><div class="source-code"><div class="toolbar"><span class="name">Nullable2.cs</span><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">using</span> System<span class="k2">;</span>
<span class="number">  2</span>
<span class="number">  3</span><span class="k1">namespace</span> Application
<span class="number">  4</span><span class="k2">{</span>
<span class="number">  5</span>    <span class="k1">class</span> Program
<span class="number">  6</span>    <span class="k2">{</span>
<span class="number">  7</span>        <span class="k1">static</span> <span class="k1">int</span> Main<span class="k2">(</span>string<span class="k2">[</span><span class="k2">]</span> args<span class="k2">)</span>
<span class="number">  8</span>        <span class="k2">{</span>
<span class="number">  9</span>            Foo? foo1 <span class="k3">=</span> null<span class="k2">;</span>
<span class="number"> 10</span>            Nullable<span class="k3">&lt;</span>Foo&gt; foo2 <span class="k3">=</span> null<span class="k2">;</span>
<span class="number"> 11</span>            var foo3 <span class="k3">=</span> <span class="k2">(</span>Foo?<span class="k2">)</span><span class="k1">new</span> Foo<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// Cast Foo to Nullable&lt;Foo&gt; (Foo?).</span>
<span class="number"> 12</span>            Foo foo4 <span class="k3">=</span> foo3.Value<span class="k2">;</span> <span class="c">// Access value of Nullable&lt;T&gt;.</span>
<span class="number"> 13</span>            Foo foo5 <span class="k3">=</span> <span class="k2">(</span>Foo<span class="k2">)</span>foo3<span class="k2">;</span> <span class="c">// Cast Nullable&lt;T&gt; back to T.</span>
<span class="number"> 14</span>
<span class="number"> 15</span>            <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 16</span>        <span class="k2">}</span>
<span class="number"> 17</span>    <span class="k2">}</span>
<span class="number"> 18</span>
<span class="number"> 19</span>    <span class="k1">struct</span> Foo
<span class="number"> 20</span>    <span class="k2">{</span>
<span class="number"> 21</span>    <span class="k2">}</span>
<span class="number"> 22</span><span class="k2">}</span>
</div></div><p>
Note, we get warnings because we didn&#39;t actually use the variables, but the program compiled successfully.
</p><pre class="terminal">Nullable2.cs(9,18): warning CS0219: The variable `foo1&#39; is assigned but its value is never used
Nullable2.cs(10,27): warning CS0219: The variable `foo2&#39; is assigned but its value is never used
Nullable2.cs(13,17): warning CS0219: The variable `foo5&#39; is assigned but its value is never used
Compilation succeeded - 3 warning(s)</pre><p>

Note, accessing `Nullable&lt;T&gt;.Value` will throw if there is no value. You can test it with the <span class="source-code">HasValue</span> property (but you can also compare the Nullable&lt;T&gt; to null).</p><p><b>Append:</b></p><p>Note that structs have limitations in C#. Unlike C++ where you can do all the same things, C# enforces stricter rules. This limits their usefulness. In the wild you won&#39;t encounter them often. Which is helpful since the semantics implicitly change.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Thu, 01 Feb 2018 08:37:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617240/1034968#target">bamccaig</a> said:</div><div class="quote"><p>In the wild you won&#39;t encounter them often</p></div></div><p>
Some often-used types in System are structs.. For example KeyValuePair&lt;T&gt;, and... Nullable&lt;T&gt; itself !
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Thu, 01 Feb 2018 16:59:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The change of semantics between classes and structs can be mitigated. If you&#39;re designing a struct, then make all its fields &#39;readonly&#39;. This will prevent you assigning them from anywhere except the constructors. Consequently, it will be impossible to try to update a struct without realising you are working on a copy - you can only update a value by making a new instance of the whole struct, like with java.lang.String (which has the opposite problem, being a class that wants to behave a bit like a struct).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bruce Perry)</author>
		<pubDate>Thu, 01 Feb 2018 23:48:38 +0000</pubDate>
	</item>
</rss>
