Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a question about scope.</p> <p>Basicly, in C#, when you open a new set of <code>{}</code>, you declare a new scope. Variable create within that scope, are destroyed when exiting it. It is a fairly simplify explanation, not fully accurate, but it is an easy one to understand.</p> <pre><code>{ var testVariable = "blah"; //set cache key and query based their being a craft name if(testVariable.Length &gt; 0) { var db = Database.Open("Connection"); // Create a new variable. var selectedData = db.Query("SELECT * FROM Products"); } // Variable doesn't exist anymore. } </code></pre> <p>To fix it:</p> <pre><code>{ var testVariable = "blah"; // Create variable outside the if scope var selectedData = null; // Won't compile, compiler cannot find valid variable type. //set cache key and query based their being a craft name if(testVariable.Length &gt; 0) { var db = Database.Open("Connection"); // Assign a value to a variable selectedData = db.Query("SELECT * FROM Products"); } // Variable still exist! } // Here, variable would cease to exist. :( </code></pre> <p>But here, the code won't compile, because the compile doesn't know what type <code>selectedData</code> is, because I assign it <code>null</code>  at its creation. So, let's say <code>selectedData</code> is of type <code>Data</code>:</p> <pre><code>{ var testVariable = "blah"; // Create variable outside the if scope Data selectedData = null; // Now it compiles. :) //set cache key and query based their being a craft name if(testVariable.Length &gt; 0) { var db = Database.Open("Connection"); // Assign a value to a variable selectedData = db.Query("SELECT * FROM Products"); } // Variable still exist! } // Here, variable would cease to exist. :( </code></pre> <p>After that, you can do <code>if (selectedData != null)</code> to know if the data was correctly queried.</p>
    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.
    1. This table or related slice is empty.
    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