|
Paul you beauty! Thank you so much, you just made my week year!
|
|
|
|
|
No problem. Happy to help
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
Does anyone know how to best lock access to the object during enumeration? Eg, is it a bad/good idea to use a Monitor.Enter in the constructor and a Monitor.Exit in the dispose function? I know, I could put a lock around, but I really need this to happen automatically.
Cheers.
|
|
|
|
|
Check out this [e-book]. It has some really nice examples on (automatic) locking and when (not) to use it in chapter 2. You might want to look at the Mutex class.
Standards are great! Everybody should have one!
|
|
|
|
|
|
When you compile this code:
IEnumerable<string> Test() {
lock (someObj) {
yield return "a";
yield return "b";
}
}
Then the C# compiler creates an enumerator class that calls Monitor.Enter on the first MoveNext call; and calls Monitor.Exit in the third MoveNext call, or in the Dispose method if Dispose is called between after the first MoveNext call and before the third.
foreach will automatically dispose the enumerator, but I've seen people write code like
IEnumerable<MyType> myCollection = ...;
if (e.GetEnumerator.MoveNext()) {
So yes, using Monitor.Exit in the Dispose function is the way locking is meant to happen in enumerators, but make sure that your team is aware of the fact that enumerators must be disposed!
|
|
|
|
|
I am trying to find every control on my page with "txt" in the id, they are all my textboxes. The following bit of code is working fine and duly goes into the inner condition for some of the textboxes on the screen. But not all.....will this only work on textboxes that are visible at runtime as I have some sent to txtBox.visible = false at startup.
foreach (Control c in Page.FindControl("MyPage").Controls)
{
if (c.ID != "" && c.ID != null && c.ID.ToString().IndexOf("txt") > -1)
{
TextBox txtClearBox =new TextBox();
txtClearBox.ID = c.ID;
txtClearBox.Text = "";
}
}
Any advice on clearing all my textboxes would be great.
Thanks in Advance
|
|
|
|
|
Instead of checking that the name isn't null, and that it contains "txt"
Can you not use Control.GetType() and see if it is a textbox?
And... are you making a new textbox everytime you want to clear it? Why not just set the text to ""?
My current favourite word is: PIE!
Good ol' pie, it's been a while.
|
|
|
|
|
No, not a new textbox, just want to clear the existing textboxes when event 'A' or 'B' happens. There are a few and obviously I could say txtBoxA = "";txtBoxB = "" etc, there are about 20.
I'm gonna try Control.GetType now, I'll let you know, thanks for the quick response Undefeated.
Harvey
|
|
|
|
|
this should do it (untested).
foreach (Control c in Page.FindControl("MyPage").Controls)
{
if (c is TextBox)
{
c.Text = String.Empty;
}
}
"On one of my cards it said I had to find temperatures lower than -8. The numbers I uncovered were -6 and -7 so I thought I had won, and so did the woman in the shop. But when she scanned the card the machine said I hadn't.
"I phoned Camelot and they fobbed me off with some story that -6 is higher - not lower - than -8 but I'm not having it."
-Tina Farrell, a 23 year old thicky from Levenshulme, Manchester.
|
|
|
|
|
thanks guys, I've got to that point, that works great. However the code does not find any of the textboxes if they are in an asp:panel
<asp:label id="lblRaisedBy">Raised By
<asp:dropdownlist ="ddlraisername"="">
<asp:textbox id="txtOrganisation">
<asp:panel id="pnlRaiser" runat="Server" visible="True">
<asp:label id="lblOrg" runat="server" width="7em" cssclass="NonMandatoryLabel" visible="true">Organisation
<asp:textbox id="txtRaiserName">
txtOrganisation is found whereas txtRaiserName isn't. Why is the panel causing a problem?
|
|
|
|
|
Because the textbox is a control on the panel, not the form, so you'd have to do this:
foreach(Control c in Page.FindControl("MyPage").Controls) {
if(c is TextBox)
c.Text = "";
if(c is Panel)
foreach(Control d in c) {
if(c is TextBox)
c.Text = "";
}
}
And loop through each control in the panel
My current favourite word is: PIE!
Good ol' pie, it's been a while.
|
|
|
|
|
It seems the foreach inside each other doesn't work..
foreach statement cannot operate on variables of type 'System.Web.UI.Control' because 'System.Web.UI.Control' does not contain a definition for 'GetEnumerator', or it is inaccessible
Is that right?
|
|
|
|
|
Oops, made a slight error it should be:
foreach(Control d in c.Controls) {<br />
...
And it then should of course be:
if(d is textbox)
...
not if(c is textbox)
foreach(Control c in Page.FindControl("MyPage").Controls) {
if(c is TextBox)
c.Text = "";
if(c is Panel)
foreach(Control d in c.Controls) {
if(d is TextBox)
d.Text = "";
}
}
My current favourite word is: PIE!
Good ol' pie, it's been a while.
|
|
|
|
|
Hey, that works like a dream, thanks a lot for that.
I now know something I didn't, what more can I want.
Thanks again.
Harvey
|
|
|
|
|
Hi all,
I would like to know how I can get out of this loop the first time the condition is true?
public string DirSearch(string sDir)
{
try
{
string compareString = "";
foreach ( string dir in Directory.GetDirectories(sDir))
{
foreach ( string file in Directory.GetFiles(dir))
{
compareString = file.Substring(file.LastIndexOf("\\"),(file.Length-file.LastIndexOf("\\")));
if (globalFileName == compareString.Substring(1, (compareString.Length - 1)))
{
return file;
}
}
DirSearch(dir);
}
return "";
}
catch (System.Exception excpt)
{
MessageBox.Show(excpt.Message,"Exception Occured",MessageBoxButtons.OK,MessageBoxIcon.Error);
return "";
}
}
Many Thanks
Regards,
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
AFAIK the only way to break out of foreach look is with a "break" statement.
There are some interesting discussions, and yes blogs, around the net about foreach versus for loops in .NET. You might want to check them out.
|
|
|
|
|
Hi Mike,
Thanks for your reply and comments but, break does not work. I have tried it without success.
Regards,
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
Programm3r wrote: Thanks for your reply and comments but, break does not work. I have tried it without success.
works for me
string[] test = {"help","me","with","for","loops"};
foreach(string t in test)
{
Console.WriteLine(t);
if( t.Equals("for"))
break;
}
|
|
|
|
|
Programm3r wrote: if (globalFileName == compareString.Substring(1, (compareString.Length - 1)))
{
/* File was found */
return file;
}
if you mean this bit, it already does by virtue of the return keyword.
In general you can break a look by using the break keyword. For completeness you may also like to look up the continue keyword in c#
|
|
|
|
|
you can use also "goto"
short example:
string filename = "";
for(int i = 0; i < 10; i++)
{
if(i == 3)
{
filename = "path"
goto FileHasFounded;
}
}
FileHasFounded:
MessageBox.Show("file name is this: " filename);
break don't work in this case because you have two loops. and it will break only from one loop
hope it will help.
respect.
spaps
|
|
|
|
|
Shpendh wrote: you can use also "goto"
The number of valid uses of goto in a language as rich as C# is negligible. In fact I can't think of any time I've used a goto in any language in the last 10 years.
Upcoming FREE developer events:
* Developer! Developer! Developer! 6
* Developer Day Scotland
My website
|
|
|
|
|
Exactly what I was thinking.
Number of times ive used goto in c#: zero!
|
|
|
|
|
Breaking out of a couple of nested loops? (I always wanted to be able to name loops, or do a break(2); or something similar)...
You're right though - theres very very few usages of goto that are "legitimate".
|
|
|
|
|
if (usingGoto)<br />
{<br />
goto jailAndDoNotPassGo;<br />
}
|
|
|
|