Copying a value in an array to a variable - variables

I have an array of type int. It has one place and I want to copy that to a variable of type int.
Ex:
Random randomNum = new Random();
int myNumber = randomNum.Next(1,1000);
int[] puterNumber = new int[1];
puterNumber[ 1 ] = myNumber;
Later in my code..
int myPuterNumber;
myPuterNumber = puterNumber[ 1 ];
I get a message on mouseover saying 'Cannot resolve symbol 'puterNumber' '
Am I missing a step for copying the value of the array to an int variable?

This looks like a scope error, probably due to object structure if you are using Java. Honestly more code would help make it clear where exactly the problem was originating from.

Related

Is it possible to init a variable in the while condition body for Kotlin?

In the code below:
var verticesCount: Int // to read a vertices count for graph
// Reading until we get a valid vertices count.
while (!Assertions.checkEnoughVertices(
verticesCount = consoleReader.readInt(null, Localization.getLocStr("type_int_vertices_count"))))
// The case when we don't have enough vertices.
println(String.format(Localization.getLocStr("no_enough_vertices_in_graph"),
Assertions.CONFIG_MIN_VERTICES_COUNT))
val resultGraph = Graph(verticesCount)
we are getting next error on the last line:
Error:(31, 33) Kotlin: Variable 'verticesCount' must be initialized
Assertions.checkEnoughVertices accepts a safe type variable as an argument (verticesCount: Int), so it's impossible for verticesCount to be uninitialized or null here (and we're getting no corresponding errors on those lines).
What's going on on the last line when already initialized variable becomes uninitialized again?
The syntax you've used denotes a function call with named arguments, not the assignment of a local variable. So verticesCount = is just an explanation to the reader that the value which is being passed here to checkEnoughVertices corresponds to the parameter of that function named verticesCount. It has nothing to do with the local variable named verticesCount declared just above, so the compiler thinks you've still to initialize that variable.
In Kotlin, the assignment to a variable (a = b) is not an expression, so it cannot be used as a value in other expressions. You have to split the assignment and the while-loop condition to achieve what you want. I'd do this with an infinite loop + a condition inside:
var verticesCount: Int
while (true) {
verticesCount = consoleReader.readInt(...)
if (Assertions.checkEnoughVertices(verticesCount)) break
...
}
val resultGraph = Graph(verticesCount)
Well, technically it is possible to assign values to variables in the while condition - and anything else you might want to do there, too.
The magic comes from the also function:
Try this: (excuse the completely useless thing this is doing...)
var i = 10
var doubleI: Int
while ((i * 2).also { doubleI = it } > 0) {
i--
println(doubleI)
}
Any expression can be "extended" with "something to do" by calling also which takes the expression it is called upon as the it parameter and executes the given block. The value also returns is identical to its caller value.
Here's a very good article to explain this and much more: https://medium.com/#elye.project/mastering-kotlin-standard-functions-run-with-let-also-and-apply-9cd334b0ef84

Objective-C: Adding to variable before assignment

I have the following code:
NSInteger variableScene = 10;
NSInteger numberOfRowsXX = //an Integer value; <-- I would like replace XX with the value of 'variableScene'
So it'll look like this:
NSInteger numberOfRows10 == //an Integer value;
How can I replace XX with the value of variableScene?
*Update*
Why I'm trying accomplish this?
It's a bit complicated. I'm using Core Data with remote Database. In a nutshell: I have 10 Scenes that'll be presented based on the User selection order. For each Scene I need to assign the numberOfRows to present at the end of all the Scenes with a UITableViewController.
I have a variableScene that I pass around from Scene to Scene.
I need to assign numberOfRowsSceneXX = //an integer
XX will come from variableScene, which could be any number from 1 - 10.
So when we get to the last scene, we'll have a value for each Section (numberOfRowInSection) which will be represented by each Scene: numberOfRowScene1, numberOfRowsScene2, etc.
I tried to simplify the question. Hope it makes sense.
Use arrays.
set it up as:
#define MAX_SCENES 10
NSInteger numberOfRows[MAX_SCENES];
then when you want to set the value:
if (variableScene < MAX_SCENES) {
numberOfRows[variableScene] = variable;
}

Objective-C: pointer arithmetic

I need to fill a dae_prim *array_prim; where dae_prim is a class I created.
I want to use C style because I will pass all those data to OpenGL.
When I'm trying to make a : mesh->array_prim[i] = mysexyprim it fails with a "subscript requires size of interface".
I think I understand the problem (Obj-C wants me to use a NSArray) but how can I bypass this?
More code
class meshes:
#public:
dae_prim *prims;
int primcount;
.
model->meshes->prims = malloc(sizeof(dae_prims*) * model->meshes->primcount);
dae_prim *prims = [[dae_prim alloc]init];
model->meshes->prims[1] = prims; //here is the problem
You need to use a double pointer for meshes->prims, as you want an array of pointers.
class meshes:
#public:
dae_prim **prims; /* a C array of objects pointers */
int primcount;
model->meshes->prims = malloc(sizeof(dae_prims*) * model->meshes->primcount);
model->meshes->prims[1] = [[dae_prim alloc]init];
Cheers.

Question about this line of code in AppSettingsReader

I was reading this example: http://blog.ondrejsv.com/post/AppSettingsReader-and-reading-typed-and-nullable-application-settings.aspx and I noticed in this line of code:
int failCount = (int) appSettingsReader.GetValue("FailAttemptCount", typeof(int));
I don't understand why there is a (int) before appSettingsReader, what it does, etc.
The call to GetValue is designed to return the result as various possible types, but the declared return value is Object. So you tell it to return an int, but then you have to cast the result to int so you can reference it as such.
it casts the returned object to an int, this makes sure that the object you get back can be assigend to the int variable failCount. This is needed since the return type of GetValue is Object, not int

COM: Create a VT_ARRAY with VT_BSTR values

I'm a COM newbie and I think what I have is correct, but the runtime doesn't like it. Any help is much appreciated.
I need to invoke a COM function that takes in a single dimensional array of BSTRs. Specifically, the documentation says the parameter must be:
Function: AddFiles ( [in] VARIANT * filePaths )
filePaths The single-dimensioned array of full paths to each file or
folder. filePaths can be of type
VT_ARRAY|VT_VARIANT, where each entry
is a VT_BSTR, or VT_ARRAY|VT_BSTR.
I have a vector<wstring> myPaths of paths which I want to pass into the function that takes the parameter above. Here's the code I wrote. Calling AddFiles on myComObject results in an AV (myComObject is not null, and I can invoke other methods on it):
...
VARIANT filePaths;
VariantInit( &filePaths );
filePaths.vt = VT_ARRAY|VT_VARIANT;
filePaths.parray = SafeArrayCreateVector( VT_BSTR, 0, (unsigned int) myPaths.size() );
long i = 0;
for( vector<wstring>::iterator it = myPaths.begin();
it != myPaths.end();
it++, i++ )
{
BSTR myPath= SysAllocString(it->c_str());
SafeArrayPutElement( filePaths.parray, &i, myPath);
}
myComObject->AddFiles( &filePaths );
...
The COM object isn't my code and I can't debug into it, but I suspect I'm not creating that array properly - based on the requirement of the AddFiles function and the code I have, anyone have ideas about what I might be doing wrong?
If myComObject->AddFiles can only deal with VT_ARRAY|VT_VARIANT, the following should work too.
VARIANT myPath;
VariantInit(&myPath);
myPath.vt = VT_BSTR;
myPath.bstrVal = SysAllocString(it->c_str());
SafeArrayPutElement(filePaths.parray, &i, &myPath);
Don't you want:
filePaths.vt = VT_ARRAY|VT_BSTR;
Since you're creating a SafeArray of BSTRs?