|
I think you're right on the life-critical aspect. I don't believe there is anything in .net that will give you that granularity.
I don't know if my idea fits your needs, or is even a step down the right path. In the solution I was talking about, we knew every time an action was taken exactly what the next 'automatic' action would be, and exactly when it would occur.
However, for us, not so time-critical. So, even though we were able to come up with a simple 'if this happens, write this event and time to the database' scenario, our server wouldn't necessarily notice that event right at the time written.
But, it did turn out to be a really simple solution to a timer-type problem.
I suppose in today's world, you'd just go ahead and use a Timer and tie it to the event you wanted to perform.
Anyway, good luck.
|
|
|
|
|
FWIW: In a Windows Service that I wrote awhile back, I used the following:
// Initiate our System Timer (set to one minute here)
double dblStartupInterval = 60000;
this.timProcessTimer_PollPV = new System.Timers.Timer (dblStartupInterval);
// Note that everything done by the Timer is tied to
// the Elapsed Event, which is raised every interval.
// Instruct the Framework to call
// the "ServiceTimer_PollPV_Tick" method when this
// Timer elapses -- that is, when its interval runs its course.
this.timProcessTimer_PollPV.Elapsed += new System.Timers.ElapsedEventHandler(this.ServiceTimer_PollPV_Tick);
|
|
|
|
|
I googled this extensively yesterday and didn't come out with an answer:
Is it possible to do some thing like this, and if so how?
labela1.Text = "something";
labela2.Text = "something";
labela3.Text = "something";
Now what I want is a way to write that code as say:
for (int i=1;i<4;i++)
labelai.Text = "something";
so that the i in labelai above is replaced by 1, 2, 3 at runtime and the net result of the for loop is the same as what happens in the block above.
Is there a way to do that?
Thanks!
|
|
|
|
|
Yes, however, you may not like it.
foreach(Control control in Controls){
if(control is Label){
((Label)control).Text = "Something";
}
}
Unfortunately this will set all labels. If you are in Windows forms you can use the Tag option to set a tag, or you can subclass your Label control to give it a type.
The other option is, after Initialize component, place all Labels you care about looping through in a collection.
|
|
|
|
|
Here's a refinement that will only touch all labels you care about:
foreach (Control control in Controls)
{
if (control is Label && label.Name.StartsWith("labelNamePrefixToModifyAsAGroup"))
{
((Label)control).Text = "Something";
}
}
|
|
|
|
|
|
which can be reduced to
foreach (Control c in Controls) if (c.Name.StartsWith("labela")) c.Text = "Something";
as every Control has a Name and a Text property.
|
|
|
|
|
Slaps self on head for missing that one.
|
|
|
|
|
Reference parameters are your friend:
private void SetLabelText(ref Label label, string text)
{
label.Text = text;
}
private void SomeFunction()
{
SetLabelText(ref label1, "something1");
SetLabelText(ref label2, "something2");
SetLabelText(ref label3, "something3");
}
.45 ACP - because shooting twice is just silly ----- "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 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
No need for 'ref', unless you want to change what the label1, label2, and label3 variables point to.
|
|
|
|
|
for (int i=1;1<4;i++){
this.Controls[string.Concat("labela", i.ToString())].Text = "something";
}
|
|
|
|
|
No-one with a LINQ version yet?
class Form1
{
IEnumerable<Control> summingLabels;
public Form1()
{
InitializeComponent();
summingLabels =
from c in Controls.Cast<Control>().AsQueryable()
where c.Name.StartsWith("sumLabel")
select c;
foreach(Control l in summingLabels)
l.Text = l.Name;
}
}
Keep a pointer around to the controls that you found; no need to iterate them every time
I are Troll
|
|
|
|
|
how about lambda?
foreach (Control l in Controls.Cast<Control>().Where(c => c.Name.StartsWith("sumLabel")))
l.Text = l.Name;
|
|
|
|
|
|
Or Maybe to make it nicer with no casting and type checking:
summingLabels =
from c in Controls.OfType<Label>()
where c.Name.StartsWith("label")
select c;
foreach(Label l in summingLabels)
l.Text = l.Location.ToString();
|
|
|
|
|
Yup, works, and more concise and readable
I are Troll
|
|
|
|
|
The correct answer is: don't do that
If you need multiple storage locations (variables) and access them by index, use an array.
If you really have multiple variables (e.g. because they are generated by the VS forms designer), then you can still put those into an array:
Label[] labels = { labela1, labela2, labela3 };
|
|
|
|
|
|
David Knechtges wrote: Is there a way to do that?
No, at least if I understand what you want... However, why would you want to do that? What's wrong with:
Label[] labela = {
labela0,
labela1,
labela2,
labela3,
};
for (int i=0;i<4;i++)
labela[i].Text = "something";
modified on Wednesday, September 1, 2010 5:55 PM
|
|
|
|
|
|
You can also try reflection if you really really really really really really really really must do it this way and not using array/collection (depending on a situation).
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
|
|
|
|
|
Try to use this construction:
this.GetType().GetField("variablename")
This should work for c# but better try to avoid such kind of code. Other languages has different RTTI, for example in c/c++ this is impossible at all.
|
|
|
|
|
Not sure about the performance implications of FindControl.. But this one is pretty close to what you originally wrote
for (int i=1;i<4;i++)
(Controls.FindControl("labela" + i.ToString()) as Label).Text = "something";
|
|
|
|
|
This is not exactly what you were asking for but is an alternative using binding. This may be overkill for what you are trying to do but it would be more elegant. Bind your labels as you create them to an object of a class that implements INotifyChanged then when you change the property of the object each label will be updated without having to work loops.
http://stackoverflow.com/questions/639894[^]
Dave
|
|
|
|
|
Array.ForEach(new Control[] { labela1, labela2, labela3 }, delegate(Control c) { c.Text = "something"; });
|
|
|
|