|
Foreach should not be used to change the contents of a collection. Use a for-loop instead.
www.troschuetz.de
|
|
|
|
|
But it isn't possible to go through a Dictionary with a for-loop. The only iteration tool available is the IEnumerator and that's what the foreach contruct uses!
|
|
|
|
|
Considering this restriction, I think the simpelst way to avoid the error would be to collect the keys of all elements that should be removed and then remove them after finishing the enumeration.
www.troschuetz.de
|
|
|
|
|
That's what I did, was just wondering if there was a better way to do it.
|
|
|
|
|
No, you are NOT.
You are looping over the Dictionary and removing entries at the same time.
Shove them into an ArrayList and remove them AFTER completion of the foreach-loop.
Cheers
Sid
Cheers,
Sebastian
--
Contra vim mortem non est medicamen in hortem.
|
|
|
|
|
Perhaps I wasn't clear enough. I've got a working version that does as you described. I thought the solution was ugly and wondered if there was a better one.
|
|
|
|
|
That's not true.
You can get the Keys collection and iterate over it with a for loop:
for (int i=0; i<myDict.Keys.Count; i++)
{
object o = myDict[Keys[i]];
} Regards,
mav
|
|
|
|
|
I think that this would be more inefficient than iterating using a foreach-loop as I'm interested in removing entries according to a condition applied to the value, not the key. The foreach-loop uses the collections enumerator which probably is the most effecient way to iterate through the collection as it makes use of the collections underlying implementation. But I could be wrong though.
Please post some code that would does the same things as the one I posted if you have the time.
|
|
|
|
|
If you have runtime considerations I don't know which version is faster.
But I just wanted to counter the statement that you can't iterate over a Dictionary with a for loop.
Your original code would look something like this:
for (int i=0; i<peers.Keys.Count; i++)
{
DateTime time = peers[peers.Keys[i]] as DateTime;
if (time < DateTime.Now)
{
peers.Remove(peers.Keys[i]);
i--;
}
} (written from memory, may contain typos/bugs).
Regards,
mav
|
|
|
|
|
My statement was perhaps a bit to general. I meant that it wasn't possible to iterate through the key-value pairs with a for loop. It is, just as you say, possible to iterate through the keys and values on their own.
|
|
|
|
|
I created a class inherited from TextBox and to add it into my forms I just added Windows TextBox objects then modified the code created by VS by modifying the type to my own textbox class.
This worked fine for a while; however, all of sudded all my controls dissapeared. This must have happened because I emptied the folder that contained my custom TextBox.
So to prevent this from happening in the future, what is the right way of adding custom controls to windows forms so it doesn't wipe them out automatically?
I also have some classes inherited from "System.Windows.Forms.UserControl" and the same problem happened a while ago, so before I start creating a form with a whole bunch of my custom controls I would rather figure out how to add them the right way to prevent them from disappearing.
Please don't tell me I have to add my controls to the GAC. That would be a pain. Reason is that a while ago I added some dll's to the GAC and when I wanted to step through my code, it wouldn't step in and I couldn't figure out the reason. Finally I removed my dll from GAC and then I could go in debug mode. Maybe I did something wrong here as well, so please feel free to comment.
|
|
|
|
|
Hi;
To add your custom control on to the toolbox in VS do the following steps:
1: Create a directory somewhere on your system where you will place your custom control dll's in. Do not delete this directory or it contents. If you do the controls will not show up in your Toolbox or any forms you put them on VS.Net.
2: Go to the Toolbox and select the tab you want the control to show up on, or create a new tab by right clicking on the Toolbox and select Add Tab from the menu and fill in the tab name at the bottom of the Toolbox and press enter key. Then click on the tab.
3: Right click on the Toolbox and select Add/Remove Toolbox Items...
4: In the Customize Toolbox dialog make sure that the .Net Framework Components tab is selected and then click on the Browse... button.
5: Now navigate to the directory you created in step 1 and double click on the custom control dll you want to add.
6: Your control should now have a check mark next to it and its version number at the bottom of the page. click the OK button.
Now any time you need your control just go to the toolbox and drag it on to the form.
I hope this has been of some help.
-Fernando Soto
|
|
|
|
|
Thanks, your detailed steps sure help.
|
|
|
|
|
Hi,
I have just installed MS SQL 2005 Express for testing purpose but a unabe t gn.. what user id and passwrd should use?
Many Thanks,
Jassim Rahma
|
|
|
|
|
I don't know, but if you didn't provide a password on installation, try user name sa and leave the password blank, you probably will get in.
-- LuisR
Luis Alonso Ramos
Intelectix - Chihuahua, Mexico
Not much here: My CP Blog!
|
|
|
|
|
Hi All,
I have a problem which is driving me barmy. I am writing a VS.NET integration package which refereces a API which contains a data layer and a web control layer. When the package is loaded by the studio and I attempt to invoke a form from UITypeEditor.EditValue on a property in a web control in design mode I can not cast the value parameter (which is a type from the API) from 'object' to its correct type, the layout below might help:
1) Package Directory:
API Assembly
Package Assembly
2) VS.NET ID Directory
API Assembly (needs to be here so the property grid can load it)
3) Web Project Running in design mode with the package loaded has a reference to the API Assembly (from the IDE Folder)
In UITypeEditor.EditValue I am doing this:
Criteria c1 = (Criteria)value; // Specified Cast is not valid (also if(value is Criteria) fails)
Criteria c2 = new Criteria();
In debug, the watch reveals the the 'value' object is infact a Criteria object, however they are loaded from two different assemblys. The versions are identical, but the locations are different:
The Location for 'value' comes from a temporary projects assembly folder (more than likely created and managed by visual studio), however 'c2' is coming from the API Assembly in the VS.NET Directory. This is probably because the call is within the context of the vs.net property grid which has loaded its dll from there.
Now, I am hoping there is a correct method of getting around this!
Regards, James Simpson
James Simpson
Web Developer
imebgo@hotmail.com
P S - This is what part of the alphabet would look like if Q and R were eliminated Mitch Hedberg
|
|
|
|
|
This may lead you in totally the wrong direction, but what if you were to specify your own assembly resolver using AppDomain.CurrentDomain.AssemblyResolve?
Does this allow you to specify where to load the assembly when using the designer?
As I said, beware of wild goose chases with regards to my suggestion.
Marc
MyXaml
Advanced Unit Testing
YAPO
|
|
|
|
|
Thank You Marc,
That was successful. I removed the DLL's from the IDE directory and handled the AssemblyResolve function in the package, returned the correct assembly and the value casts as it should.
Cheers!
James
James Simpson
Web Developer
imebgo@hotmail.com
P S - This is what part of the alphabet would look like if Q and R were eliminated Mitch Hedberg
|
|
|
|
|
I was looking through some dll's using dependancy Walker and alot of them are pointing toward a Watchdog.sys file. Looking through this file I see alot of monitoring and event functions.
What exactly does this file monitor?
And is there a way to interop into it from C#?
Thanks
Steve
Steve Welborn
Software Architect
|
|
|
|
|
I have been developing a Soap messaging application. I created a message handler class which was derived from from the SoapReciever class. I created a node by putting the class in my web server and added it to the web config file under httpHandlers. I can send soap messages and the recieving end is intended to write the messages to an XML file, the code is correct however in the end there is no file.
Does anyone know what I should do?
|
|
|
|
|
Hi , C# Fellows,
I am writting a program to deal with C++ Dll.
When I do stress testing , (run the program continously), at the end I got "Out of Memory" Exception , So I am thinking of adding some code to monitor memory change. But the prolem here is , I dont know when the Garbage Collector will kick in. So , Its critical to catch this event.How Can I do it in C#--???
Thanks
|
|
|
|
|
Is there something wrong with using the Performance Monitor built into Windows?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Well, stress testgin will last for a quite long time , and I can't stay there stare at it whole day.
|
|
|
|
|
It can log the data to a Perofmrnace log file. But since, in your other post, you said that you want to log what looks like every function call, Performance Monitor won't help you.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|