|
That is unformatted and unreadable. I think I saw some CreateGraphics which is probably very wrong. Please edit your question to include PRE tags, then read this[^].
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
My list collection has several items.
I would like to add/update this collection list with the value I have while looping through the collection using foreach...
How can I check if the value already exists (update it) and if it does not exist, then add it?
Do I have to have another collection or array to hold the unique values while looping through the collection list?
Thanks
|
|
|
|
|
You can use a HashSet<T> to hold unique values, but adding elements to any collection while looping through it is not a good practice. Tell us a little more about what you want to do, and maybe we can give you a better approach.
|
|
|
|
|
Basically my existing list is as follows:
Basically my existing list is as follows:
private List<monitor> results = new List<monitor>(); //hold the messages...
There are fields in monitor class i.e. Field1, Field2, Field3, ...
private void UpdateList(string strField1Value)
{
//loop through results collection
//chek if Field1 in results is as same as strField1value
//if strNewvalue is already present in results collection, then only update that row with the new fields
//else add the new row to the results collection...
}
|
|
|
|
|
Ok, then use a Dictionary<TKey, TValue> instead of a List or HashSet. Use the field1 as the key of that dictionary, and the monitor objects as values. This way you will not need to check anything:
Dictionary<string, monitor> results = new Dictionary<string, monitor>();
void UpdateList(monitor mon)
{
results[mon.Field1] = mon;
}
As it is that simple, you can even delete the UpdateList method and just modify the Dicionary as required. No loop needed for this.
|
|
|
|
|
Dictionary<string, monitor> results = new Dictionary<string, monitor>();
It says, Tkey, Tvalue is required
|
|
|
|
|
Yes, sorry, I made a little mistake. I've fixted it, read the post again.
|
|
|
|
|
Yes it needed a little fix.
I'd appreciate you don't do stealth fixes though, as they may make a thread incomprehensible.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Yes, you're right. I am quite an absentminded person . I will try to quote my fixes in the future. Thanks for the advise.
|
|
|
|
|
A warning: if the key value (strField1Value) of some object changes after it has been added to the dictionary, then the dictionary is no longer valid, i.e. it needs updating (removing and re-inserting the object).
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
I might do it this way:
public class MyCollection : List<MyItem>
{
public bool AddUnique(MyItem newItem)
{
bool found = false;
if (!this.Contains(newItem))
{
foreach (MyItem item in this)
{
if (item.MyProperty == newItem.MyProperty)
{
found = true;
break;
}
}
}
else
{
found = true;
}
if (!found)
{
this.Add(newItem);
}
return found;
}
}
".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 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
I think I asked the same question like a week ago or so and you offered me this solution so basically him and I requeired the same thing. if you didnt responce to him I would have responded. good call tho.
|
|
|
|
|
Sorry John, but this isn't a great solution if the size of the data gets too large. Basically, every time you add a record you increase the amount of time it takes to search for a new item just to see if it can be added. I recently had to rework some code that somebody had put together like this to find and add files into a list of monitored files. As you can imagine, it works fine when there are only a few items in the list, but the algorithm completely falls apart when you start getting to the few tens of thousands.
A simpler way is to use a Dictionary. As in:
private Dictionary<string, MyItem> _items = new Dictionary<string, MyItem>();
public void Add(MyItem value)
{
if (!_items.ContainsKey(value.MyProperty))
{
_items.Add(value.MyProperty, value);
}
} Alternatively if the OP is using .NET 3.5+, a HashSet provides a good choice because it removes the duplication where the dictionary contains MyProperty both in the value, and in the key.
|
|
|
|
|
I merely provided a solution. He didn't specify how big the collection would get, but until said collection gets to an unwieldy size, my answer is fine, especisally given his apparent lack of programming ability is concerned. Besides that, "duplicate" can mean any number of things, and my solution covers the possibilites.
He could use a HashSet with some extension methods, but honestly, that's beyond his level of skill. I was merely considering the audience, that's all.
".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 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
hi m doing work with AT commands with c# could not find any AT command for voice msg. there are AT commands for SMS and voice call also but i could not find any command for voice msg. is it possible to snd or receive voice msg through AT commands???
|
|
|
|
|
What exactly do you try? ATDT...... (...... is the desired number) establishes a connection, if we both are talking about old Hayes compatible modems.
maybe this link is enlightening
http://www.cellular.co.za/hayesat.htm
I cannot remember: What did I before google?
|
|
|
|
|
I have an arbitrary question regarding WIA Property Constants. I`ve worked through numerous examples where these constants get set, but I don't fully understand HOW it is really set. If you simply declare "private const int WIA_DPS_HORIZONTAL_BED_SIZE = 500" in your code, does this set the actual property by just declaring it like that?
|
|
|
|
|
HI there!!.
I have got a project in which i have to get the data of rfid card through a rfid reader connected through rs232 serial port to the computer and then further manipulate the data...i know i have to use the IO.Ports ...but the thing is i have got so many buks of c# , but whenever the chapter comes about IO.. there is nothing written about IO.ports!!...i just have to read the data from the serial port by detecting the appropriate com port to which its connected.. there are few tutorials but are in bits and pieces and is becoming real hard to understand...please help me refering to some good complete tutorial regarding this..and if you people know some c# book in which IO.Ports is explained then please tell.
Thanx in advance!
|
|
|
|
|
It isn't covered in much detail because there isn't a whole lot to it!
There are articles here which may help (try Serial Communication using C# and Whidbey[^])
There are also many more on other sites: Google is your friend here: http://www.google.co.uk/search?sourceid=chrome&ie=UTF-8&q=SerialPort+tutorial+c%23[^]
It is pretty simple: the complexity comes when you actually talk to the device, and no book or tutorial will cover that!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
|
|
|
|
|
Who the hell uses RS232 these days?
------------------------------------
I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
CCC Link[ ^]
Trolls[ ^]
|
|
|
|
|
People who don't want to pay for the LAN hardware and protocol stack processing overheads!
Seriously, in embedded computing, you can fit RS232 for a few tens of pence, whereas the connector alone for a network cable can cost twice that before you get to the hardware behind the connector. Particularly when you are talking about low volumes.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
|
|
|
|
|
i couldn't find any rfid reader at low cost, with any ither interface...the one with rs232 was the most common one..thats why i sticked to it...will try to understand the tutorial...will atch back i i encounter problems(which i definitely will! )
thnx
|
|
|
|
|
Good luck! (It won't be that bad, honest!)
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
|
|
|
|
|
I have developed a windows service (MainService) which monitors the messages sent from other windows services.
The MainService knows which services to monitor by reading The services names from settings.xml file.
At present in the settings.xml, there are several services names i.e. service1, service2, etc... which the MainService reads (using a foreach loop).
As you see below, the code shows how the MainService reads the settings and checks the previous time of the service message...
Question:
Everytime I add a new service name to the setting.xml, I have to duplicate the below code for the new service (But with new related variables i.e. _dtPreviousService?Time), etc ...
How can I make the code dynamic, so that if a new service name is added to the settings.xmml, then I do not have to add/duplicate code just to check the new service?...
Hope you see what I am trying to achieve?
Thank you
The c# code in the MainService is something similar to the following:
_dtPreviousService1Time datetime;
_dtPreviousService2Time datetime;
...
...
foreach(ServerMonitor item in monitorSettings)
{
if (item.Servicename == "service1")
{
if (_dtPreviousService1Time != DateTime.MinValue)
{
TimeSpan span = PresentTime.Subtract(_dtPreviousService1Time);
dblTimeDifference = span.TotalSeconds;
_dtPreviousService1Time = PresentTime;
}
else
{
_dtPreviousService1Time = DateTime.Now;
}
break;
}
elseif (item.Servicename == "service2")
{
...
...
...
}
}
...
...
...
|
|
|
|
|
using a collection/dictionary/list/array of DateTime?
I cannot remember: What did I before google?
|
|
|
|