Wish Option to auto generate getters and setters.

Discussion in 'B4J Bugs & Wishlist' started by keirS, Mar 9, 2018.

  1. keirS

    keirS Well-Known Member Licensed User

    Something like being able to right click on a variable declaration in Class_Globals and select an option to auto generate it's getter and setter subs.
    Claudio Oliveira likes this.
  2. Lahksman

    Lahksman Active Member Licensed User

    I've made myself a small tool for generating the getters en setters.
    Source is attached to this post. Just paste your variables in the left textfield an push the button to generate the getters and setters.

    Attached Files:

  3. Claudio Oliveira

    Claudio Oliveira Active Member Licensed User

    Nice one!
    If you don't mind, I did a small change to ignore comment and blank lines, so we can copy/paste a block of code withou minding these lines.
    Added this in the very beginning of the For/Next loop:
    mainString(i) = mainString(i).Trim
    If mainString(i).StartsWith("'"Or mainString(i) = "" Then Continue
    This nice tool can be further optimized to make it capable of dealing with statements like this:
    Private Var1, Var2, Var3, Var4 as String
    Well done, @Lahksman!

    Johan Schoeman and Lahksman like this.
  4. Lahksman

    Lahksman Active Member Licensed User

    Feel free to do whatever is necessary.
    I just wrote this for myself some time ago so it's based on my style of coding.
    Claudio Oliveira likes this.
  5. Lahksman

    Lahksman Active Member Licensed User

    It would probably be neater code to implement a stringbuilder for the output.
    That would become something like this.
    Sub btnGenerateProperties_MouseClicked (EventData As MouseEvent)
        txtProperties.Text = 
    Dim mainString() As String
        mainString = 
    For i = 0 To mainString.Length -1
            mainString(i) = mainString(i).Trim
    If mainString(i).StartsWith("'"Or mainString(i) = "" Then Continue
    Dim variables() As String
            variables = 
    Regex.Split(" ",mainString(i))
    "Sub get" & variables(1) & " " & variables(2) & " " & variables(3) & CRLF)
    TAB & "Return " & variables(1) & CRLF)
    "End Sub" & CRLF)
    "Sub set" & variables(1) & " " &  " (t " & variables(2) & " " & variables(3) & ")" & CRLF)
    TAB & variables(1) & " = t" & CRLF)
    "End Sub")
        txtProperties.Text = sb.ToString
    End Sub
  6. Claudio Oliveira

    Claudio Oliveira Active Member Licensed User

    Yes, absolutely!
    Good one!
  7. Jeffrey Cameron

    Jeffrey Cameron Active Member Licensed User

    You're still doing a lot of in-line concatenating, I think it would be more efficient/faster to use the append repeatedly, such as:
    sb.Append("Sub get").Append(variables(1)).Append(" ").Append(variables(2)).Append(" ").AppendLine(variables(3))
    But only Erel knows for sure ;)
  8. Lahksman

    Lahksman Active Member Licensed User

    Good tip!
    You could indeed do the following.
    sb.Append("Sub get").Append(variables(1)).Append(" ").Append(variables(2)).Append(" ").Append(variables(3)).Append(CRLF) _
    TAB).Append("Return ").Append(variables(1)).append(CRLF) _
    "End Sub").Append(CRLF) _
    "Sub set").Append(variables(1)).Append(" ").Append(" (t ").Append(variables(2)).Append(" ").Append(variables(3)).Append(")").Append(CRLF) _
    TAB).append(variables(1)).Append(" = t").Append(CRLF) _
    "End Sub").Append(CRLF)
    Johan Schoeman likes this.
  9. Claudio Oliveira

    Claudio Oliveira Active Member Licensed User

    Yeah, that works as well...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice