|
I am attempting to write a short program in C# that deals with resistances (in electronics). I would like to be able to parse string values into floating point values. The string can be in the format like 100, 4.7K, 2.2M, or shorthand equivalents like 100, 4K7, 2M2. They should never have more than 3 digits, at least one of which is a whole number part (no more than 2 decimals). Also I would like to be able to display them in similar formats. I've looked into MS documentation, and I'm more confused than ever. When do I need to use (if at all) IFormatProvider, ICustomFormatter, IFormattable, IParsable, etc. I've gone 'round and 'round, and I'm thoroughly at sea. I'm not a new programmer, just a retired one. I wrote code for about 30 years in everything from COBOL to VB.NET (including some C and C++). I don't need someone to write the code for me, but any hints would be much appreciated.
|
|
|
|
|
Hah been there done that one - try parsing Murex input. We had to build up a set of patterns to test the input against, it took ages to nail the bulk of the patterns that traders would enter as values and they were not limited to 3 characters.
Test each entry against your pattern list, use any found pattern to parse the values, throw an error if no match is found (and add it to your pattern list).
Oh and forget the existing tools as this falls way outside their design specs.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
You mean don't use any of those interfaces, just parse them myself? I'm already kinda of doing that, but I was hoping going the interface method might be more flexible.
|
|
|
|
|
I would also write my own Parser because the rules for this are clear. I'm not sure if I would try against a list because there are not much points to test against :
- 1st I would search for the multiplier (if there is one) - m for 0.001, k for 1000 and M for 1000000
- then I would test the value before the multiplier (if there is one)
- last I would look, if you have a multiplier if there is a value after it
-> now build you output-value
|
|
|
|
|
That's pretty much the way I am doing it now.
|
|
|
|
|
Tracy Dryden wrote: Also I would like to be able to display them in similar formats.
Two ways to do that after you have parsed.
First keep the original value as a string. You can create a class that has both the parsed value and the original value.
Second create your own format method(s) that take the value and normalize the display. Problem with this is you will need to deal with precision.
|
|
|
|
|
Me, wanting to "peek" the "last" entry in a ConcurrentQueue, because the "next" entry I will enqueue needs "prior info" if it exists.
I can use ToArray, and GetEnumerator. Surprising .Last() compiles but I thought I'd "Bing" anyway.
Me: peek last entry in concurrentqueue
Copilot with GPT-4:
Quote: Certainly! In C#, if you want to peek at the last entry in a ConcurrentQueue, you can use the TryPeek method. Here’s an example:
(Some "wrong" / right code).
Quote: In this example, the TryPeek method allows you to examine the last item in the queue without removing it. If the queue is empty, it returns false, and you can handle that case accordingly.
If you have any more questions or need further assistance, feel free to ask! 😊
MSDN re: TryPeek:
Quote: Tries to return an object from the beginning of the ConcurrentQueue<t> without removing it.
Seems it can't differentiate a queue from a stack. Nice. VS is also getting more "aggressive" when it's getting it wrong.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
It is not intelligent.
Marketing sells it like it is, but we are far away from thinking machines.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
The good thing about the new "AI" tools is that they always give a reasonable and convincing looking answer.... especially when they are wrong.
|
|
|
|
|
Besides AI getting it wrong:
1) If you have concurrent writers to the queue you do not know something else does not insert a new "last" item after you checked what it is. This can of course be solved with some locking, but then we are at the next point:
2) If you do not have concurrent writers you can just capture the last item to a variable as you enqueue it.
|
|
|
|
|
I'm having a problem with a MessageBox.Show() call.
It's being called from a window that was shown with Form.ShowDialog().
The problem is that the message box sometimes appears behind the dialog box, and makes the dialog box unresponsive until the message box window is found and OK is clicked on it.
This would be terribly confusing for users.
Is there any way to guarantee that a message box is displayed above the form that is calling it?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
The problem is that ShowDialog is a blocking call: it doesn't return control to the UI thread until the dialog has been dismissed. And MessageBox.Show is also a blocking call: it's the equivelant of calling ShowDialog on a "normal" form.
You don't really have many options here other than to create your own message box and call Show on that if you must be able to continue working with it visible.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
You need a different "pattern". If the "error" is critical, exit the dialog, then give the message; then "retry" based on ok / cancel. Or the "dialog" might function better as a "wizard" (next; back; cancel).
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
These are great ideas, thanks!
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
You're welcome!
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Assuming you're using WPF...
You could try my Customizable WPF MessageBox[^].
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
When you call MessageBox.Show(), you can pass the owner form as a parameter. This ensures that the message box will always be displayed above the owner form.
|
|
|
|
|
Thanks! I'll try this.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I am attempting to update a sql table based off the tables ID (which is self generated by sql when the row was first entered into the table. The below code is what I am trying to use but it will not update the row.
protected void ButtonUpdate_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=ucpdapps2;Initial Catalog=Offline_CAD;Persist Security Info=True;User ID=sa;Password=Propaganda9800!");
con.Open();
SqlCommand cmd = new SqlCommand("Update [Law_Responses] SET (Calltaker=@Calltaker,Date=@Date,City=@City,County=@County,Address=@Address) where TextBox171.text=@ID", con);
cmd.Parameters.AddWithValue("@Calltaker", DropDownList1.SelectedValue);
cmd.Parameters.AddWithValue("@Date", TextBox1.Text);
cmd.Parameters.AddWithValue("@City", DropDownList9.SelectedValue);
cmd.Parameters.AddWithValue("@County", DropDownList2.SelectedValue);
cmd.Parameters.AddWithValue("@Address", TextBox3.Text);
con.Close();
cmd.ExecuteNonQuery();
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Incident Was Updated');", true);
}
|
|
|
|
|
So little code and so many problems.
You never said what the error message was, but a quick glance shows this little gem:
con.Close();
cmd.ExecuteNonQuery();
Why would you close the connection to the database and then try to tell it to execute your query?
And then look at your query.
Update [Law_Responses] SET (Calltaker=@Calltaker, Date=@Date, City=@City, County=@County, Address=@Address) where TextBox171.text=@ID
How would your SQL server know anything at all about a TextBox in your app? It doesn't have any access to the controls in your app at all, let alone it's Text property, and it's very improbable that you have a column in your table called "Textbox171.text".
Since you're using parameters to pass values to the query already, I find it odd that you're trying, and failing, to use string concatentation to pass the value of a textbox to the query. The @ID value should also be passed as a parameter to the query.
You should also be giving your control names meaningful names and not accepting the default names in the designer. Given that this textbox is numbered 171, it seems you have a lot of work to do to make your code readable, understandable, debuggable, and supportable.
|
|
|
|
|
In addition to Dave's comments, you are also connecting to your server using the sa account. This is an unrestricted super-user account, which should never1 be used by applications. One mistake2 in your application code will lead to a hacker or disgruntled employee taking over your entire database server, and possibly your whole network.
Instead, either connect using Windows authentication, or create a specific SQL account for your application which has only the permissions required by your application.
Also, if that's your real sa password that you've just posted to a public forum, then change it immediately. And this time, pick or generate a strong password, rather than something that would likely be guessed in a few minutes.
1 The only exception would be for tools such as SQL Server Management Studio, and even then only if Windows authentication cannot be used.
2 Even without a mistake, if this is a desktop application, a user could easily decompile the code, extract the connection string, and take complete control.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I don't fully understand what functionality you want to achieve,but based on your code,you can try this:
SqlCommand cmd = new SqlCommand("Update [Law_Responses] SET (Calltaker=@Calltaker,Date=@Date,City=@City,County=@County,Address=@Address) where @Columname=@ID", con);
cmd.Parameters.AddWithValue("@Calltaker", DropDownList1.SelectedValue);
cmd.Parameters.AddWithValue("@Date", TextBox1.Text);
cmd.Parameters.AddWithValue("@City", DropDownList9.SelectedValue);
cmd.Parameters.AddWithValue("@County", DropDownList2.SelectedValue);
cmd.Parameters.AddWithValue("@Address", TextBox3.Text);
cmd.Parameters.AddWithValue("@Coumname",TextBox171.text);
con.Close();
cmd.ExecuteNonQuery();
|
|
|
|
|
Hi, I'm working in .NET 8 (not .NET Framework). Trying to programmatically create a file system link (LNK file) in C#.
I've added a reference to the Windows Script Host Object Model as detailed in many different online articles.
However, I can't seem to find the correct Using statement to import the classes in that reference.
When I say, "Using IWshRuntimeLibrary", it says it cannot resolve the type or namespace.
What must I do to import the types in the Windows Script Host Object Model reference?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
This worked fine for me in .NET 8:
using IWshRuntimeLibrary;
.
.
.
WshShell shell = new();
WshShortcut shortcut = shell.CreateShortcut(@"C:\Users\test\Desktop\Test.lnk");
shortcut.TargetPath = @"C:\Windows";
shortcut.Save();
I didn't do anything other than set the reference to the Windows Script Host Library.
|
|
|
|
|
Yes, I found that code on the web. But VS is not able to resolve IWshRuntimeLibrary even though I added the reference.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|