patterncsharpMinor
Outputting Errors to User
Viewed 0 times
outputtingerrorsuser
Problem
I currently am making a small application where a user will enter their ID which needs to be in a certain format. When entered in the wrong format, different output errors are produced.
I currently output errors like this:
I have read that using
but then I basically do the same thing later:
Is there a cleaner way to output errors? The output is injected under the submit button into:
This is what I ended up using going off of Charles' response to AWinkle
Then :
I currently output errors like this:
public void errorLogger(string error)
{
errors += error + Environment.NewLine + "";
}I have read that using
+= is not a great operator to use. Alternatively I have thought this may be better:public void errorLogger(string key, string err)
{
if(!String.IsNullOrEmpty(err))
output.Add(key, err);
}but then I basically do the same thing later:
string keys = "";
foreach (KeyValuePair entry in core.errorLogger())
{
keys += entry.Key + ": " + entry.Value + "";
}Is there a cleaner way to output errors? The output is injected under the submit button into:
errorSpan.InnerHtml = (keys);This is what I ended up using going off of Charles' response to AWinkle
public void logError(string err, string desc)
{
if(!String.IsNullOrEmpty(desc))
errors.AppendFormat("{0,10}: {1}", err, desc);
}
public string getErrors()
{
return errors.ToString();
}Then :
errorspan.InnerHtml = (core.getErrors());Solution
Using the
turns into
Then you take
Alternatively, you could use this to replace the dictionary as CharlesNRice suggests.
StringBuilder class will typically be better for looped string building.string keys = "";
foreach (KeyValuePair entry in core.errorLogger())
{
keys += entry.Key + ": " + entry.Value + "";
}turns into
StringBuilder keys = new StringBuilder();
foreach (KeyValuePair entry in core.errorLogger())
{
keys.AppendFormat("{0}: {1}", entry.Key, entry.Value);
}Then you take
errorSpan.InnerHtml = (keys); and change it to errorSpan.InnerHtml = (keys.ToString());Alternatively, you could use this to replace the dictionary as CharlesNRice suggests.
StringBuilder keys = new StringBuilder();
public void errorLogger(string key, string err)
{
if(!String.IsNullOrEmpty(err))
keys.AppendFormat("{0}: {1}", key,err);
}Code Snippets
string keys = "";
foreach (KeyValuePair<string, string> entry in core.errorLogger())
{
keys += entry.Key + ": " + entry.Value + "<br>";
}StringBuilder keys = new StringBuilder();
foreach (KeyValuePair<string, string> entry in core.errorLogger())
{
keys.AppendFormat("{0}: {1}<br>", entry.Key, entry.Value);
}StringBuilder keys = new StringBuilder();
public void errorLogger(string key, string err)
{
if(!String.IsNullOrEmpty(err))
keys.AppendFormat("{0}: {1}<br>", key,err);
}Context
StackExchange Code Review Q#69194, answer score: 4
Revisions (0)
No revisions yet.