Note that there are some explanatory texts on larger screens.

plurals
  1. PODo I really have to give an initial value to all my variables?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/1423437/why-do-i-have-to-assign-a-value-to-an-int-in-c-sharp-when-defaults-to-0">Why do I have to assign a value to an int in C# when defaults to 0?</a> </p> </blockquote> <p>I'm just starting to learn C# by writing a contrived application called Journal. In a function for parsing journal files I've declared the variable <code>DateTime currentEntryDate</code>. It won't get a value until I reach a line that defines a new entry. The <em>second</em> time I reach an entry line, the variable will be used to create an instance of class <code>JournalEntry</code> for the previous entry.</p> <p>The problem is that the code for the usage of the variable won't compile:</p> <blockquote> <p>Use of unassigned local variable 'currentEntryDate'</p> </blockquote> <p>This makes no sense to me. Do I really have to give a wasted initial value to my variables just to keep the compiler happy? Surely I've misunderstood something, or there's an error in my code somewhere.</p> <p>The code on Pastebin: <a href="http://pastebin.com/1JcqDzx4" rel="nofollow noreferrer">Journal.cs</a>. I've highlighted the relevant lines.</p> <p>The code:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.IO; namespace Journal { class Journal { public List&lt;JournalEntry&gt; Entries; private static readonly string EntryLineRegex = @"-- Entry: (?&lt;title&gt;.*) \((?&lt;year&gt;\d{4})-(?&lt;month&gt;\d{2})" + @"-(?&lt;day&gt;\d{2})\)"; public static Journal FromFile(string filePath) { Journal returnValue = new Journal(); StreamReader fileReader = new StreamReader(filePath); // Prepare variables for parsing the journal file. bool hitFirstEntry = false; DateTime currentEntryDate; string currentEntryTitle; StringBuilder currentEntryText = new StringBuilder(); // Prepare a regular expression for the entry lines. Regex entryLineRegex = new Regex(EntryLineRegex); while (!fileReader.EndOfStream) { string line = fileReader.ReadLine(); if (line.StartsWith("--")) { // Is this the first entry encountered? If so, don't try to // process the previous entry. if (!hitFirstEntry) { hitFirstEntry = true; } else { // Create a JournalEntry with the current entry, then // reset for the next entry. returnValue.Entries.Add( new JournalEntry( currentEntryText.ToString(), currentEntryDate ) ); currentEntryDate = new DateTime(); currentEntryText.Clear(); } // Extract the new entry title and date from this line and // save them. Match entryMatch = entryLineRegex.Match(line); GroupCollection matches = entryMatch.Groups; currentEntryDate = new DateTime( Convert.ToInt16(matches["year"].Value), Convert.ToInt16(matches["month"].Value), Convert.ToInt16(matches["day"].Value) ); currentEntryTitle = matches["title"].Value; } else { currentEntryText.Append(line); } } return returnValue; } } class JournalEntry { public string Text; public DateTime EntryDate; public JournalEntry(string text, DateTime entryDate) { this.Text = text; this.EntryDate = entryDate; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload