Loop values one by one - xamarin

I am new in Xamarin. I am trying to create my first sample app that will be used loop values one by one. Each button click should print next value. So the first click, should print: 1, second: 2 and so one. How can i do it? Currently i've got only the last value: 9.
for (int i = 0; i < 10; i++)
{
Toast.MakeText(this, i.ToString(), ToastLength.Long).Show();
}

Please try this
Class Yourclass : Activity{
int i = 0;
yourButtonClickEvent(){
i++;
Toast.MakeText(this, i.ToString(), ToastLength.Long).Show();
}}
Update:
I have made a counter variable named i, each time the button is pressed I increase the variable count. So that in your toast you can get the button pressed count and access that required data.

You can maintain a counter variable to achieve this. Make the counter variable global. The counter will keep count for the number of times your button was clicked. Based on the count you can print the next value.

Related

How to forcefully end an iteration of a for-loop (without stopping the for-loop)?

This may be a stupid question, but here goes. Is there a way to forcefully end an iteration of a for-loop and ignore all other conditional statements within the loop, and move onto the next iteration?
I'm trying to make a tool where whenever a user types a letter, it's printed to the canvas.
I'm using a for loop to do this, with each iteration making the next letter move to the right each time.
Inside the for-loop includes the conditional statements for typing the letter:
function draw(){
}
function keyPressed(){
for(i=0; i<100; i++){
if(keyCode == 65){
text("a", 60 + i*10, 60)
}
...
...
...
}
}
and so on, and so forth. However, this would only work if after each letter it moved onto the next iteration (otherwise the letters would be printed in the same place due to 'i' not increasing, making it unreadable).
Using 'return' at the end of each condition statement doesn't work for me as 'return' ends the entire for-loop, whereas I just want to end that specific iteration.
All help is appreciated, thank you.
It sounds like you're looking for the continue keyword. Here's an example from W3Schools:
for (i = 0; i < 10; i++) {
if (i === 3) { continue; }
text += "The number is " + i + "<br>";
}
Here is a linke to try the code yourself.
But taking a step back, this feels like a flawed design. If you want to get the key they user pressed, you don't have to use a bunch of if statements. You can just use the key variable. From the P5.js reference:
function setup() {
fill(245, 123, 158);
textSize(50);
}
function draw() {
background(200);
text(key, 33, 65); // Display last key pressed.
}
Even if that won't work for some reason (if you want to display something for the arrow keys, for example), there are probably better ways to solve this problem than a bunch of if statements in a for loop. For example you could create a mapping from keyCode to the string you want to display, and then call that mapping. Something like:
var m = new Map()
m.set(65, 'a');
m.set(66, 'b');
//..
function keyPressed(){
text(m.get(keyCode), 50, 50);
}

How to add a 2D array several times at the end of an existing 2D array using a for loop?

Starting point (boundary conditions):
There is a set of recurring events each week (weekly events section
row 9:11).
My colleagues can set a starting date A6 and how many weeks they'd like to add A5.
Start situation:
Goal:
Running the script should add A5 number of weeks starting from the date A6 to the "upcoming events" section (row 22 and following) with the correct date.
This is how it would look like after the script ran successfully:
What works so far:
The script is able to add the recurring events for one week in the right order to the upcoming events section. It works as well if the starting date is in the middle of the week. (Not shown here as it is probably not relevant.)
My code:
function recurringWeeks() {
var ss = SpreadsheetApp.getActiveSheet(); // selects the active spreadsheet
var repeatingWeeks = ss.getRange(5,1).getValue(); // gets how many weeks it should repeat
var regWeek = ss.getRange(9, 2, 3, 7).getValues(); // gets the regular week data
var regWeekRepeated = ss.getRange(9, 2, repeatingWeeks*3, 7); // create an array to store the events for all weeks
// fill regWeekRepeated with regWeek
for (var j = 0; j < repeatingWeeks; j++){
for (var i = 0; i < 3; i++){
regWeekRepeated[i+j*3] = regWeek[i];
}
}
// Repeat week for "A5" times and add to start/end date a week
for (var j = 0; j < repeatingWeeks; j++){
for (var i = 0; i < 3; i++){
regWeekRepeated[i+j*3][0] = new Date(regWeek[i][0].getTime() + j*7*3600000*24); // <-This line leads to an error message.
}
}
ss.getRange(ss.getLastRow()+1,2,repeatingWeeks*3,7).setValues(regWeekRepeated); // copies weekly events after the last row
}
Edit of [i+j*6] to [i+j*3] in Repeat week for "A5" times and add to start/end date a week
Approach:
As I have solved how to add one week of recurring events with the correct date and right order, I use this as my "point of attack". I'm pretty sure that a for-loop does the job and this is currently my preferred tool.
create an array (regWeek) filled with the recurring event for one
week with the right order and dates. DONE
create an array (regWeekRepeated) and fill it with A5 number of
regular weeks (regWeek) starting from the date A6. ERROR 1:
Object does not allow properties to be added or changed.
make changes to the filled array regWeekRepeated. ERROR 2: TypeError: Cannot set property "0.0" of undefined to "(class)#3d8e4650"
Copy the values into the "upcoming events" section. DONE
Best hit I've found in the search results:
creating 2 dimension arrays
However, this uses .push and as far as I understand this means an element (can be a row) is placed at the end of an array. I've tried to use push as well but have not been successful yet.
Questions:
ERROR 1: Why is it not possible to assign the value of an element from array regWeek to array regWeekRepeated? Solved
ERROR 2: Is this property issue related to ERROR 1 or something different? I've tried to solve both errors individually. Solved
Which approach makes more sense (logically or performance wise) in this context: push individual rows at the end of an existing array or use the whole week as array building blocks?
A demo version of the spreadsheet
Update V01:
Changes: regWeekRepeated is now an array.
I've changed the for loop due to the feedback I've received.
// fill regWeekRepeated with regWeek
var regWeekRepeated = [];
for (var j = 0; j < repeatingWeeks; j++){
for (var i = 0; i < 3; i++){
regWeekRepeated.push(regWeek[i]);
}
}
Logger.log(regWeekRepeated)
Update V02:
// Repeat week for "A5" times and add to start/end date a week
for (var j = 0; j < repeatingWeeks; j++){
for (var i = 0; i < 3; i++){
regWeekRepeated[i+j*3][0] = new Date(regWeek[i][0].getTime() + j*7*3600000*24); //adds a week to the dates for each cycle
//Logger.log(regWeekRepeated[i]); // log is as expected and desired
}
Logger.log(regWeekRepeated); // second part of log not as expected.
}
//Logger.log(regWeekRepeated);
ss.getRange(ss.getLastRow()+1,2,repeatingWeeks*3,7).setValues(regWeekRepeated); // copies weekly events after the last row
Here the log output placed in the "outer" for loop.
1 represents the first cycle, 2 the second cycle
It looks like the second for loop overwrites the elements 0 to 2.
And here the output in google sheets
Update V03:
This makes sure that the changes don't affect the copy.
// fill regWeekRepeated with regWeek
for (var j = 0; j < repeatingWeeks; j++){
for (var i = 0; i < 3; i++){
regWeekRepeated[i+j*3] = regWeek[i].slice(); // shallow copy of an array
}
}
regWeekRepeated is not a array. getRange() doesn't return a array.
Try changing from
var regWeekRepeated = ss.getRange(9, 2, repeatingWeeks*3, 7); // create an array to store the events for all weeks
To
var regWeekRepeated = ss.getRange(9, 2, repeatingWeeks*3, 7).getValues(); // create an array to store the events for all weeks
Creating a array without touching the spreadsheet will increase performance.
var regWeekRepeated =[];
Making scripts for spreadsheets could be tricked because the spreadsheet and JavaScript jargons/lexics use the same terms in different ways. Perhaps this is what is happening here.
ERROR 1: Why is it not possible to assign the value of an element from array regWeek to array regWeekRepeated?
regWeekRepeated is a Range object not a JavaScript Array
ERROR 2: Is this property issue related to ERROR 1 or something different? I've tried to solve both errors individually.
Yes it's related. See the previous answer.
Which approach makes more sense (logically or performance wise) in this context: push individual rows at the end of an existing array or use the whole week as array building blocks?
We could say that calling Google Apps Scripts classes and methods are "expensive" so we should try to minimize the number calls to these kind of elements. One way to do this is by passing the range values to a JavaScript Array , then make all the changes directly to it and we finish pass the resulting values to the corresponding range.
To pass the values of a range to a JavaScript 2D array, use range.getValues() and to pass the JavaScript 2D array values to a range use range.setValues().

Better way to show iAd interstitial ads

Is there a better way to show interstitial ads than just picking a random number between 0-4 and if its 3 show the ad?
This method has its downsides because sometimes ads wont show for awhile and other times they will show 3 times in a row which is very annoying. Any help would be appreciated!
Why generate a random number at all? There is no requirement for you to implement your interstitials in this manner. How often do you want to show your ads? You can just increment an index and show your interstitial once it reaches a certain amount. For example:
// Create an Int to increment
var index = 0
// Call this func after an event
func showInterstitial() {
index++
if (index == 5) {
// Show ad
// Reset index
index = 0
}
}

How To Get A Random Number To Count Down

I am trying to get a random number to count down to zero, but what i have keeps giving me a random number. Then every second it will generate a new random number between 4 and 11, and won't count down. if anyone can help it would be greatly appreciated. thanks in advance!
enter code here
private void timer1_Tick(object sender, EventArgs e)
{
Random nuber = new Random();
i = nuber.Next(4, 11);
i--;
textBox1.Text = i.ToString();
if(i == 0)
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();
f2.Show();
f1.Close();
}
}
If I understand what you are trying to do, then you need to generate the number outside of the loop, in an initialization phase, in a static (global) variable. Then the timer tick will count it down. Note your f1 is created and then closed, it never shows, so is superfluous. Also you are creating a new form every time which is maybe not what you want.
Basically you need to move just the two statements:
random nuber = new Random();
i = number.next();
to wherever it is that you create the timer.

C++/CLI multiple buttons maintaining variable value

OK let me explain my problem.
I'm working on a program where I have a button. Clicking it causes the number "1" to appear, then, after that, any further clicks will increment that value until it reaches the value of "9". (It's a string). I wrote this code which declares an int variable to 0 (Yes, this was a mistake but let me continue) then increment it and parse it to string and show it on the button text(This is the code that executes on button click):
private: System::Void a0_Click(System::Object^ sender, System::EventArgs^ e) {
int i = 0;
i++;
a0->Text = i.ToString();
}
However, as you can suspect, I did the foolishness of declaring i with 0 for each button press, so the result was that 1 was the only value showing on the button. The next thing I tried doing, was declaring i as global variable with the value of 0. However, I came to another problem. I have 82 buttons of that kind, and I'm going for the easiest sollution I can find, so sharing the i variable seemed logical,
The next problem was that if I pressed 5 times the first button, the number displayed on it would be "5" however if I pressed another button, the value wouldn't be "1" by default, it would be "6" (The value of the first button incremented by one). Basically it would inherit the value of the first.
Now I'm at a dead end. I have no idea what to do. I tried using i and i2 but I was just chasing my own tail. Is there a very easy solution to this? Keep in mind I've got 82 buttons (Yes I know it's alot) which are by default 0. When I click each one I need it to increment by one, starting from 0. Any ideas?
Notes: OS is Windows XP, IDE is Visual Studio 2010m using windows forms app, C++/CLI. If I forgot to mention anything post in comments and I'll add it.
You can inspect sender to find out what button was clicked.
void anybutton_Click(System::Object^ sender, System::EventArgs^)
{
Button^ btn = dynamic_cast<Button^>(sender); // or safe_cast
int i;
if (System::Int32::TryParse(btn->Text, i)) {
i++;
btn->Text = i.ToString();
}
}

Resources