using-statement

Should '#include' and 'using' statements be repeated in both header and implementation files (C++)?

岁酱吖の 提交于 2019-12-04 05:19:48
I'm fairly new to C++, but my understanding is that a #include statement will essentially just dump the contents of the #included file into the location of that statement. This means that if I have a number of '#include' and 'using' statements in my header file, my implementation file can just #include the header file, and the compiler won't mind if I don't repeat the other statements. What about people though? My main concern is that if I don't repeat the '#include', 'using', and also 'typedef' (now that I think of it) statements, it takes that information away from the file in which it's

.NET/C# - Disposing an object with the 'using' statement

℡╲_俬逩灬. 提交于 2019-12-04 03:56:04
Suppose I have a method like so: public byte[] GetThoseBytes() { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { ms.WriteByte(1); ms.WriteByte(2); return ms.ToArray(); } } Would this still dispose the 'ms' object? I'm having doubts, maybe because something is returned before the statement block is finished. Thanks, AJ. Yes. using (x = e) { s } is sugar for { x = e; try { s } finally { x.Dispose(); } } Yes, Using creates a try..finally block , so it disposes the ms (and even does a null check in case you set ns to null). Yes, the whole idea behind the Using statement is that

SqlCommand (Using Statement / Disposing issue)

帅比萌擦擦* 提交于 2019-12-04 02:34:51
Take the following example... Using cn As New SqlConnection(ConnectionString) Try Dim cmd As SqlCommand = New SqlCommand With cmd .Connection = cn .Connection.Open() .CommandText = "dbo.GetCustomerByID" .CommandType = CommandType.StoredProcedure .Parameters.Add("@CustomerID", SqlDbType.Int, 4) .Parameters("@CustomerID").Value = CustomerID End With da = New SqlDataAdapter(cmd) da.Fill(ds, "Customer") Catch ex As Exception End Try End Using From my research today is sounds as though this is basically okay but the SqlCommand is not being disposed of. Question -> Which of the following examples is

Using for IDbConnection/IDbTransaction safe to use?

大憨熊 提交于 2019-12-03 17:35:38
问题 While my assumption may seem to sound subjective, after some research, I found that it's not uncommon to find developers who favour a dummy Try/Catch instead of using the Using statement for IDbConnection/IDbTransaction processing (Close/Commit/Rollback). This holds true for even some of the most seasoned developers and some new ones. I am intentionally not going to reference any of the question on StackOverflow or forum links as an example, so people don't get offended. From what I found,

include and using namespace in C++

扶醉桌前 提交于 2019-12-03 12:36:50
问题 for using cout , I need to specify both: #include<iostream> and using namespace std; Where is cout defined? in iostream , correct? So, it is that iostream itself is there in namespace std ? What is the meaning of both the statements with respect to using cout ? I am confused why we need to include them both. 回答1: iostream is the name of the file where cout is defined. On the other hand, std is a namespace, equivalent (in some sense) to java's package. cout is an instance defined in the

What is the standard conform syntax for template constructor inheritance?

随声附和 提交于 2019-12-03 12:25:40
问题 GCC 4.8.1 accepts template <typename T> class Subclass : public Baseclass<T> { public: using typename Baseclass<T>::Baseclass; }; but MSVC does not. On the other hand, MSVC accepts template <typename T> class Subclass : public Baseclass<T> { public: using typename Baseclass::Baseclass; }; but GCC does not. Then I've seen another kind of declaration in this questions: c++11 inheriting template constructors template <typename T> class Subclass : public Baseclass<T> { public: using typename

Can the C# using statement be written without the curly braces?

你说的曾经没有我的故事 提交于 2019-12-03 11:27:25
问题 I was browsing a coworkers c# code today and found the following: using (MemoryStream data1 = new MemoryStream()) using (MemoryStream data2 = new MemoryStream()) { // Lots of code.......... } I had always seen the using statement followed by a pair of curly braces that defined the scope of the object life. My coworker who wrote the code said that the curly braces for the data1 using statement weren't needed and the code did the same thing as if they were present and nested the data2 using

When are C# “using” statements most useful?

徘徊边缘 提交于 2019-12-03 10:39:27
So a using statement automatically calls the dispose method on the object that is being "used", when the using block is exited, right? But when is this necessary/beneficial? For example let's say you have this method: public void DoSomething() { using (Font font1 = new Font("Arial", 10.0f)) { // Draw some text here } } Is it necessary to have the using statement here, since the object is created in the method? When the method exits, wont the Font object be disposed of anyway? Or does the Dispose method get run at another time after the method exits? For example if the method was like this:

Nested using statements

橙三吉。 提交于 2019-12-03 09:30:12
As Eric Gunnerson shows in this blog post, in C# you can nest using statements as: using (StreamWriter w1 = File.CreateText("W1")) using (StreamWriter w2 = File.CreateText("W2")) { // code here } Is there a similar way to do it in VB.Net? I want to avoid too many indentation levels. SLaks Like this: Using a As New Thingy(), _ b As New OtherThingy() ... End Using Well, you can do: Using w1 = File.CreateText("W1"), w2 = File.CreateText("W2") ' Code goes here. ' End Using 来源: https://stackoverflow.com/questions/3345303/nested-using-statements

C# conditional using block statement

血红的双手。 提交于 2019-12-03 06:36:49
问题 I have the follow code but it is awkward. How could I better structure it? Do I have to make my consuming class implement IDisposable and conditionally construct the network access class and dispose it when I am done? protected void ValidateExportDirectoryExists() { if (useNetworkAccess) { using (new Core.NetworkAccess(username, password, domain)) { CheckExportDirectoryExists(); } } else { CheckExportDirectoryExists(); } } 回答1: One option, which is somewhat nasty but would work, based on the