locked
missing usefull C# bracket technique in VB RRS feed

  • Question

  • User2110873642 posted

    In C# i use  { } brackets to seperate concerns and avoid duplicate variables. which i think is very clean

    // Do something
    {
    string str = "";
    }
    
    // Repeat it
    {
    string str = "";
    }

    in VB, i cannot find a feature similar to that. so i use this ugly workaround:

    ' Do something
    with ""
      string str = "";
    end with
    
    ' Repeat it
    with ""
      string str = "";
    end with

    what is the recommended practise to achieve this in VB?

    Wednesday, March 17, 2021 6:53 PM

All replies

  • User1535942433 posted

    Hi fazioliamboina,

    As far as I think, With.. End With is a simplified and recommend syntax.It executes a series of statements that repeatedly refer to a single object or structure.

    Best regards,

    Yijing Sun

    Thursday, March 18, 2021 3:23 AM
  • User-1038772411 posted

    Hi fazioliamboina check this, 

    Private Sub SurroundingSub()
    If True Then
    Dim str As String = ""
    End If
    
    If True Then
    Dim str As String = ""
    End If
    End Sub



    Friday, March 19, 2021 8:18 AM
  • User303363814 posted

    i think is very clean
    Always imagine that the maintenance programmer is an axe murder who knows where you live.

    Don't reuse variable names within a method.  Just don't do it. (Yes, you are allowed but think of the people coming after you trying to sort out the code/confusion).

    Don't use 'str' as a variable name.  Variable names should be descriptive.

    If your code is repetitive (as 'Do something' and 'Repeat it' implies) then split the repetition into its own method so you don't repeat yourself. You may need a parameter, that's ok. (Time passes ... 'I need to modify that code ... I wonder how many places I copied and pasted it slightly differently????' Programmer runs screaming into the distance!)

    Remember that in six months you are a different person ... that maintenance programmer is you.  Be nice to your future self.

    Friday, March 19, 2021 10:39 PM