|
You could have added a counter to the "values" side of the dictionary: e.g.
Dictionary<string, Tuple<int, int>>.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
The simplest way is to create a dictionary that holds a tupple:
Dictionary<string, (int, double)> dict = new Dictionary<string, (int, double)>();
Then when you add your items, you check if the key name is already there.
If it isn't add it with a int of 1 as the count, and a double which is the value:
dict.Add("Alex", (1, 20.0));
If it is, increment the integer, and sum the new value :
dict["Alex"] = (dict["Alex"].Item1 + 1, dict["Alex"].Item2 + 17.5);
You can then work out the averages very simply in a loop when all items are added.
"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!
|
|
|
|
|
In C# generic Dictionaries do not allow duplicate Keys.
You could use another data structure; for example:
List<KeyValuePair<string, double>> ScoreList = new List<KeyValuePair<string, double>>();
Once you've got all your data entered, to average use Linq:
double averageOfAll = ScoreList.Average(kvp => kvp.Value);
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
Hello Guys,
I need to build a comma separated string from 100+ different field(few are string and others are either int or decimal), here each field should be in specific order:
Solution i can imagine are:
1)Define a string builder and Call methods for each field to fill the string builder
2)build each item in list and then call for string.join
3) Call for string.Format...
I am not able to decide which method is more efficient and maintainable.
Any thought on this? may be better approach ?
|
|
|
|
|
There are plenty of CSV libraries in Nuget. Why roll another one when it's been a repeatedly solved problem?
|
|
|
|
|
In addition to the excellent advice that Dave gave you, there is an easy way to join any number of fields with a common delimiter.
If you have the fields in an array or a List<> object, you can call string.Join(",", Fields.ToArray())
That method gives you a single string with all the elements separated by commas.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Depends on where the "100+" fields are coming from. Trying to spec a solution to an ill-defined problem.
(I'm currently using Excel to create "data tables" which are then exported as CSV files for use as embedded resources in another app).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Hi,
I'm using DevExpress Spreadsheet. I divide a number by another number and convert the result to string and then I put it in a cell in spreadsheet. The problem is that the output is an integer, not a double value. How can I fix it?
|
|
|
|
|
Try:
int x = 95;
int y = 3;
string str = ((double) (x / y)).ToString("F2"); Or
int x = 95;
int y = 3;
string str = ((double) x / (double) y).ToString("F2"); Depending on the result you wanted.
"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!
|
|
|
|
|
I did test it:
MessageBox.Show(((double)(31 / 2)).ToString("F2"));
But the result shows 15.00
How can I solve it?
|
|
|
|
|
Use the other one?
When you write a numeric constant with no fractional component: 31 say, or 2 you are defining an integer constant. Which means that any operations you do with them use integer math: integer divided by integer gives the same type: integer - and any fractional part is discarded.
So 31 / 2 is 15 and casting the result to a double gives the double equivalent or the integer: 15.0
If you want floating point operations, you need at least one floating point operand!
Hence my giving you two versions: one uses integer math, the other floating point.
"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!
|
|
|
|
|
Thanks. 
|
|
|
|
|
And it's taken you a week to test it?
"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!
|
|
|
|
|
Why not put the double value in the cell rather than converting it to a string?
|
|
|
|
|
The result is integer even when I don't convert it to string value.
|
|
|
|
|
Well without seeing your code it is impossible to guess the answer.
|
|
|
|
|
I have a notification icon running on the taskbar and it would only show its context menu when I right-clicked it.
I wanted to make it show the context menu when clicking the left mouse button, also.
So I attached a handler to the MouseClick event with the following code:
_NotifyIcon.ContextMenuStrip.Show(Control.MousePosition, ToolStripDropDownDirection.AboveRight); This shows the menu properly, but there is one problem.
The menu doesn't go away if I click on another window without choosing an item from the menu. Right now, the only way to make the left-click-shown menu go away is to click a menu item or right-click the notification icon.
The menu that appears when right clicking the icon goes away properly if you click another window on the desktop.
Does anyone know how to make the menu go away if the user clicks another window?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Hi Richard,
is this WinForms? That behavior is unknown to me.
I have menus on NotifyIcons in WinForms, they show automatically with either left or right click, simply by attaching an event handler to the Click event, like so:
notifyIcon.Click += new EventHandler(notifyIcon_Click);
notifyIcon.Visible = true;
...
void notifyIcon_Click(object sender, EventArgs e) {
populateContextMenu();
MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu",
BindingFlags.Instance | BindingFlags.NonPublic);
mi.Invoke(notifyIcon, null);
}
and that is no surprise, the Click event works for both mouse buttons; if you want to get different behavior, you would have to check MouseEventArgs.Button inside the Click handler.
edited, essential code in click handler was missing
Luc Pattyn [My Articles]
If you can't find it on YouTube try TikTok...
modified 23-Jul-21 10:02am.
|
|
|
|
|
Thanks Luc, I'll give that a try.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
It works! Thanks!
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
You're welcome.
Luc Pattyn [My Articles]
If you can't find it on YouTube try TikTok...
|
|
|
|
|
DataGirdView show black screen on some computers.
I don't know how to solve.
DataGirdView setting
this.dGVMemberMain_MemberList.AllowUserToAddRows = false;
this.dGVMemberMain_MemberList.AllowUserToDeleteRows = false;
this.dGVMemberMain_MemberList.AllowUserToResizeColumns = false;
this.dGVMemberMain_MemberList.AllowUserToResizeRows = false;
this.dGVMemberMain_MemberList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dGVMemberMain_MemberList.BackgroundColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
dataGridViewCellStyle1.Font = new System.Drawing.Font("굴림체", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(129)));
dataGridViewCellStyle1.ForeColor = System.Drawing.Color.DarkBlue;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dGVMemberMain_MemberList.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
this.dGVMemberMain_MemberList.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window;
dataGridViewCellStyle2.Font = new System.Drawing.Font("굴림체", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(129)));
dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText;
dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
this.dGVMemberMain_MemberList.DefaultCellStyle = dataGridViewCellStyle2;
this.dGVMemberMain_MemberList.GridColor = System.Drawing.SystemColors.ControlLight;
this.dGVMemberMain_MemberList.Location = new System.Drawing.Point(7, 70);
this.dGVMemberMain_MemberList.Name = "dGVMemberMain_MemberList";
this.dGVMemberMain_MemberList.RowHeadersVisible = false;
this.dGVMemberMain_MemberList.RowTemplate.Height = 23;
this.dGVMemberMain_MemberList.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
this.dGVMemberMain_MemberList.Size = new System.Drawing.Size(758, 222);
this.dGVMemberMain_MemberList.TabIndex = 14;
|
|
|
|
|
To be honest, we really can't help you - you need to start by looking at the computers where it fails, and the computers where it doesn't, and try to establish some connection or difference which separates them into the two groups.
We can't do that: we can't run your code here and see your problem, and we can't access teh computers where it works or doesn't!
Sorry, but ... there is nothing we can do!
"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!
|
|
|
|
|
Thank you.
Start some computer Windows 10 installation.
now~~

|
|
|
|
|
You're welcome!
"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!
|
|
|
|