I am trying to write a nested for loop in visual basic macro which runs on excel.
Here is my simplified code
Dim intVar(2) As Integer
For intVar(1) = 0 To 4
For intVar(2) = intVar(1) To 4
Var = Var + intVar(1) + intVar(2)
Next intVar(2)
Next intVar(1)
When I try to compile the code "For control variable already in use" compile error is thrown. Is there any solution for using array variables for For Loop or should I declare different variables for each For Loop?
There are some questions with the same tag but none of them for the array type control variables. If you help me I would be glad.
Thank you for your interest
Related
I have an array, and it used to work just fine in VBA. I am now using SeleniumBasic and am trying to find the equivalent of the command ".length"
Dim i As Object
Dim elmCollection As Object
Set elmCollection = WDrv.FindElementsByTag("table")
For i = 0 To (elmCollection.Length)
It bombs out on the "For" statement. So, is there an equivalent to the ".length" in SeleniumBasic?
In visual basic it's ubound(array) to get the upper boundary of an array.
You'll want to use:
For i = 0 To ubound(elmCollection)
Selenium basic WebElements collections IIRC start at 1 and the appropriate property is .Count. You are not working with an array.
Therefore your loop should in fact be a For Each, e.g.
Dim i As Object: For Each i In elmCollection
to enumerate through the WebElements collection; if using the slower method of enumeration with objects then you want For i = 1 to elmCollection.Count
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 5 years ago.
Hello I have a code and blah blah blah...
The problem is I have a maskedtextbox(s) in a row (12 of them to be exact) and i want to give them a specific value from array. Is it possible to do it by loop?
I tried this:
This is the declaration:
Dim BiA11() As MaskedTextBox = New MaskedTextBox() {BiA11_1, BiA11_2,
BiA11_3, BiA11_4, BiA11_5, BiA11_6, BiA11_7, BiA11_8, BiA11_9, BiA11_10,
BiA11_11, BiA11_12}
And this is the code later in the program:
For index As Integer = 0 To 11
BiA11(index).Text = ""
Next
Yes I know now that i am pusshing nothing to the MaskedTextBox but this was only for my test. But the visual studio gives me this error:
System.NullReferenceException: Object reference not set to an instance of an object.
BiA11() – Nothing.
In the end I want to push to the textboxes strings from an array throgh loop (it will save a quantum of space because I have 132 of these textboxes.
Thank you for your time.
********************EDIT********************
So I found an answer
First of all in the declaration i changed the array a little bit:
Dim BiA11() As MaskedTextBox = New MaskedTextBox(11) {}
The declaration takes a place in the Public Class Form1
Then in the sub that handels the first events of appication in the first lines I filled the array with the references of the MaskedTextBox('s)
BiA11 = {BiA11_1, BiA11_2, BiA11_3, BiA11_4, BiA11_5, BiA11_6, BiA11_7, BiA11_8, BiA11_9, BiA11_10, BiA11_11, BiA11_12}
And then finaly when it comes to deleting all of the MaskedTextBox('s) i just puted this for loop inside and it worked for me.
For index As Integer = 0 To 11
BiA11(index).Text = ""
Next
No exceptions and the MaskedTextBox('s) are clear. Thank you for all your help. Hope that this will come in handy when someone will have similiar problem.
The issue is presumably that you have got that first code snippet at the class level. In that case, it will be executed before the constructor, in which case your controls haven't been created yet. What you need to do is declare the array variable at the class level:
Private BiA11 As MaskedTextBox()
and then create and populate the array inside the Load event handler of the form:
BiA11 = {BiA11_1, BiA11_2, BiA11_3, BiA11_4, BiA11_5, BiA11_6, BiA11_7, BiA11_8, BiA11_9, BiA11_10, BiA11_11, BiA11_12}
I am using the following code to filter a large array:
var arrayOfSelectedRowDetails = self.projectRowDetails.filter(
{ $0.projectNumber == self.projectNumberArray[selectedRow] }
)
Normally the code runs fine and I have no issues. But in one scenario (after I have deleted some management objects from the persistent store) and then rerun the code I am getting a EXC_BAD_ACCESS (code = 1, address=0x0) error at runtime.
I have set a break and stepped through the runtime of this statement. It is a large array built from a core data entity (using a fetch statement) - and therefore takes a long time. When I step through the code over the first dozen or so indexes the code runs ok - when i remove the break and let it run it then presents the error.
Is it possible to println() from within the closure statement to assist with debugging? I have tried a number of different syntaxes and cannot get it to work.
Alternatively, is it possible to set an error capture statement within the closure so that the code ceases through a break or an abort() statement?
Fundamentally i am trying to identify the index of the array at the point that the error occurs so that I can get sufficient information to debug the delete function (which is where I think the error is). I do not seem to be able to ascertain the index from the info available to me when the error occurs.
This is the first time I have tried programming in Swift and making use of closures so I am learning as I go. Apologies if I am asking fundamental questions. I have not been able to find a similar question elsewhere here with an answer that works.
You can set an exception breakpoint in Xcode (for an example see here).
Also, I suggest that you move the access to self.projectNumberArray out of the closure:
let pn = self.projectNumberArray[selectedRow]
var arrayOfSelectedRowDetails = self.projectRowDetails.filter(
{ $0.projectNumber == pn }
)
The change might not solve the issue, but it will at least help the debugging.
Lastly, if you want to print the index, the following approach will probably work:
let pn = self.projectNumberArray[selectedRow]
var index = 0
var arrayOfSelectedRowDetails = self.projectRowDetails.filter(
{ println(index++); return $0.projectNumber == pn }
)
I have a SWIG generated R wrapper which contains the following setClass operations:
setClass('_p_f_p_struct_parameters_p_struct_chromosome_p_struct_dataSet__double',
prototype = list(parameterTypes = c('_p_parameters', '_p_chromosome', '_p_dataSet'),
returnType = '_p_f_p_struct_parameters_p_struct_chromosome_p_struct_dataSet__double'),
contains = 'CRoutinePointer')
setClass('_p_f_p_struct_parameters_p_p_struct_chromosome_p_p_struct_chromosome_int_int__void',
prototype = list(parameterTypes = c('_p_parameters', '_p_p_chromosome', '_p_p_chromosome', '_int', '_int'),
returnType = '_p_f_p_struct_parameters_p_p_struct_chromosome_p_p_struct_chromosome_int_int__void'),
contains = 'CRoutinePointer')
These operation do not appear to be behaving as expected. When I call a function with the output being the creation of a _p_parameters object (defined above), I get the following error:
Error in getClass(Class, where = topenv(parent.frame())) :
“_p_parameters” is not a defined class
The setClass therefore seems to be not doing it's thing.
I tried to manually set the _p_parameters class via:
p_parameters<-setClass(Class="_p_parameters", representation = representation(ref = "externalptr"))
But this does not seem to work as when I try and modify other parameters (or even print parameters via an inbuilt function) the terminal crashes and hangs.
For reference, the final lines in initialiseParameters (the function which initially own _p_parameters) are calling the native C function via .Call then assigning the external pointer to a new object of class _p_paramters as follows:
;ans = .Call('R_swig_initialiseParameters', numInputs, numNodes, numOutputs, arity, as.logical(.copy), PACKAGE='cgp');
ans <- new("_p_parameters", ref=ans) ;
I've read various R doc on new(), setClass, S3/S4 classes but nothing seems to clarify what I'm meant to be doing here.
Any suggestions on where to start or tutorials that would give a good heads up would be most welcome.
Please keep in mind the C code is not mine (but is freely available under GNU), I am not a C programmer and am only weakly-moderately proficient in R. So please be gentle :)
Cheers.
PS: If I call the function in R terminal via .Call it works as expected (so it doesn't seem to e a C function error)
I thought I should post the solution I have to this. The least I could do for a 'tumbleweed' medal haha.
The only solution I could find to this is to comment out the following line:
ans <- new("_p_parameters", ref=ans) ;
in all function that try to create an R object.
The resulting memory pointer is then assigned to an R object at function call.
It's dirty, but I couldn't work out how to create an object from a memory pointer (at least from within the functions themselves).
It seems to work so I guess it will do.
I have created an array of objects and I would like assign a property value in a vector operation without using a for loop. Unfortunately I get an error.
A simplified example of the problem.
classdef clsMyClass < handle
properties
dblMyProperty1
end
methods
function obj = clsMyClass()
end
end
end
And when running
vecMyArray = clsMyClass.empty(100,0);
vecMyArray(100) = clsMyClass;
vecMyArray.dblMyProperty1 = 1:100;
We get the following error:
??? Incorrect number of right hand side elements in dot name
assignment. Missing [] around left hand side is a likely cause.
Any help would be appreciated.
I see what you're trying to do now. Use disperse from the MATLAB File Exchange:
>> [vecMyArray.dblMyProperty1] = disperse(1:100);
>> vecMyArray(1).dblMyProperty1
ans =
1
>> vecMyArray(10).dblMyProperty1
ans =
10
You can use the deal function for exactly this purpose:
[vecMyArray.dblMyProperty1] = deal(1:100);
See: http://www.mathworks.com/company/newsletters/articles/whats-the-big-deal.html
Edit: No you can't, actually; that'll set them to all be the vector 1:100.
I think you'll find your answer here in "Struct array errors." Even though this is a class, similar rules apply.
Unfortunately missing [] is not the cause, since adding them causes more errors. The cause is that you cannot assign the same value to all fields of the same name at once, you must do it one at a time, as in the following code:
So you'll need:
for ii=1:100
vecMyArray(ii).dblMyProperty1 = ii;
end
I know it's not satisfying, but I think it at least helps us to definitively understand this error.