# Simple property formatting

In a VB.Net class, I have an method that replaces all square brackets [] in certain properties by normal brackets ().

Edit: The program allows users to write custom calculations, such as [3-(6-2)]. Being able to use two kinds of brackets makes things easier for them, but when processing the calculation, it's easier to have the same brackets everywhere.

The code is really redundant:

Public Sub removeBadBrackets()
ValueCalc = Replace(Replace(ValueCalc, "[", "("), "]", ")")
TargetCalc = Replace(Replace(TargetCalc, "[", "("), "]", ")")
LLLCalc = Replace(Replace(LLLCalc, "[", "("), "]", ")")
LLCalc = Replace(Replace(LLCalc, "[", "("), "]", ")")
LCalc = Replace(Replace(LCalc, "[", "("), "]", ")")
HCalc = Replace(Replace(HCalc, "[", "("), "]", ")")
HHCalc = Replace(Replace(HHCalc, "[", "("), "]", ")")
HHHCalc = Replace(Replace(HHHCalc, "[", "("), "]", ")")
End Sub


I think this could be done in a more efficient way, and I'd like to learn how.

Side note:

• Those properties are not the only one of the object.
• They are in the same region (although I am not sure regions can be used for anything else than my own convenience).

Thank you for your help improving this.

My VB.NET is incredibly rusty but you should write a separate function:

Public Shared Sub ReplaceBracketsWithParentheses(ByRef text As String)
text = Replace(Replace(text, "[", "("), "]", ")")
End Sub


Calling code will then simply be:

Public Sub removeBadBrackets()
ReplaceBracketsWithParentheses(ValueCalc)
...
End Sub


Note that if you have to replace more than few substrings then a fluid approach may be more readable:

Public Shared Sub ReplaceBracketsWithParentheses(ByRef text As String)
text = text.Replace("[", "(") _
.Replace("]", ")") _
.Replace("{", "(") _
.Replace("}", "]")
End Sub


If you're translating some sort of mathematical expressions (but not only!) you may also consider to use a Regex.

One word about naming: I don't know the domain you're using this code but to me LLLCalc, LLCalc and so on are not descriptive names (and, anyway, acronyms should be cased PascalCase like LllCalc, the same is true for removeBadBrackets()).

• I think vb still requires an _ and the end of a line when you multiline statements – Icepickle Aug 23 '17 at 7:41
• @Icepickle you're right! Rusty and dusty... – Adriano Repetti Aug 23 '17 at 7:45
• I agree with the suggestions, but I'm not a big fan of ByRef statements. Vb.net does support extension modules, maybe this might be a more appropariate approach but I'm unsure as long as I don't know why the OP wants to do it :) – Icepickle Aug 23 '17 at 7:49
• At first sight I thought it's to translate mathematical expressions (with parentheses, square brackets and curly brackets...) into parser friendly expressions (for example to use them in a parser like NCalc). Maybe a Regex may do better and faster. – Adriano Repetti Aug 23 '17 at 7:52
• Thank you for your answer. A word on pascalCase, in this context. As guessed, it is to be used in NCalc, and it's a value, along with limits, LowLowLow, LowLow, Low, High, HighHigh and HighHighHigh. In such a case where the three letters have the exact same meaning, and the information is contained in their repetition, would it be OK to break the rule? – Maxime Aug 24 '17 at 9:17