if-statement inside function - c#

I have this piece of code:
public static void Debug(string workName, string message, string logcontext, string exMessage = "")
{
LogEventInfo logEvent = new LogEventInfo(LogLevel.Debug, logcontext, message + ": " + exMessage);
...
}
As you can see, the last parameter of the Debug method is optional (string exMessage = ""). That means that if I run this code as-is it will look fine if a exMessage arrived: "...message: exMessage"
However, If a exMessage wasn't provided, it will look like this: "...message: " (Note the trailing :).
Of course, this can easily be solved by using something like this:
if(!String.IsNullOrEmpty(exMessage)
{
...(...,message + ": " + exMessage);
}
else
{
...(...,message);
}
I want a more beautiful approach. Like if there is something in the lines of:
...(...(!exMessage -> ,message | else -> ,message + ": " + exMessage));
Is there some way of doing this in-line if-statement in C#? Can I use lambdas some how? (I'm super-new to the whole concept of lambdas)

LogEventInfo logEvent = new LogEventInfo(LogLevel.Debug, logcontext, message + (!string.IsNullOrEmpty(exMessage) ? ": " : string.Empty) + exMessage);

You can use the conditional operator
exMessage == "" ? message : message + ": " + exMessage

There is its called the ?: operator. You use it like this:
var logText = string.IsNullOrEmpty(exMessage) ? message : message + ": " + exMessage;

Related

Updating front end during postback

I have a method that runs through a loop that can take quite a while to complete as it requires getting data back form an API.
What I would like to do is display a message on the front end explaining how the system is progressing during each loop. Is there a way to update the front end while processing?
public static void GetScreenshot(List<string> urlList, List<DesiredCapabilities> capabilities, String platform, Literal updateNote)
{
foreach (String url in urlList)
{
String siteName = new Uri(url).Host;
String dir = AppDomain.CurrentDomain.BaseDirectory+ "/Screenshots/" + siteName + "/" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm");
foreach (DesiredCapabilities cap in capabilities)
{
String saveDirectory = "";
if (platform == "btnGenDesktopScreens")
{
saveDirectory = dir + "/" + cap.GetCapability("os") + "-" + cap.GetCapability("os_version") + "-" + cap.GetCapability("browser") + cap.GetCapability("browser_version");
}
else if(platform == "btnMobile")
{
saveDirectory = dir + "/" + cap.GetCapability("platform") + "" + cap.GetCapability("device") + "-" + cap.GetCapability("browserName");
}
updateNote.Text += "<br/>" + cap.GetCapability("platform") + " - " + cap.GetCapability("device") + "-" + cap.GetCapability("browserName");
//I'd like to display a message here
TakeScreenshot(url, cap, saveDirectory);
//I'd like to display a message here
}
}
}
Has anyone come across a method of doing this?
Depending on how you're returning the feedback to the user, you might be able to do this by using HttpResponse.Flush in a loop to push parts of the HTML response to the user a bit at a time. See https://msdn.microsoft.com/en-us/library/system.web.httpresponse.flush(v=vs.100).aspx

C# Creating white space lines between assembled .text

private void btnAssemble_Click(object sender, EventArgs e)
{
txtAssembled.Text = (cboTitle.Text + txtFirstName.Text[0] + txtMiddle.Text + txtLastName.Text + "\r\n" +txtStreet.Text + "\r\n"+ cboCity.Text);
}
I'm trying to get 1 character white space inbetween cboTitle.Text, txtFirname.Text, txtMiddle.Text, and txtLastName, but they all output the information together, but I want them spaced evenly. what do I need to do? thanks in advance.
I'm going to post some other code thats below the one above in my project, just in case it might be relevant.
string AssembleText(string Title, string FirstName, string MiddleInitial, string LastName, string AddressLines, string City )
{
string Result = "";
Result += Title + " ";
Result += FirstName.Substring(0, 2) + " ";
// Only append middle initial if it is entered
if (MiddleInitial != "")
{
Result += MiddleInitial + " ";
}
Result += LastName + "\r\n";
// Only append items from the multiline address box
// if they are entered
if ( AddressLines != "")
{
Result += AddressLines + "\r\n";
}
//if (AddressLines.Length > 0 && AddressLines.ToString() != "")
//{
// Result += AddressLines + "\r\n";
//}
Result += City;
return Result;
}
}
}
If you just want a space between those specific fields in btnAssemble_Click, you can just insert them like this:
string myStr = foo + " " + bar + " " + baz;
So your first function would be modified to read:
private void btnAssemble_Click(object sender, EventArgs e)
{
txtAssembled.Text = (cboTitle.Text + " " + txtFirstName.Text[0] + " " + txtMiddle.Text + " " + txtLastName.Text + "\r\n" + txtStreet.Text + "\r\n" + cboCity.Text);
}
A few other comments:
It's not clear to me what the AssembleText() function you posted has to do with this. I am confused though, as I see a few lines appending spaces at the end just like I mentioned above.
Using the String.Format() function may make this code easier to read and maintain.
Using Environment.NewLine instead of "\r\n" will make the string contain the newline character defined for that specific environment.
Using a StringBuilder object may be faster over concatenation when building strings inside of a loop (which may not apply here).
Using String.format() should feet the bill. It also make your code easy to read.
txt.assembled.text = String.Format("{0} {1} {2} {3}",
cboTitle.Text,
txtFirstName.Text[0],
txtMiddle.Text,
txtLastName.Text
);
It would be like this
private void btnAssemble_Click(object sender, EventArgs e)
{
txtAssembled.Text = (cboTitle.Text + " " + txtFirstName.Text[0] + " " +txtMiddle.Text + " " + txtLastName.Text + "\r\n" +txtStreet.Text + "\r\n"+ cboCity.Text);
}
It seems that you want String.Join; whenever you want to combine strings with a delimiter, say, " " (space) all you need is to put
String combined = String.Join(" ",
cboTitle.Text,
txtFirstName.Text[0],
txtMiddle.Text,
txtLastName.Text);
Complete implementation (joining by space and new line) could be
txtAssembled.Text = String.Join(Environment.NewLine,
String.Join(" ",
cboTitle.Text,
txtFirstName.Text[0],
txtMiddle.Text,
txtLastName.Text),
txtStreet.Text,
cboCity.Text);

How can I successfully extract this substring in C#?

Here is the offending code. I haven't done much string manipulation yet, and am currently having issues.
if (orderid != orderlist[orderlist.Count - 1])
{
response2 = GetSubstringByString("{\"orderid\": \"" + orderid + "\"", "{\"orderid\": \"", response2);
}
else
{
response2 = GetSubstringByString("{\"orderid\": \"" + orderid + "\"", "success", response2);
}
Console.WriteLine("Response 2 is: " + response2);
logger.Log("Writing " + writepath + filename);
File.WriteAllText(writepath + filename, response2);
}
public string GetSubstringByString(string a, string b, string c) //trims beginning and ending of string
{
Console.WriteLine("String a is: " + a + "String b is: " + b + "String c is: " + c);
return c.Substring((c.IndexOf(a) + a.Length), (c.IndexOf(b) - c.IndexOf(a) - a.Length));
}
I am having issues extracting a substring, as the beginning and ending strings are the same, and therefore it is unable to differentiate the strings from each other.
Here is the main issue:
response2 = GetSubstringByString("{\"orderid\": \"" + orderid + "\"", "{\"orderid\": \"", response2);
Is there a way I can add a check if the orderid for the ending string differs from the starting string orderid? Thanks for any help!
I was working with code that was already set to scan rather than parse JSON in the optimal fashion.
I utilized this regex to remove orderid before each number as to not cause scanner length exceptions. I also overloaded string.IndexOf as mentioned by juharr.
var regex = new Regex(Regex.Escape("orderid")); //replace first occurrence of orderid
response2 = regex.Replace(response2, "", parsecount-1);
public string GetSubstringByString(string a, string b, string c) //trims beginning and ending of string
{
logger.Log("String a is: " + a + "String b is: " + b + "String c is: " + c);
var offset = c.IndexOf(b);
//lastcheck will return 0 if it's last element in orderlist, because ending string differs for last
return c.Substring((c.IndexOf(a) + a.Length), (c.IndexOf(b, offset + lastcheck) - c.IndexOf(a) - a.Length));
}

RichTextBox new Text append on top

I have a RichtTextBox in my C# application that shows a log to the user. The problem is that newly inserted text appends below the old text, but I want to append it on top of the old text.
For example, When I append the text "Newtext" it looks like this:
RichtTextBox:
|---------------------
|Oldtext |
|Newtext |
|---------------------
But it needs to look like this:
RichTextBox:
|---------------------
|Newtext |
|Oldtext |
|---------------------
This is the code I'm using for filling my RichTextBox:
public void DisplayLog(string logtext)
{
if (logtext != "")
{
if (this.txtLog.InvokeRequired && !txtLog.IsDisposed)
{
Invoke(new MethodInvoker(delegate()
{
txtLog.AppendText(DateTime.UtcNow + ": " + logtext + "\n");
}));
}
else if (!txtLog.IsDisposed)
{
txtLog.AppendText(DateTime.UtcNow + ": " + logtext + "\n");
}
}
}
Can somebody help me out please?
Answer:
Inserting at top of richtextbox
Use Insert
txtLog.Text = txtLog.Text.Insert(0,DateTime.UtcNow + ": " + logtext + "\n");
I think txtlog is the RichTextBox and you should prepend this.
To do this go at start using
txtlog .SelectionStart = 0;
txtlog .SelectionLength = 0;
txtlog .SelectedText = (DateTime.UtcNow + ": " + logtext + "\n");

c# null string?

I had the following:
string Name = name.First + " " + name.Last;
This returns Tom Jones just fine.
In case name.First may be null or name.Last may be null, I have the following:
string SpeakerName = name.First ?? string.Empty + " " + name.Last ?? string.Empty;
What is strange is that it only returns Tom. Why is this and how can I fix it such that if null it defaults to empty string for either first or last name?
Because of the relative precedence of the ?? and + operators. Try this:
string SpeakerName = (name.First ?? "") + " " + (name.Last ?? "");
Your original example is evaluating as if it was:
string SpeakerName = name.First ?? ("" + " " + (name.Last ?? ""));
Also, read Jon's answer here: What is the operator precedence of C# null-coalescing (??) operator?
As he suggests there, this should work as well:
string SpeakerName = name.First + " " + name.Last;
Because that compiles to #L.B.'s answer below, minus the trim:
string SpeakerName = String.Format("{0} {1}", name.First, name.Last)
EDIT:
You also asked that first and last both == null makes the result an empty string. Generally, this is solved by calling .Trim() on the result, but that isn't exactly equivalent. For instance, you may for some reason want leading or trailing spaces if the names are not null, e.g. " Fred" + "Astair " => " Fred Astair ". We all assumed that you would want to trim these out. If you don't, then I'd suggest using a conditional:
string SpeakerName = name.First + " " + name.Last;
SpeakerName = SpeakerName == " " ? String.Empty : SpeakerName;
If you never want the leading or trailing spaces, just add a .Trim() as #L.B. did
string SpeakerName = String.Format("{0} {1}", name.First, name.Last).Trim();
string SpeakerName = name.First != null && name.Last != null
? string.Format("{0} {1}", name.First, name.Last)
: string.Empty;
string fullName = (name.First + " " + name.Last).Trim();
This works for either or both being null and will not return a string with leading, trailing, or only spaces.

Categories