String.Replace and Objects

D

Derek Martin

I have an object with several string elements that I would like to check for
invalid characters in the properties of each element. Can I use
string.replace to do that or is there a better alternative to this?
Example:

Property orgname() As String
Get
orgname = m_orgname
End Get
Set(ByVal Value As String)
If Value = "" Then Throw New ArgumentException("OrgName cannot be
null or blank")
value.Replace("'", "&&")
value.Replace(";", "&&")
value.replace(",", "&&")
m_orgname = Value
End Set
End Property

Is there an easier way to do this/a way to do it with fewer repetative lines
of code?

Thanks,
Derek
 
H

Herfried K. Wagner [MVP]

* "Derek Martin said:
I have an object with several string elements that I would like to check for
invalid characters in the properties of each element. Can I use
string.replace to do that or is there a better alternative to this?
Example:

Property orgname() As String
Get
orgname = m_orgname
End Get
Set(ByVal Value As String)
If Value = "" Then Throw New ArgumentException("OrgName cannot be
null or blank")
value.Replace("'", "&&")
value.Replace(";", "&&")
value.replace(",", "&&")

Use 'value = value.Replace(...)'. 'Replace' is a function, not a sub.
 
J

Jay B. Harlow [MVP - Outlook]

Derek,
As Herfried suggests, String.Replace is a function you need to assign the
value returned to a variable.

If I have more then 5 to 10 replaces I would consider using a StringBuilder
instead of String.Replace.

Something like:
If Value = "" Then Throw New ArgumentException("OrgName cannot be
null or blank")
Dim sb As New StringBuilder(value, value.Length * 2)
sb.Replace("'", "&&")
sb.Replace(";", "&&")
sb.replace(",", "&&")
m_orgname = sb.ToString

If you are replacing all the matched string with the same value (as it
appears you are) I would also consider using a RegEx.
If Value = "" Then Throw New ArgumentException("OrgName cannot be
null or blank")
Dim re As New RegEx("'|;|,")
m_orgname = re.Replace(value, "&&"

If I used the RegEx approach I would consider making the RegEx itself a
shared or static variable and passing the RegexOptions.Compiled option to
the RegEx constructor.

Static re As New RegEx("'|;|,", RegexOptions.Compiled)
m_orgname = re.Replace(value, "&&"

Double check the reg ex pattern, I did not test it.

Hope this helps
Jay
 
D

Derek Martin

Wonderful, thank you all for your help!

Derek



Jay B. Harlow said:
Derek,
As Herfried suggests, String.Replace is a function you need to assign the
value returned to a variable.

If I have more then 5 to 10 replaces I would consider using a
StringBuilder
instead of String.Replace.

Something like:

Dim sb As New StringBuilder(value, value.Length * 2)
sb.Replace("'", "&&")
sb.Replace(";", "&&")
sb.replace(",", "&&")
m_orgname = sb.ToString

If you are replacing all the matched string with the same value (as it
appears you are) I would also consider using a RegEx.

Dim re As New RegEx("'|;|,")
m_orgname = re.Replace(value, "&&"

If I used the RegEx approach I would consider making the RegEx itself a
shared or static variable and passing the RegexOptions.Compiled option to
the RegEx constructor.

Static re As New RegEx("'|;|,", RegexOptions.Compiled)
m_orgname = re.Replace(value, "&&"

Double check the reg ex pattern, I did not test it.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top