problem about using-statement-Collection of common programming errors


  • Ian Nelson
    c# asp.net winforms using-statement
    I’m trying to use the System.Drawing.Color namespace. I’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;… but I’d rather just be able to do this:txtBox.BackColor = Color.LightPink;If it’s a matter of a missing reference/dll, why am I able to make reference to System.Drawing.Color in my code?

  • Ben Voigt

  • DH_Prog
    c# sql exception datareader using-statement
    I 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’s close to 2,000,000 rows…), 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 – 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

  • DzinX
    c# multithreading thread-safety using-statement abort
    I’ve just finished reading “C# 4.0 in a Nutshell” (O’Reilly) and I think it’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(“file.txt”)) {… }is precisely equivalent to:StreamReader reader = File.OpenText(“file.txt”); try {… } finally {if (reader != null)((IDisposable)reader).Dispose(); }Suppose, however, that this is true and that this

  • Kyle Uithoven
    c# .net namespaces using-statement
    I recently stopped using “using statements” and instead use the full namespace path of any .NET object that I call.Example:using System; namespace QuizViewer {class Class1{Console.WriteLine(“Hello World!”);} }This is what I do now.namespace QuizViewer {class Class1{System.Console.WriteLine(“Hello World!”);} }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

  • John Rasch
    visual-studio-2008 windows-7 clr using-statement directoryinfo
    Can someone help me find a solution to the following error:”fatal error C1190: managed targeted code requires a ‘/clr’ option” My configuration is ..Visual studio 2008 Windows 7 Here is the code (i got by using net resources)#using <mscorlib.dll> 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

  • makerofthings7
    c# .net com dispose using-statement
    What is better, the using directive, or the dispose directive when finished with an object?using(FileStream fileStream = new FileStream(“logs/myapp.log”,FileMode.Open,FileAccess.Read,FileShare.ReadWrite)){using(StreamReader streamReader = new StreamReader(fileStream)){this.textBoxLogs.Text = streamReader.ReadToEnd();}}On the other hand, when I’m dealing with System.Net.Mail, I’m told I need to Dispose() of the object to release any stray locks. Is there any consistent guidance? How do I tell wh

  • Shiraz Bhaiji
    c# mocking rhino-mocks using-statement
    Most 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<IMyProxy>, 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

  • nan
    c# scope using-statement sqlconnection
    I use the using statement for SqlConnection. It’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();//…connection = new SqlConnection(connectionString2);//…connection = new SqlConnection(connectionString3);}I was wondering if I can replace using, and do

  • GDM
    c# winforms .net-3.5 exception-handling using-statement
    Ive been doing some research today on when an how to use the “using” statement to dispose of my sql objects. However I’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’m doing something wrong?private BindingList<My_Object> Search(int ID) {string strSelectStatement = “SELECT ‘coloumns’ ” +”FROM ‘table’ ” +”WHERE ID = @ID;”;DataTable dt = new DataTable();try{using (SqlConnection sqlConn = new

  • SwDevMan81
    c# language-features using using-statement
    I understand the point of “using” is to guarantee that the Dispose method of the object will be called. But how should an exception within a “using” statement be handled? If there is an exception, I need to wrap my “using” 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(“LippertTheLeopard”)){connection.Open();}}catch (Excep

  • Jeff Atwood
    c# exception using using-statement
    Does the using catch the exception or throw it? i.e. using (StreamReader rdr = File.OpenText(“file.txt”)) {//do stuff }If the streamreader throws an exception is it caught by using or thrown so the calling function can handle it?

  • Jon Seigel
    c# coding-style using-statement
    This 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; }

  • Michael Meadows

Web site is in building