locked
Would this cause any problems? RRS feed

  • Question

  • User-1514917534 posted

    Alright, so I'm updating a web page that accepts user input, then applies it to an email. It used to be in ASP, now i'm making it in ASP.NET with VB.NET. In order to get formatting for the email, I made a little funtion that, when you pass the name of a text file through it, it returns all the data in the text file and applies it to a string. Once I have that string, I cut out special markers in the string and input user data into the same spot. Like this:

    Dim strName As String = Server.HTMLEncode(txtFirstName.Text + " " + txtLastName.Text)
    Dim EmailBody As String = FileReader("EmailBodyFile.txt") 
    
    EmailBody = EmailBody.Replace("^name^", strName)

    Works great, never had a problem, but it's just that whenever I need to change a segment, I need to type the string EmailBody over and over again. However, if I use the With/EndWith, then I don't type it so much! So I turned that above into:

            With EmailContent
                EmailContent = .Replace("fieldname", "Workshop")
                EmailContent = .Replace("^first_name^", strFirstName)
                EmailContent = .Replace("^last_name^", strLastName)
                EmailContent = .Replace("^homeaddress^", strHomeAddress)
                EmailContent = .Replace("^homecity^", strHomeCity)
                EmailContent = .Replace("^homestate^", strHomeState)
                EmailContent = .Replace("^homezip^", strHomeZip)
                EmailContent = .Replace("^homephone^", strHomePhone)
                EmailContent = .Replace("^homeemail^", strHomeEmail)
            End With

    Now, my question is, Is that segment above going to cause any kind of a problem? Or, is there a little bit more efficient way of doing it?

    I can post code if someone would like to see...

    Friday, July 30, 2010 2:54 PM

Answers

  • User-1514917534 posted

    Would you please re-post your third code clip?

     

    Huh, that was weird, but yeah sure, here it is:

    1. With EmailContent   
    2.     EmailContent = .Replace("fieldname""Workshop")   
    3.     EmailContent = .Replace("^first_name^", strFirstName)   
    4.     EmailContent = .Replace("^last_name^", strLastName)   
    5.     EmailContent = .Replace("^homeaddress^", strHomeAddress)   
    6.     EmailContent = .Replace("^homecity^", strHomeCity)   
    7.     EmailContent = .Replace("^homestate^", strHomeState)   
    8.     EmailContent = .Replace("^homezip^", strHomeZip)   
    9.     EmailContent = .Replace("^homephone^", strHomePhone)   
    10.     EmailContent = .Replace("^homeemail^", strHomeEmail)   
    11. End With  
            With EmailContent
                EmailContent = .Replace("fieldname", "Workshop")
                EmailContent = .Replace("^first_name^", strFirstName)
                EmailContent = .Replace("^last_name^", strLastName)
                EmailContent = .Replace("^homeaddress^", strHomeAddress)
                EmailContent = .Replace("^homecity^", strHomeCity)
                EmailContent = .Replace("^homestate^", strHomeState)
                EmailContent = .Replace("^homezip^", strHomeZip)
                EmailContent = .Replace("^homephone^", strHomePhone)
                EmailContent = .Replace("^homeemail^", strHomeEmail)
            End With


     

     

     

    Alright, I've discovered something. This IS a bad idea. Looks like a good idea...but it isn't. You see, because it calls the string EmailContent only once, then only the last .Replace command is used.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 3, 2010 4:08 PM

All replies

  • User-2010311731 posted

    Would you please re-post your third code clip?

    Friday, July 30, 2010 4:24 PM
  • User-1514917534 posted

    Would you please re-post your third code clip?

     

    Huh, that was weird, but yeah sure, here it is:

            With EmailContent
                EmailContent = .Replace("fieldname", "Workshop")
                EmailContent = .Replace("^first_name^", strFirstName)
                EmailContent = .Replace("^last_name^", strLastName)
                EmailContent = .Replace("^homeaddress^", strHomeAddress)
                EmailContent = .Replace("^homecity^", strHomeCity)
                EmailContent = .Replace("^homestate^", strHomeState)
                EmailContent = .Replace("^homezip^", strHomeZip)
                EmailContent = .Replace("^homephone^", strHomePhone)
                EmailContent = .Replace("^homeemail^", strHomeEmail)
            End With


     

    Friday, July 30, 2010 4:43 PM
  • User-1514917534 posted

    Would you please re-post your third code clip?

     

    Huh, that was weird, but yeah sure, here it is:

    1. With EmailContent   
    2.     EmailContent = .Replace("fieldname""Workshop")   
    3.     EmailContent = .Replace("^first_name^", strFirstName)   
    4.     EmailContent = .Replace("^last_name^", strLastName)   
    5.     EmailContent = .Replace("^homeaddress^", strHomeAddress)   
    6.     EmailContent = .Replace("^homecity^", strHomeCity)   
    7.     EmailContent = .Replace("^homestate^", strHomeState)   
    8.     EmailContent = .Replace("^homezip^", strHomeZip)   
    9.     EmailContent = .Replace("^homephone^", strHomePhone)   
    10.     EmailContent = .Replace("^homeemail^", strHomeEmail)   
    11. End With  
            With EmailContent
                EmailContent = .Replace("fieldname", "Workshop")
                EmailContent = .Replace("^first_name^", strFirstName)
                EmailContent = .Replace("^last_name^", strLastName)
                EmailContent = .Replace("^homeaddress^", strHomeAddress)
                EmailContent = .Replace("^homecity^", strHomeCity)
                EmailContent = .Replace("^homestate^", strHomeState)
                EmailContent = .Replace("^homezip^", strHomeZip)
                EmailContent = .Replace("^homephone^", strHomePhone)
                EmailContent = .Replace("^homeemail^", strHomeEmail)
            End With


     

     

     

    Alright, I've discovered something. This IS a bad idea. Looks like a good idea...but it isn't. You see, because it calls the string EmailContent only once, then only the last .Replace command is used.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 3, 2010 4:08 PM