{"id":4439,"date":"2014-03-30T10:58:52","date_gmt":"2014-03-30T10:58:52","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-about-using-statement-collection-of-common-programming-errors\/"},"modified":"2014-03-30T10:58:52","modified_gmt":"2014-03-30T10:58:52","slug":"problem-about-using-statement-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-about-using-statement-collection-of-common-programming-errors\/","title":{"rendered":"problem about using-statement-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/a8a9524783c9b49f47b4ba57e0103dd6?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nIan Nelson<br \/>\nc# asp.net winforms using-statement<br \/>\nI&#8217;m trying to use the System.Drawing.Color namespace. I&#8217;m not able to define it at the top of the class:However, I can reference it within the class. That is, I can use this line of code, and it works:txtBox.BackColor = System.Drawing.Color.LightPink;&#8230; but I&#8217;d rather just be able to do this:txtBox.BackColor = Color.LightPink;If it&#8217;s a matter of a missing reference\/dll, why am I able to make reference to System.Drawing.Color in my code?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/dde3a5048e456c451b7c518857e1818c?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nBen Voigt<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/14a5c8e8c85744c0349f838ee23be002?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nDH_Prog<br \/>\nc# sql exception datareader using-statement<br \/>\nI have an application in which I have to get a large amount of data from DB. Since it failed to get all of those rows (it&#8217;s close to 2,000,000 rows&#8230;), I cut it in breaks, and I run each time the sql query and get only 200,000 rows each time.I use DataTable to which I enter all of the data (meaning &#8211; all 2,000,000 rows should be there).The first few runs are fine. Then it fails with the OutOfMemoryException.My code works as following:private static void RunQueryAndAddToDT(string sql, string las<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/8ba0489ea90652c08d1406f2afe2e0e2?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nDzinX<br \/>\nc# multithreading thread-safety using-statement abort<br \/>\nI&#8217;ve just finished reading &#8220;C# 4.0 in a Nutshell&#8221; (O&#8217;Reilly) and I think it&#8217;s a great book for a programmer willing to switch to C#, but it left me wondering. My problem is the definition of using statement. According to the book (p. 138),using (StreamReader reader = File.OpenText(&#8220;file.txt&#8221;)) {&#8230; }is precisely equivalent to:StreamReader reader = File.OpenText(&#8220;file.txt&#8221;); try {&#8230; } finally {if (reader != null)((IDisposable)reader).Dispose(); }Suppose, however, that this is true and that this<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/caed2b35fed7c3bfcd4ff0287c824bd7?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nKyle Uithoven<br \/>\nc# .net namespaces using-statement<br \/>\nI recently stopped using &#8220;using statements&#8221; and instead use the full namespace path of any .NET object that I call.Example:using System; namespace QuizViewer {class Class1{Console.WriteLine(&#8220;Hello World!&#8221;);} }This is what I do now.namespace QuizViewer {class Class1{System.Console.WriteLine(&#8220;Hello World!&#8221;);} }Before you ask why I do this, I am using this style so that I can see exactly where my objects are coming from and its easier when using the different Timer objects and other objects with<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/cdebdc3cb76f97ad5324da71a27d9917?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJohn Rasch<br \/>\nvisual-studio-2008 windows-7 clr using-statement directoryinfo<br \/>\nCan someone help me find a solution to the following error:&#8221;fatal error C1190: managed targeted code requires a &#8216;\/clr&#8217; option&#8221; My configuration is ..Visual studio 2008 Windows 7 Here is the code (i got by using net resources)#using &lt;mscorlib.dll&gt; using namespace System; using namespace System::IO;int main() {\/\/ Create a reference to the current directory.DirectoryInfo* di = new DirectoryInfo(Environment::CurrentDirectory);\/\/ Create an array representing the files in the current directory<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/9cc3e3bb2ff2ef30810be80ca741de70?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nmakerofthings7<br \/>\nc# .net com dispose using-statement<br \/>\nWhat is better, the using directive, or the dispose directive when finished with an object?using(FileStream fileStream = new FileStream(&#8220;logs\/myapp.log&#8221;,FileMode.Open,FileAccess.Read,FileShare.ReadWrite)){using(StreamReader streamReader = new StreamReader(fileStream)){this.textBoxLogs.Text = streamReader.ReadToEnd();}}On the other hand, when I&#8217;m dealing with System.Net.Mail, I&#8217;m told I need to Dispose() of the object to release any stray locks. Is there any consistent guidance? How do I tell wh<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7ce9a18f294e088d18deeceb65ff3df4?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nShiraz Bhaiji<br \/>\nc# mocking rhino-mocks using-statement<br \/>\nMost of the time when we use Rhino mocks it works well, but we have problems mocking objects created using the using statements.We have a WCF proxy that is implemented as follows:public class MyProxy : System.ServiceModel&lt;IMyProxy&gt;, IMyProxy {public Response DoWork(Request request){return base.Channel.DoWork(request);} }Normally in our business layer we would have a property:IProxy MyProxy;Which we could set as being the mocked interface.But when we use a using statement, our business laye<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/02fcfbf30734d0313097a15db996732d?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nnan<br \/>\nc# scope using-statement sqlconnection<br \/>\nI use the using statement for SqlConnection. It&#8217;s is good for performance because forces calling Dispose() that simply releases the connection to the pool sooner.However, I realized that object created in using cannot be redefined. I cannot do like this:using (SqlConnection connection = new SqlConnection(connectionString)){connection.Open();\/\/&#8230;connection = new SqlConnection(connectionString2);\/\/&#8230;connection = new SqlConnection(connectionString3);}I was wondering if I can replace using, and do<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/32a5e571d17473145588e0b41d7f624f?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nGDM<br \/>\nc# winforms .net-3.5 exception-handling using-statement<br \/>\nIve been doing some research today on when an how to use the &#8220;using&#8221; statement to dispose of my sql objects. However I&#8217;m still confused about when and how to catch unforseen errors. I have a simple method here and would appreciate any input on wheter its correct or I&#8217;m doing something wrong?private BindingList&lt;My_Object&gt; Search(int ID) {string strSelectStatement = &#8220;SELECT &#8216;coloumns&#8217; &#8221; +&#8221;FROM &#8216;table&#8217; &#8221; +&#8221;WHERE ID = @ID;&#8221;;DataTable dt = new DataTable();try{using (SqlConnection sqlConn = new<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/8ceec9f7f83e3c12a72b6442393bde2a?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nSwDevMan81<br \/>\nc# language-features using using-statement<br \/>\nI understand the point of &#8220;using&#8221; is to guarantee that the Dispose method of the object will be called. But how should an exception within a &#8220;using&#8221; statement be handled? If there is an exception, I need to wrap my &#8220;using&#8221; statement in a try catch. For example:Lets say there is an exception created in the creation of the object inside the using parametertry{\/\/ Exception in using parameterusing (SqlConnection connection = new SqlConnection(&#8220;LippertTheLeopard&#8221;)){connection.Open();}}catch (Excep<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/51d623f33f8b83095db84ff35e15dbe8?s=128&amp;d=identicon&amp;r=PG\" \/><br \/>\nJeff Atwood<br \/>\nc# exception using using-statement<br \/>\nDoes the using catch the exception or throw it? i.e. using (StreamReader rdr = File.OpenText(&#8220;file.txt&#8221;)) {\/\/do stuff }If the streamreader throws an exception is it caught by using or thrown so the calling function can handle it?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/297e728fc79110ee2ea25ccc69a8ffae?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJon Seigel<br \/>\nc# coding-style using-statement<br \/>\nThis question already has an answer here:returning in the middle of a using block5 answersWhich way is better practice: return a value from a method inside an using statement or declare a variable before, set it inside and return it after?public int Foo() {using(..){return bar;} }orpublic int Foo() {var b = null;using(..){b = bar;}return b; }<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/954765ec98d01561285873097362e3ee?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMichael Meadows<\/li>\n<\/ul>\n<p>Web site is in building<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ian Nelson c# asp.net winforms using-statement I&#8217;m trying to use the System.Drawing.Color namespace. I&#8217;m not able to define it at the top of the class:However, I can reference it within the class. That is, I can use this line of code, and it works:txtBox.BackColor = System.Drawing.Color.LightPink;&#8230; but I&#8217;d rather just be able to do this:txtBox.BackColor [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4439","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4439"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4439\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}