How to correctly reset an incremented counter? - winforms

In one of my scripts I'm using a counter which I gradually increment if a specific event happens. I use the following syntax to do so:
if ($IndexPositionTrue) {
$CorrectTextbox.Text = $script:countercorrect++
}
Right now I'm trying to implement a button with which you can reset the $countercorrect value back to 0. For this I setup a button with a click_handler:
$reset = New-Object System.Windows.Forms.Button
$reset.Add_Click($handler_reset_click)
$form1.Controls.Add($Reset)
$handler_reset_click = {
Reset
}
function Reset {
Clear-Variable countercorrect
}
Let's say the $countercorrect value has been incremented to 5 and I use the Reset function. The value does get reset to 0 but after the next increment instead of displaying 1 it displays 6.
Is my syntax incorrect or am I "resetting" the wrong value?

I think this is a scope issue ,can you modify the reset function like this:
function Reset {
$script:countercorrect = 0
}

You refer $Script:countercorrect in your If-Statement but refer to $countercorrect in your function reset. If think you are using the wrong scope here.
Use the function
function Reset {
Clear-Variable countercorrect -Scope Script
}

Related

Removing a hyphen to the last number of a counter app but also not repeatedly duplicating the same value

The problem
The counter app that I build can save each number in each entry. And each number is separated using the hyphen(-), but the problem is how can I remove the hyphen in the last number or entry but also not repeatedly duplicate the same number whenever I hit the save button twice.
What I Try
Although, I managed to get the trick by asking for a solution on chatGPT. But, it turns out it duplicates the same number whenever I clicked the save button.
Here's the code:
const save = () => {
// count !== 0 && setCounter(counter + count + "-"); previous code that adds the hyphen
if (count !== 0) {
const updatedCounter = counter.endsWith('-')
? counter + count
: counter + count + "-";
setCounter(updatedCounter);
}
};
I think the duplication of the same value occurs on the else statement of : counter + count + "-";
Because I tried to re-arrange the variables and the hyphen by this : counter + "-" + count; and it didn't duplicate the same number twice after clicking the save button. But, the problem is it's not the intended output. Where the intended output would be 1-1-2-3-4 and not 11-2-3-4 or -1-1-2-3-4
Also, whenever I clicked the reset button, it returns an error of Uncaught TypeError: counter.endsWith is not a function
const reset = () => {
setCount(0);
setCounter(0);
setAddAll([]);
setAddOnce(false);
};
You can also view the full project in stackblitz
This is just a simple project with some additional features that I implement to practice my coding skills in React JS. So, comments and suggestions are much appreciated. Thanks!
In your reset function you're setting the counter to a number instead of a string.
Instead use this, this way you'll reset back to your initial value.
setCounter("");
Full reset function
const reset = () => {
setCount(0);
setCounter("");
setAddAll([]);
setAddOnce(false);
};

Is there something in C that is analogous to out/out keyword of C#?

void extract_left_subtree(node *right_child)
{
while(right_child->right)
{
right_child = right_child->right;
}
printf("rightmost inside the funtion is %d\n",right_child->data);
}
in this function the last line is printing the correct value.
node *right_child=root;
extract_left_subtree(right_child);
printf("rightmost child is %d\n",right_child->data);
But here I'm getting some garbage value.
I know what the problem is, I know why it's happening, the only thing I don't know is how to rectify this?
There are keywords like ref and out in C# which can be used to achieve the same but the issue is, how can we do the same in C language?
I don't want to return values from the method please
I don't want to return values from the method please
If you don't want to return a value you can do:
void extract_left_subtree(node **right_child)
{
while((*right_child)->right)
{
(*right_child) = (*right_child)->right;
}
printf("rightmost inside the funtion is %d\n", (*right_child)->data);
}
and call it like:
extract_left_subtree(&right_child);
This passes the address of right_child to the function and then the function can directly update the value of right_child

as3 array of objects - movement with constant speed

Ok, so I have some experience with as3 and some of the basics. But this problem has been stumping me for so long. I tried to do a workaround based on what I currently know about as3. But somehow either i get an error message or it just doesn't do anything at all. Here is the code that i'm trying to solve.
var zombieCount:Array = new Array();
var helltime:Timer = new Timer(1500);
helltime.addEventListener(TimerEvent.TIMER, spawnzombies)
helltime.start();
function spawnzombies(happened:TimerEvent){
var zombie1:Zombie = new Zombie();
zombieCount.push(zombie1);
stage.addChild(zombieCount[zombieCount.length - 1]);
zombie1.x = 135 + (330*Math.random())
zombie1.y = -29
stage.addEventListener(Event.ENTER_FRAME, move_zombie)
function move_zombie(happened:Event){
for(var i:int; i < zombieCount.length; i++){
zombieCount[i].y = zombieCount[i].y + 1;
if(zombieCount[i].hitTestObject(border)){
stage.removeChild(zombieCount[i]);
zombieCount.shift();
trace(zombieCount.length);
}
}
}
}
While this may not be inclusive of everything wrong, here are at least a few of the issues I see.
Inline function issue:
Inside your timer tick handler (spawnZombies), you create an inline function called move_zombie and then add an enter frame handler that calls that function.
The issue here, is that every tick of the timer, will then create a whole new copy of that function, and add ANOTHER enter frame handler. This will create huge problems after a few timer ticks.
Break that move_zombie function OUT OF the spawn function:
eg:
helltime.addEventListener(TimerEvent.TIMER, spawnzombies)
helltime.start();
stage.addEventListener(Event.ENTER_FRAME, move_zombie);
function move_zombie(......
function spawnzombies(.....
Iteration issue:
In your for loop:
for(var i:int; i < zombieCount.length; i++){
zombieCount[i].y = zombieCount[i].y + 1;
if(zombieCount[i].hitTestObject(border)){
stage.removeChild(zombieCount[i]);
zombieCount.shift();
trace(zombieCount.length);
}
}
You are not initializing your i value. While this will default it to 0, it's still a good idea for readability to initialize it.
So your iterating forward from 0 to the end of the array. However, if your hit test succeeds, you then use the shift method of the array. This removes the first item of the array (irrespective of what value i is at the time). This will remove the wrong item, plus mess up what zombieCount[i] refers to (because the amount of items has now changed after doing shift, so the next iteration zombieCount[i] will be a reference to same item as the previous iteration).
Instead of what you're currently doing, use the splice method to remove, and iterate backwards so your index doesn't get out of whack.
for(var i:int=zombieCount.length-1;i >=0;i--){
zombieCount[i].y += 1; //move it down 1 pixel
if(zombieCount[i].hitTestObject(border)){
stage.removeChild(zombieCount[i]);
zombieCount.splice(i,1); //remove the item at the current index (do this instead of shift)
trace(zombieCount.length);
}
}

Listener for every array element

i've created an array. Each element is a button object. Is there a possibility to hook mouseclick on every array at the same time? I mean something like this.
var Objects:Array = new Array
Objects[0] = new button(parameters)
Objects[1] = new button(parameters)
Objects[2] = new button(parameters)
Objects[n].addEventListener(MouseEvent.CLICK, Clicked(n));
function Clicked(n,...)
{
THECODE PROCEEEEDS for Objects[n]
}
I know that's not the clearest and most correct writing, but I'm asking if this is possible in similiar way? And how to do it? I know I can hook every mouseclick and then check if the clicked under the mouse is one of the array elements with for loop, but I'm asking about this way.
Yes. You are unable to directly pass an index into a listener, but you can retrieve that via calling indexOf() inside it.
for each (b in Objects) b.addEventListener(MouseEvent.CLICK, clicked);
// note, you just put function name here!
public function clicked(e:MouseEent):void {
var i:int=Object.indexOf(e.target);
if (i==-1) {
// panic behavior
return;
}
// now you can parse that index into something valuable
}

AS3 adding EventListener to array of movieClips but getting error #2007 :Parameter listener must be non-null

I am trying to add an event listener to ALL of my buttons in the buttons array. I can make them buttons within the loop but when I try and add the event listener is give me this error:
TypeError: Error #2007: Parameter listener must be non-null.
at flash.events::EventDispatcher/addEventListener()
at Main()
I can add this event to another array but just not this one. Ive placed these buttons on the stage and gave them instance names which I'm referring to in my as file. I am learning AS3 in school so this is probably a very obvious problem but Im not qualified to debug my code yet :S Thanks for all your help.
//array of buttons and making them buttons
var buttons:Array = [armButton, lobeButton, beakButton, crotchButton, earButton, hairButton, legButton, shoulderButton, spineButton, tailButton, tearButton, eyeButton];
for(var b:int = 0; b<buttons.length; b++){
buttons[b].buttonMode = true;
buttons[b].addEventListener(MouseEvent.CLICK, clickMe);
}
function clickMe(e:MouseEvent):void{
trace("hello");
}
Check the name of the "clickMe" function both in definition and parameters-section, make sure that exactly the same characters are used (a character can sometimes be mistaken for the character from another code table). The error occurs because "clickMe" expression is null at the moment when the loop is being executed.
var buttons:Array = [armButton, lobeButton, beakButton, crotchButton, earButton, hairButton, legButton, shoulderButton, spineButton, tailButton, tearButton, eyeButton];
for(var b:int = 0; b<buttons.length; b++){
buttons[b].buttonMode = true;
// what is the output of the following expression?
trace(clickMe) // should be "function Function() {}"
buttons[b].addEventListener(MouseEvent.CLICK, clickMe);
}
function clickMe(e:MouseEvent):void{
trace("hello");
}
In your addEventListener line clickMe is null.
I suspect that we aren't seeing all the code here. Is that code together in the same file ? Or did you cut/paste just parts you thought were important ?

Resources