Wish Option to auto generate getters and setters.

Claudio Oliveira

Active Member
Licensed User
Longtime 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:
B4X:
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:
B4X:
Private Var1, Var2, Var3, Var4 as String

Well done, @Lahksman!

Regards
 

Lahksman

Active Member
Licensed User
Longtime User
It would probably be neater code to implement a stringbuilder for the output.
That would become something like this.
B4X:
Sub btnGenerateProperties_MouseClicked (EventData As MouseEvent)
    txtProperties.Text = ""
    sb_out.Initialize
    sb.Initialize
    Dim mainString() As String
    mainString = Regex.Split(CRLF,txtVariables.Text)
    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))
        Log(variables(1))
        sb.Append("Sub get" & variables(1) & " " & variables(2) & " " & variables(3) & CRLF)
        sb.Append(TAB & "Return " & variables(1) & CRLF)
        sb.Append("End Sub" & CRLF)
        sb.Append("Sub set" & variables(1) & " " &  " (t " & variables(2) & " " & variables(3) & ")" & CRLF)
        sb.Append(TAB & variables(1) & " = t" & CRLF)
        sb.Append("End Sub")
        Log(sb.tostring)
    Next
    txtProperties.Text = sb.ToString
End Sub
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
It would probably be neater code to implement a stringbuilder for the output.
That would become something like this.

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:
B4X:
sb.Append("Sub get").Append(variables(1)).Append(" ").Append(variables(2)).Append(" ").AppendLine(variables(3))
But only Erel knows for sure ;)
 

Lahksman

Active Member
Licensed User
Longtime User
Good tip!
You could indeed do the following.
B4X:
sb.Append("Sub get").Append(variables(1)).Append(" ").Append(variables(2)).Append(" ").Append(variables(3)).Append(CRLF) _
            .Append(TAB).Append("Return ").Append(variables(1)).append(CRLF) _
            .Append("End Sub").Append(CRLF) _
            .Append("Sub set").Append(variables(1)).Append(" ").Append(" (t ").Append(variables(2)).Append(" ").Append(variables(3)).Append(")").Append(CRLF) _
            .Append(TAB).append(variables(1)).Append(" = t").Append(CRLF) _
            .Append("End Sub").Append(CRLF)
 
Top