I am currently developing a windows form application and I have a arraylist notificationList that contains notificationForms.
How do I use a for loop to loop through every single form in the arrayList to check if the forms are disposed through the isDisposed property?
int formDisposed = 0;
for (int i = 0; i < notificationList.Count; i++)
{
if(notificationList.?????.IsDisposed == true)
{
formDisposed ++;
}
}
Thanks!
Usually by using
if(notificationList[i].IsDisposed)
if for some reason you don't have this option: add the following line to your .cs file
using System.Data;
and Syste.Data and Syste.Data.DataSetExtensions so you will able to access xxx.ElementAt(i) method.
You can also use foreach:
int formDisposed = 0;
foreach (Form f in notificationList)
{
if(f.IsDisposed)
formDisposed ++;
}
ArrayLists are indexable with square brackets (notificationList[i]), but this isn't going to work without a cast to your type. ArrayList returns the type Object and Object does not implement IsDisposed. You may want to consider using a Generic List of Controls (List<Control>) instead, which would work the same but not need a cast.
Related
I'm programming an objloader and this is a small part of its code.I want to be able to loop through different vectors in a single for loop.The for loop doesn't work here but is it possible to implement this concept somehow? make the (for ((GLdouble* val : container)&&(GLdouble* val2:NContainer)) condition work somehow?
aclass e;
std::vector<GLdouble*> container = e.function();
Nclass n;
std::vector<GLdouble*> Ncontainer = n.function();
for ((GLdouble* val : container)&&(GLdouble* val2:NContainer))
{
glVertex3dv(val);
glNormal3dv(val2);
}
My code structure is as follows->
MyApp-> app -> model, view, controller, store, utils
I have the following utility class created for my validation errors:
Ext.define('MyApp.shared.utils.GlobalStrings', {
AmountLimitViolation: "Amount Limit Violated",
DurationLimitViolation: "Duration Limit Violated"
});
I am trying to get the strings dynamically in the following way, but they keep coming up undefined:
Ext.define('MyApp.controller.ViewController', {
extend: 'Ext.grid.Panel',
violationCheck: function (tradelimit, tradelimitViolations){
let me = this,
tradeViolationMessage = "";
if(tradelimit > 0){
for(i = 0; i < tradelimitViolations.length; i++){
violationString = tradelimitViolations[i] + "Violation";
tradeViolationMessage += GlobalStrings.violationString;
}
}
}
});
tradelimitViolations is a string that would contain something like: ("AmountLimit", "DurationLimit")
So is there any method I could use to get AmountLimitViolation and DurationLimitViolation dynamically without having to resort to using a long list of conditional statements? Because I'm just showing two of the many violation errors I have.
The problem is how you are "indexing" into GlobalStrings. The dot operator uses the text to the right as the key, but you have a variable holding your key.
Try this:
for(i = 0; i < tradelimitViolations.length; i++){
violationString = tradelimitViolations[i] + "Violation";
tradeViolationMessage += GlobalStrings[violationString];
}
I have many class in my library almost 300, and I want to generate instance name by loop. In other words I wanna instead of this approach(witch has many code line):
X:This is my problem:
var cm1: Cm1 = new Cm1();
var cm2: Cm2 = new Cm2();
var cm3: Cm3 = new Cm3();
...
use like this approach (less than 10 code lines):
Y:I think this is solution:
for (var i: uint = 1; i < 4; i++)
{
var getChildByName("cm" + i): getChildByName("Cm" + i) = new getChildByName("Cm" + i);
}
but I know above code does not work, is there any way to make them !
-What am I actually trying to solve?
Make many variable by a few line code and save time and size app!
-Why do I have ~300 classes and why are you trying to create them in a loop at once?
This is about to data of request application!
-What do these classes do that you unconditionally need one of each all at the same time?
Because those data would show at first time!
First, it is better to store the classes in an Array or Object rather than an external variable for each item. if you want to access them by name, better to use object:
var classList:Object=new Object();
Then in your loop:
for(var i:uint=1;i<NumberOfClasses;i++){
classList["cm"+i.toString()]=new (getDefinitionByName("Cm"+i.toString()) as Class)();
}
getDefinitionByName is used to make Constructors using String;
Note: if your classes contain a package directory, you should include it. for example:
getDefinitionByName("myclasses.cm.Cm123")();
Then you can access them using Bracket syntax:
classList["cm123"].prop=val;
And don't forget to:
import flash.utils.getDefinitionByName;
I Hope it will help.
EDIT
to use Array instead of object, the loop should be:
for(var i:uint=1;i<NumberOfClasses;i++){
classList[i]=new (getDefinitionByName("Cm"+i.toString()) as Class)();
}
then to access them:
addChild(classList[0]);//0 or any other index;
I'm using the following array to add children to the stage:
for(var i=0;i<6;i++) {
var aCherry=new cCherry()
aCherry.y=10
aCherry.x=10+100*i
stage.addChild(aCherry)
}
Now I want to modify each cherry based on another array. Something like this:
var cherryLetter:Array=[1,0,0,0,0,0]
for(i=0;i<6;++) {
if(cherryLetter[i]) stage.getChildByName("aCherry")[i].y+=90
}
Clearly stage.getChildByName("aCherry")[i] isn't correct, but coming from JavaScript this makes the most sense to me and should accurately portray what I'm trying to achieve for you guys reading this. So, how would I actually do this? This being getting an array of children added to the stage under a certain name or class (so an array of cCherry would work too, if necessary), then using them in a way similar to the above loop.
Here is my recommendation for how the code might look, based on the desire to use getChildByName() to find the instances of your cCherry class. Please note that I've changed the class name to Cherry in the example (which I recommend, since capitalizing class names is AS3 convention). Also, it's good practice to end statements with semi-colons. While it's usually optional, there are cases where omitting the semi-colon can produce very difficult to track down runtime bugs, so I recommend getting int he habit of using them. I also recommend including type in all your variable declarations, as shown with var aCherry:Cherry, for example.
var i:int;
for(i=0; i<6; ++i)
{
var aCherry:Cherry=new Cherry(); // Note, it's my recommendation that you rename cCherry class to Cherry (convention)
aCherry.y=10;
aCherry.x=10+100*i;
aCherry.name = "aCherry" + String(i); // String() cast for clarity only, not necessary
stage.addChild(aCherry);
}
and
var cherryLetter:Array=[1,0,0,0,0,0];
for(i=0; i<6; ++i)
{
var cherry:Cherry = stage.getChildByName("aCherry" + String(i)) as Cherry;
if(cherry && cherryLetter[i]) cherry.y += 90;
}
I have an array of strings which I am trying to store in Isolated storage, However I need to store each string in the array in a new file of its own.
Any approach is welcomed.
Thanks.
I do something similar in an app with code roughly along these lines. Though I am serializing objects in an array to json. Same rough idea though.
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication()) {
for (int i = 0; i < array.Length; i++) {
string fileName = "file" + i.ToString() + ".dat";
using (var stream = file.CreateFile(filename)) {
using (var writer = new StreamWriter(stream)) {
writer.Write(array[i]);
}
}
}
}
Note this is just typed straight in, I may have a mistake in there :)
Your question is a little vauge, but here I go.
What is stopping you from just serializing each string to a file with the index as the name? For example, store stringarray[0] in a file 0.xml.
Just check whether the file exists before trying to read it.