468,301 Members | 1,462 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,301 developers. It's quick & easy.

String.Replace and Objects

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
--
Derek Martin
593074
Nov 21 '05 #1
4 1362
"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in
news:Oz*************@tk2msftngp13.phx.gbl:
Is there an easier way to do this/a way to do it with fewer repetative
lines of code?


Try using Regular Expressions.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 21 '05 #2
* "Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> scripsit:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3
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

"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:Oz*************@tk2msftngp13.phx.gbl... 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
--
Derek Martin
593074

Nov 21 '05 #4
Wonderful, thank you all for your help!

Derek

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Of**************@TK2MSFTNGP12.phx.gbl...
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

"Derek Martin" <dm*****@DONTSPAMMEokstateDOT.edu> wrote in message
news:Oz*************@tk2msftngp13.phx.gbl...
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
--
Derek Martin
593074


Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Roose | last post: by
4 posts views Thread by higabe | last post: by
32 posts views Thread by tshad | last post: by
9 posts views Thread by Crirus | last post: by
4 posts views Thread by Craig Buchanan | last post: by
21 posts views Thread by gary | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.