Results 1 to 5 of 5

Thread: [2008] text parse problem (or long code)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    [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

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    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.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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).

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Public Class TemplateViews
    2.  
    3.     Private _clientName As String
    4.     '...
    5.  
    6.     Public Property ClientName() As String
    7.         Get
    8.             Return If(String.IsNullOrEmpty(Me._clientName), "N/A", Me._clientName)
    9.         End Get
    10.         Set(ByVal value As String)
    11.             Me._clientName = value
    12.         End Set
    13.     End Property
    14.  
    15.     '...
    16.  
    17. End Class
    2. Use a similar If statement when calling Replace each time:
    vb.net Code:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width