I suggest you ...

Combine "Using" with "With"

Combine "Using" with "With", so that you could write for example:

Using System.IO.File.CreateText("log.txt")
.WriteLine("This is line one.")
.WriteLine("This is line two.")
End Using

instead of

Using writer As System.IO.TextWriter = System.IO.File.CreateText("log.txt")
writer.WriteLine("This is line one.")
writer.WriteLine("This is line two.")
End Using

31 votes
Sign in
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
Anonymous shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →


Sign in
Sign in with: facebook google
Signed in as (Sign out)
  • Qwertiy commented  ·   ·  Flag as inappropriate

    That's an axample of code where With Using construction would be very useful:

    Protected Async Function GetHtmlPageByPost(ByVal Url As String, ByVal Data As Dictionary(Of String, String), Optional ByVal AllowAutoRedirect As Boolean = True) As Task(Of String)
    Dim Request As HttpWebRequest = WebRequest.Create(Url)
    Dim DataStr As String = String.Join("&", From Kvp In Data Select Uri.EscapeUriString(Kvp.Key) & "=" & Uri.EscapeUriString(Kvp.Value))
    Dim BinData As Byte() = Options.SiteEncoding.GetBytes(DataStr)
    Dim SetCookie As String

    With Request
    .Method = WebRequestMethods.Http.Post
    .AllowAutoRedirect = AllowAutoRedirect
    .CookieContainer = CookieContainer
    .ContentType = "application/x-www-form-urlencoded"
    .ContentLength = BinData.Length
    .GetRequestStream().Write(BinData, 0, BinData.Length)
    Using Response As HttpWebResponse = Await .GetResponseAsync()
    With Response
    SetCookie = .Headers("Set-Cookie")
    If Not String.IsNullOrEmpty(SetCookie) Then CookieContainer.SetCookies(Options.CookieUri, SetCookie)
    Using ResponseStream As Stream = .GetResponseStream()
    Using Reader As New StreamReader(ResponseStream, Options.SiteEncoding)
    Return Await Reader.ReadToEndAsync()
    End Using
    End Using
    End With
    End Using
    End With
    End Function

  • Qwertiy commented  ·   ·  Flag as inappropriate

    I'd like to see this all constructions without adding With logic (i.e. dotted field access) to Using statemant:
    With ...
    Using ...
    With Using ...

  • Craig Johnson commented  ·   ·  Flag as inappropriate

    I opened a Roslyn discussion on With (including Using).


    Proposed syntax:

    With Using r = New DisposableRequest
    Return r
    End With

    "Not sure what makes more sense - to have With, then Using or Using, then With. I think I prefer With first since in my mind I'm saying "With this disposable resource, do this and this and this" instead of "Using this disposable resource and now with that resource do this and this and this." Using is sort of an attribute of the thing we're working with so I think With makes more sense to have first."

  • Dan Walker commented  ·   ·  Flag as inappropriate

    I like the "With Using x" nomenclature, personally. If you were to add SyncLock to it I would suggest "SyncLock With Using x". Of course this is only for the special case of a single expression on the using statement.

    I also like the idea of allowing the With keyword to have Using-like syntax..
    With x As Something = New Something
    .Color = Red
    End With

    However, this does not make it clear that "Something" has to implement IDisposable - as that is not currently a requirement for the With keyword. I wouldn't have a problem with this if the IDisposable requirement were lifted. So if the "Something" type implements IDisposable then call it's Dispose method on End With, however, if it does not implement this interface then treat the With block as any other scope block and just let the variable die.

    Also, back on the SyncLock concept - I really don't see the need to integrate it into this With/Using feature. But if you must, then make the editor reorder the words into a standard.. like it already does with default properties: Try typing Public Default Property Orge As String into the IDE.

    I think your biggest language issue here is going to be what to do at the end of the code block.
    With Using x As New Something

    End ' Uh.. what am I ending? End With? End Using?

    Maybe we need a new keyword:
    Oozing x As Something = Something.Create()
    .Color = Blue
    End Oozing

  • Mike C commented  ·   ·  Flag as inappropriate

    "Using With x" seems like the most logical order of the keywords to me.

    I was unaware the Using statement supported multiple expressions, so thanks for that! My first instinct is to apply the With only to the first expression in the Using statement, but upon reflection, I think it should be the last expression, as that's the one you're most likely to use inside the block (such as in your example above). This should avoid any special cases.

    I don't think "Using With" should be combined with SyncLock, because that does add too much complexity. The SyncLock block could either wrap around the Using With block if you need to include any constructors inside the SyncLock, or it could be placed inside the Using With block otherwise. Also, the statement "Using With SyncLock", while being easy to read, devalues the emphasis on the With.

  • Mike C commented  ·   ·  Flag as inappropriate

    An alternate suggestion is to allow a "Using With x..." block, so you can have a Using block with and without With.

Feedback and Knowledge Base