Easy way to get a formatted string

  • Thread starter Thread starter Jack Addington
  • Start date Start date
J

Jack Addington

I am capturing a set of validation messages from my validation method. I
was putting each message back into a StringCollection. I then wanted to
output a big warning message in a Dialog box. Is there a way to quickly
convert essentially an ArrayList of strings into a single string with each
element separated with a newline?

or is

foreach(string s in StrList) newstring.concat("\n" + s);

what I need to do?

thx
 
I would use a string builder to add messages to it, and add "\r\n" to
each message string. Something like this:

public void SaveData ()
{
StringBuilder errMsgs = new StringBuilder ();
if (!ValidateData (errMsgs))
MessageBox.Show (errMsgs.ToString);
else
{
//Save stuff here
}
}

public bool ValidateData (StringBuilder errMsgs)
{
bool isValid = true;
if (fnameText.TextLength == 0)
{
errMsgs.Append ("Please enter a valid first name \r\n");
isValid = false;
}
if (lnameText.TextLength == 0)
{
errMsgs.Append ("Please enter a valid last name \r\n");
isValid = false;
}
return isValid;
}

NuTcAsE
 
I did this by inheriting from StringCollection and then overriding
ToString().

--Bob
 
I think oj's solution is the best option here. In this situation, we
have all the strings that need to be joined, so a call to string.Join(.
.. .) would be the simplest. Using StringBuilder would complicate the
code and yield no benefit. NuTcAsE, if you think I'm wrong, please do
a test and post the results.

Best regards,
Jeffrey Palermo
Blog: http://www.jeffreypalermo.com
 
Back
Top