[2008] text parse problem (or long code)
I have the following code:
Code:
stringToParse = stringToParse.Replace("[CLIENTNAME]", tv.ClientName)
stringToParse = stringToParse.Replace("[EMAIL]", tv.Email)
stringToParse = stringToParse.Replace("[COUNTRY]", tv.Country)
stringToParse = stringToParse.Replace("[DATE]", tv.DateNow.ToString)
stringToParse = stringToParse.Replace("[COMPANYNAME]", tv.CompanyName)
stringToParse = stringToParse.Replace("[USERNAME]", tv.UserName)
stringToParse = stringToParse.Replace("[ORDERID]", tv.OrderID)
stringToParse = stringToParse.Replace("[ORDERBALANCE]", tv.OrderBalance)
stringToParse = stringToParse.Replace("[ORDERITEMS]", tv.OrderItems.ToString)
stringToParse = stringToParse.Replace("[OPENORDERS]", tv.NumberOfOpenOrders)
stringToParse = stringToParse.Replace("[NONPAIDORDERS]", tv.NumberOfNonPaidOrders)
As you can see im just replacing some stuff with actual values.. I want to make it so string.isnullorempty = true then it should replace with "N/A".
Is the only way to do this by putting a lot of if statements?
TV is a class called Template Values
Code:
Public Class TemplateValues
Public ClientName As String
Public Email As String
Public Country As String
Public DateNow As Date
Public CompanyName As String
Public UserName As String
Public OrderID As String
Public OrderBalance As String
Public OrderItems() As String
Public NumberOfOpenOrders As String
Public NumberOfNonPaidOrders As String
End Class
Re: [2008] text parse problem (or long code)
I don't quite understand what you're trying to do, but I can tell you that it may not work out the way you want. This line here:
Code:
stringToParse = stringToParse.Replace("[ORDERITEMS]", tv.OrderItems.ToString)
tv.OrderItems is a string array, so when you call its ToString method, you will get something like "System.String[]" instead of the actual strings in the array.
Re: [2008] text parse problem (or long code)
^ thanks, i forgot to remove that.. I knew that :P
okay, pretty much if we pretend OrderItems were a string, then in the case that it was either null/empty then it should be replaced with "N/A" rather then actually giving me an empty value, or throwing a null exception.
Re: [2008] text parse problem (or long code)
Quote:
Originally Posted by masfenix
^ thanks, i forgot to remove that.. I knew that :P
okay, pretty much if we pretend OrderItems were a string, then in the case that it was either null/empty then it should be replaced with "N/A" rather then actually giving me an empty value, or throwing a null exception.
No, with that line of code it doesn't replace any empty string with "N/A". What that line of code does is that it'll replace all the substrings "[ORDERITEMS]" in stringToParse (if there are any) with whatever the value of tv.orderItems is (again, pretending that this is a string like you said).
Re: [2008] text parse problem (or long code)
Your description is really not very clear. Are you saying that if one of the fields in your TemplateValues object is Nothing or an empty string, you want to replace the template field with "N/A"? If so then I'd say that you have two options:
1. Build that functionality into your TemplateValues class:
vb.net Code:
Public Class TemplateViews
Private _clientName As String
'...
Public Property ClientName() As String
Get
Return If(String.IsNullOrEmpty(Me._clientName), "N/A", Me._clientName)
End Get
Set(ByVal value As String)
Me._clientName = value
End Set
End Property
'...
End Class
2. Use a similar If statement when calling Replace each time:
vb.net Code:
stringToParse = stringToParse.Replace("[CLIENTNAME]", If(String.IsNullOrEmpty(tv.ClientName), "N/A", tv.ClientName))
The first option would be preferable unless that functionality is not logically part of the TemplateValues class.