|
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?
|
|
|
|
|
Use a container to which you can add items dynamically. For example, you can use a Dictionary<string,DateTime> . Service names will be keys to this dictionary; values would correspond to your dtPreviousService##Time variables. Documentation is here[^].
|
|
|
|
|
Store the previousServiceTime value in the ServerMonitorClass
"You get that on the big jobs."
|
|
|
|
|
|
Having multiple threads in the application to use the same helper class what is the better approach to code it in terms of performance and thread safety? static or non-static?
There is only one method in class.
static public class Img
{
static public Bitmap Crop(Bitmap image, Rectangle rect)
{
Bitmap cropped = new Bitmap(rect.Width, rect.Height, image.PixelFormat);
using (Graphics g = Graphics.FromImage(cropped))
{
g.DrawImageUnscaledAndClipped(image, rect);
return cropped;
}
}
}
Will the access to image parameter be safe? Each thread has its own image object created and disposed.
Will there be the performance bottleneck in that function?
Чесноков
|
|
|
|
|
According to the docs, Graphics.FromImage[^] is thread-safe (thread safety section is at the bottom of this very long doc). Your code does not have or need explicit locking, so it would not create additional synchronization bottlenecks. However, in the unlikely case that Graphics.FromImage uses explicit synchronization, your code would become a synchronization bottleneck implicitly.
|
|
|
|
|
Are the parameters passed to static Crop() function are also thread safe inside the function body similar to local variables? Bitmap image is a reference type, each thread creates it inside its worker method and passes to Crop() .
Чесноков
|
|
|
|
|
As long as threads do not share instances of Bitmap image (as you indicated), there is no difference between local variables and parameters.
|
|
|
|
|
There should be no problem with your static method as long as it uses the image parameter only for reading purposes. Problems might arise if any other thread of your application is also attempting to write into of this object, remember, the one referenced by the image parameter.
|
|
|
|
|
I'm disturbed about static function reference object parameters thread safety.
Local parameters in the function are safe, they are created for each thread, e.g. cropped image.
2nd parameter is value object Rectangle is also safe, copy is created as you pass it.
1st parameter is reference object Bitmap. There is no writing to that parameter as you can see in the code. There should not be any difference if you modify it either if it is safe.
My question is whether parameters to static function are passed in a thread safe approach?
That is do they have the same thread safety as local variables in that static function body?
Чесноков
|
|
|
|
|
Chesnokov Yuriy wrote: whether parameters to static function are passed in a thread safe approach?
the question doesn't make sense. there is no such thing as "passing parameters in a thread-safe way".
parameters get handled by the method called, whether it is static or not.
and they exist on behalf of the caller, who could or could not share them with other code on other threads.
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.
|
|
|
|