How can I find the end position of enumerator? - c++-cli

How can I find the end position of enumerator? For this enumerator I didn't see any atEnd method in the documentation.
SortedSet<Button ^>::Enumerator^ enumer = btnSet->GetEnumerator();
enumer->atEnd();
my error
error 'atEnd' : is not a member of 'System::Collections::Generic::SortedSet<T>::Enumerator'

Is your intention to write a loop like while (enumer != btnSet->end()), as is common with many C++ iterators?
You can check if an enumerator is not yet at the end, by checking the return value of MoveNext(), so the loop would be this:
SortedSet<Button^>::Enumerator^ enumer = btnSet->GetEnumerator();
while (enumer->MoveNext()) {
Button^ btn = enumer->Current;
// do stuff
}
You could also iterate over the collection using a for each loop, which looks a bit cleaner:
for each (Button^ btn in btnSet) {
// do stuff
}

Related

How to not add to the list, when the return of a LINQ IEnumerable is empty?

I do have the following VB.NET code:
Dim list = directoryQuery.Select(
Function(d) New With {
.dir = d.FullName,
.acl = GetFileSystemAccessRule(d).Select(
Function(a) New With {.if = a.Reference.ToString()}
)
}
)
End Sub
Sometimes the return of GetFileSystemAccessRule(d).Select is Return Enumerable.Empty(Of FileSystemAccessRule)(). In that case, I would like to neither add .directory nor .acl to that list. I want to skip it.
So I tought about the options to remove afterwards the empty items.
//tried but failed:
list = list.Where(Function(a) a.acl IsNot Enumerable.Empty(list)).ToList()
//tried but failed:
list = list.Where(Function(a) a.acl IsNot Nothing).ToList()
But unfortunately all of them failed. What do I wrong?
I think this would be the way to go:
list = list.Where(Function(a) a.acl.Any())
or something closely resembling that (I'm not very well versed in VB.NET syntax).

dynamically change a part of the variable path

I know this question has been asked a bunch of times, but none of the answers (or at least what i took away from them) was a help to my particiular problem.
I want to dynamically change a part of the variable path, so i don't have to repeat the same code x-times with just two characters changing.
Here's what i got:
In the beginning of my script, i'm setting the reference to PlayerData scripts, attached to the GameManager object like this:
var P1 : P1_Data;
var P2 : P2_Data;
function Start(){
P1 = GameObject.Find("GameManager").GetComponent.<P1_Data>();
P2 = GameObject.Find("GameManager").GetComponent.<P2_Data>();
}
Later, i want to access these scripts using the currentPlayer variable to dynamically adjust the path:
var currentPlayer : String = "P1"; //this actually happens along with some other stuff in the SwitchPlayers function, i just put it here for better understanding
if (currentPlayer.PlayerEnergy >= value){
// do stuff
}
As i was afraid, i got an error saying, that PlayerEnergy was not a part of UnityEngine.String.
So how do I get unity to read "currentPlayer" as part of the variable path?
Maybe some parse function I haven't found?
Or am I going down an entirely wrong road here?
Cheers
PS: I also tried putting the P1 and P2 variables into an array and access them like this:
if (PlayerData[CurrentPlayerInt].PlayerEnergy >= value){
// do stuff
}
to no success.
First of all,
var currentPlayer : String = "P1"
here P1 is just string, not the previous P1/P2 which are referenced to two scripts. So, if you want, you can change
currentPlayer.PlayerEnergy >= value
to
P1.PlayerEnergy >= value
or,
P2.PlayerEnergy >= value
But if you just want one function for them, like
currentPlayer.PlayerEnergy >= value
Then you have to first set currentPlayer to P1/P2 which I assume you are trying to do. You must have some codes that can verify which player is selected. Then, maybe this can help -
var playerSelected: int = 0;
var currentPlayerEnergy: int = 0;
.....
//Use your codes to verify which player is selected and then,
if (playerSelected == 1) {
currentPlayerEnergy = P1.PlayerEnergy;
} else if (playerSelected == 2) {
currentPlayerEnergy = P2.PlayerEnergy;
}
//Now use your favorite function
if (currentPlayerEnergy >= value) {
//Do stuff
}
As there was no reply providing the answer I needed, I'll share the solution that did the trick for me, provided by a fellow student.
Instead of having the PlayerData scripts pre-written, I generate them using a public class function in a Playermanager script. This generates the Playerdata as attached scripts, saved into an array.
I can then access them through Playermanager.Playerlist[Playernumber].targetvariable.
Which is what I wanted to do, only with the Playerdata being attached to a script instead of a gameobject. And it works great!
Here's the full code of my Playermanager Script:
//initialise max players
public var maxplayers : int = 2;
// Initialise Playerlist
static var Players = new List.<PlayerData>();
function Start () {
for (var i : int = 0; i < maxplayers; i++){
var Player = new PlayerData();
Players.Add(Player);
Players[i].PlayerName = "Player " + i;
}
DontDestroyOnLoad (transform.gameObject);
}
public class PlayerData {
public var PlayerName : String;
public var PlayerEnergy : int = 15;
public var Fleet : List.<GameObject> = new List.<GameObject>();
}
As you see, you can put any type of variable in this class.
I hope this helps some of you who have the same problem.
cheers,
Tux

Can't access colum value in QSqlQueryModel

i have some problem with changing value in QTableView cell.
ui->search_results->show();
int num = model->rowCount();
for (int i = 0; i<num; i++) {
QSqlRecord rec = model->record(i);
QString file_chron = rec.value(1).toString();
QString file_age = get_file_agestring_from_chron(file_chron);
//model->setData(model->index(i,1), QVariant(file_age), Qt::EditRole);
rec.setValue(1,file_age);
it seems that both functions work as they should in a loop
qDebug()<<rec.value(1).toString()+" "+rec.value(2).toString()+" "+rec.value(3).toString()+" "+rec.value(4).toString()+" "+rec.value(5).toString();
}
but outside we have old value.
qDebug()<<model->data(model->index(3,1)).toString();
ui->search_results->setModel(model);
ui->search_results->hideColumn(4);
i need to reimplement value inside this field.
I think that it is access error, but i can't understand how to set new value!
sorry, i can't add tag QSqlQueryModel
You are operation on QSqlRecord rec; which is copy of the record instead of the reference to it. As for setting data - see model : setData function

Implement the same function in AS2 for an Array

I have an array, and I would like to make a function onRelease for all of the array positions.
The code would be like:
pick = new Array(2,3,4);
var botoes1:MovieClip = lev.attachMovie("block", "block_"+lev.getNextHighestDepth(), lev.getNextHighestDepth(), {_x:550, _y:1*22});
_root.botoes1.gotoAndStop(pick[1]);
var botoes2:MovieClip = lev.attachMovie("block", "block_"+lev.getNextHighestDepth(), lev.getNextHighestDepth(), {_x:550, _y:2*22});
_root.botoes2.gotoAndStop(pick[2]);
var botoes3:MovieClip = lev.attachMovie("block", "block_"+lev.getNextHighestDepth(), lev.getNextHighestDepth(), {_x:550, _y:3*22});
_root.botoes3.gotoAndStop(pick[3]);
for(i=0;i<3;i++){
_root['botoes'+i].onRelease() = Function () {
}
}
but it doesn't work this way...
and if possible, how can I make the MovieClip declaring for all the buttons in an for loop?
Couple syntax errors there, here's what this line should look like:
_root['botoes' + i].onRelease = function()
{
// Function body.
//
}
Your previous code was trying to assign the result of _root['botoes' + i].onRelease() (which would have been undefined) to the result of Function() (which would have been a Function object).

Determing if a SQL select returns an empty set asynchronously?

Is this possible easily? It seems the handleResult method is only executed if the result isn't the empty set.
A thought I had was to have handleResult and handleCompletion be member functions of an object and have handleResult update a member variable that handleCompletion can check. If the variable is set, not empty, if variable unset, empty and can act accordingly.
seems to be overly complicated and hoping there's a better solution?
to sketch out a solution (the thought i had above) (edit2: per comment I made below)
function sql() {
this.results = false;
var me = this;
this.handleResult = function(aResultSet) {
for (var row = aResultSet.getNextRow(); row; row = aResultSet.getNextRow()) {
me.results = true;
var value = row.getResultByName("name");
}
};
this.handleError = function(aError) {
.... //deal with error
};
this.handleCompletion = function(aReason) {
if (me.results) {
....//results
} else {
....//no results
}
if (aReason != Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED) {
....//handle these
};
};
s = new sql();
statement.executeAsync({
handleResult: s.handleResult,
handleError: s.handleError,
handleCompletion: s.handleCompletion
});
is this considered a good way to solve this problem?
edit1: this doesn't behave in the manner I'd expect (it works, but not 100% sure why). i.e. the this.results variable is undefined (not false), if handleResult never runs. So it appers as if handleResult and handleCompletion are operating on a different set of variables than I'd expect.
any help to understand what I'm doing wrong would be appreciated.