|
Toyota breaks brakes
|
|
|
|
|
0) Yes, foreach may be more readable
1) for is generally quicker than foreach .
2) In some cases, you can't use for .
3) With foreach , you can't alter the collection
4) Breaking out of a foreach is generally poor design
4.1) Same with for , but you can set the test appropriately
In conclusion, it depends on the situation.
|
|
|
|
|
PIEBALDconsult wrote: In some cases, you can't use for.
Sure you can:
var enumerator = (new List<int> { 1, 2 }).GetEnumerator();
for (; enumerator.MoveNext(); )
{
MessageBox.Show(enumerator.Current.ToString());
}
PIEBALDconsult wrote: With foreach, you can't alter the collection
Depends on the implementation, actually. The built-in .Net collections have logic that checks if a modification has been made and the enumerator will throw an error if it detects a change was made to the collection while in a foreach. However, about every 2 or 4 billion changes, this detection fails (it uses an int to keep a version of modifications made to a list, so it overflows and keeps looping like that), so sometimes it does not throw an exception (this is actually a bug I reported on Microsoft Connect). And you can create your own collection that doesn't have this type of logic. And you can make the modification, so long as you trap the exception (i.e., the list will remain modified, but you will break out of the loop). You can also break out of the foreach after you make the change (but before the next iteration) and no exception will be thrown. Similarly, you can make a change to the last item in the collection and that should prevent an error from occurring as well (as there is no next iteration). What you stated is generally correct, but I thought I'd let you know about a few of the nuances related to when it might not be.
|
|
|
|
|
aspdotnetdev wrote: for (; enumerator.MoveNext(); )
I hadn't thought of that. Now I'll try to forget it.
aspdotnetdev wrote: every 2 or 4 billion changes
I'd forgotten about that; it hardly seems worth remembering.
|
|
|
|
|
For each their own.
|
|
|
|
|
I am developing a resume builder software for users to build there resumes and export to MS word in different layout.. I thought of saving the resume details (personal, work experience, education, etc) in OpenXML but it seems I will be limited to one layout or it will too complicated to have different layouts as output.. what's your idea? save it in XML and manupulite to export as MS Word using MS Word Object? or binary database? or what?
|
|
|
|
|
I've always thought that a database would work best for that, but I've never actually tried to do it.
|
|
|
|
|
but the question why should I install SQL server express and add additional 200MB to my setup package? that's the only thing i don't like..
|
|
|
|
|
I don't like it either!
It's why I suggest VistaDB[^]!
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
i know about it... i tried it before BUT it's not FREE my friend...
|
|
|
|
|
jrahma wrote: add additional 200MB to my setup package
Don't. Just document that it requires SQL Server; the user may already have it anyway. Or, better yet, make it database agnostic.
Same with .net; don't include it in the installer, the user probably already has it.
|
|
|
|
|
200 MB is for the full package with management tools, something I doubt you would need for clients. Its 84 MB for just the database, still big but not as big. Of course you could also choose SQL Server Compact edition.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
hi, friends
i wrote a code in c#.net and it's ok.
but i have a problem which is running slow
i hope thare is some suggestions
my code is folow
static void calis(int rer, int dizin)
{
while (it < iterasyon && mmse <= 0.00001)
{
mmse = 0;
mae = 0;
for (int nf = 0; nf < orneksay; nf++)
{
mmse += hesaplas(ndata, nf);
trains(nf);
}//nf
mmse = 0.5 * (mmse / (orneksay * nron));
if (it % 100 == 0)
{
yazs.WriteLine(mmse.ToString());
yazs.Flush();
}
it++;
}//it
dosyas.Close();
yazss.WriteLine(rer.ToString() + " " + mmse.ToString() + " " + it.ToString());
yazss.Flush();
dosyass.Close();
}
}
i call calis function and i see cpu usage %25
mey it improve its performs ?
can you help me?
regads.
modified on Thursday, April 1, 2010 2:53 AM
|
|
|
|
|
Please edit your original post, add PRE tags around the code so it gets all formatted and readable, and check some of it, I don't think while (it < iterasyon mmse <= 0.00001) could ever compile.
|
|
|
|
|
Your code is very confusing. As Luc suggested, I wonder if it would compile.
karayel_kara wrote: while (it < iterasyon && mmse <= 0.00001)
{
mmse = 0;
mae = 0;
for (int nf = 0; nf < orneksay; nf++)
{
mmse += hesaplas(ndata, nf);
trains(nf);
}//nf
I see the delay happening in this block. But we cannot help you till the time we don't know the initial value of mmse and what does hesaplas(ndata, nf) do?
Also, what is the value of orneksay?
There are so many loopholes here.
Suggestion:
Add two int counters. one in while loop and one in for loop just to notice how many times does this loop run. If this number is too high, you should probably consider ways to reduce them.
|
|
|
|
|
thanks for reply.
i take notice of a point,
when i look at cpu using, i observer my code is using cpu %25 and i have quad pc so i can't use %75 of my cpu performance
is there this problem solution ?
not: orneksay=dataset
|
|
|
|
|
No buddy... it is not.
You are working on a single thread and it will try to squeeze as much time of processor as it can. I wouldn't suggest trying multithreading till the time you haven't sorted number of loops.
I have a feeling that its just number of loops. Could you do that counter and tell us how many times while loop runs and how many times for loop runs?
|
|
|
|
|
I have generated the AXLAPIService.cs file through wsdl.exe as the CSCO readme states. I have made all of the associated changes that are listed throughout the readme as well. Everytime the constructor is called an InvalidOperationException occurs. Currently the Inner Exception is "There was an error reflecting type 'APIRequest'.". I have regenerated the class modifying the namespace as well twice now. Has anyone seen this type of error? Is it a namespace issue?
The exception message reads "There was an error reflecting type 'RemoveLineReq'." Earlier it was on another type before I regenerated the class. Any help is appreciated.
|
|
|
|
|
Hi All,
I wrote some simple application with thread.
I getting exceptoin that say 'Specified cast is not valid.'
I dont know what i did wronge.
Code attached.
Thanks.
public struct SomeStruct
{
public Int32 t1;
public Int32 t2;
public SomeStruct(Int32 t1_, Int32 t2_)
{
t1= t1_;
t2= t2_;
}
}
private void Thread1(Int32 T1, Int32 T2)
{
object t = new SomeStruct(T1, T2);
Thread RunningThread= new Thread(Mathod1);
RunningThread.Start(t);
}
public void Mathod1(object ap)
{
Mathod2(((SomeStruct)ap).T1, ((SomeStruct)ap).T2);
}2
|
|
|
|
|
What's the signature of "Mathod2"?
|
|
|
|
|
|
Ok, then time to trace it...
Set a breakpoint on the line that's crashing, and verify that the parameter being passed is the proper type.
|
|
|
|
|
did it already - the object is fine and the parameters are there.
|
|
|
|
|
Have you tried breaking it down?
SomeStruct ss = (SomeStruct)ap;
Mathod2(ss.T1, ss.T2);
Functionally no different, but should confirm exactly where the problem is.
If the exception happens on the first line, then double-check "ap", because it isn't the right type. If the exception happens on the second line, then either your Mathod2 signature is wrong, or you're hitting a problem inside Mathod2()
(By the way, it's "Method", not "Mathod" )
|
|
|
|
|
The exception happaned in the first line
The exception is System.InvalidCastException was unhandled.
|
|
|
|