C# Arrays and Loops with Xamarin.Forms

Sharing buttons:

hello welcome to technical founders my

name is carlos lara software engineer

and entrepreneur and in this video we'll

be examining c-sharp erase and loops

with is Ameren forms in the previous

video we examined conditional statements

with if-else here upon a button clicked

so we have our applications here and for

example you clicked a button whenever

you clicked the label would change so it

says button clicked and it shows the

current text in the label

hello universe from technical founders

you hit OK and then it changes the label

to the other label that we had to find

here which was we are learning mobile


so we have our again our button here

that we created and the constructor

method for the page and then on the on

button clicked event good we examined

events in the in the last video and

conditional stay in the various an

introductory video to conditional

statements so depending on what the text

is it changes to the other one so it's

if one do this and if it's the other

it's text so else do the other things

you click and you keep flip flipping

back and forth and last time we only ran

it on iOS but here we have it on the

Android as well and if you click same

thing and by the way if you click here

it looks different right because again

with xamarin forms at the you write the

code once and simultaneously you get it

for both iOS and Android and it looks

different right because iOS any Android

are different so this would be what we

call native iOS and this is native

Android so you click OK it flips and

here the same thing you click OK and it

flips so if you if you click again flows

back to whole universe from technical

founders and so on and well again in

this video we'll just do iOS just to

keep it on simpler or just to make this

video just a little bit shorter so let's

just stop running the current

application and now here we want to do a

race instead of of these conditional

statements so we'll go ahead and delete

this if-else statement now a string is a


objects of a certain type for example

string or integer and then so you hold

it in a in a collection and array and

then you can do things without a race

with with that with those objects and an

array is a fixed size so in c-sharp the

way that you create or declare an array

is you say the type of the array so in

this case we're going to do an array of

string strings as you like the text

right so just a string of characters and

then before we used what we we just said

string for example name equals carlos

learn for example my name this is how

you declare a string now to declare a

string array here you do brackets you

just open a couple of brackets and it

just in c-sharp this just means an array

of strings so here we're going to create

an array let's give it an identifier

called names and then assign a new

instance of the stuff of this array so

we're going to a new string and then

here when you're creating the new

instance of this array object we have to

define how many objects are going to be

how many string objects are going to be

inside of this array so for example

let's say 3 and erase though and close

with a semicolon now erase the way that

they work is they have a set number of

elements in them so once you created it

has that set so it has three spots so

for example we'll be able to add three

objects of type string into this array

here so let's so the way you do that is

so you grab the identifier names you do

dot actually no not we're not going to

use the dot yet you actually just open

up the brackets again and then you're

you're going to say which index so right

now this is an empty array so it has

three spots but they have there's

nothing in them there they're empty and

the way that that arrays work is I'm

going to do a little comment here the

they enumerate the elements starting

with the index 0 so

use indices in the and they start at

zero so for example this string that has

three spots three elements would have

the indices zero one and two

it'll be three elements and that is how

we reach them so for example in the

names array of strings we want to grab

the zero spots so that the very first

spot and here we just want to assign a

name for example so we are actually

going to say xamarin forms which is what

we're using right now to develop close

it now add the zero index so at the

first spot on the index we've added this

string and again strings are always in

and inside quotation marks and now we

have two spots left so let's go ahead

and fill those out too so names again

now at one which is the next spot let's

say xamarin iOS which would be if we

were doing development just an iOS

exclusively and this is our iOS project

right here this is our Android and this

is our xamarin forms right here okay now

for the last one - which will be the

third spot right 0 1 - we're going to

say summer and Android close ok so we

have our array we've declared an


we want to do something to perform

perform an operation with each element

of that array so for this will will use

our the first kind of loop that we're

going to look at in this video which is

called a for each loop so we so we say

for each and then we tap twice hit tab

twice and visual studio here

automatically brings up this snippet so

for each will s a parameter here we'll

grab variable item so we'll grab a

variable so for example fur and you can

name it whatever you want so because

we're going to be working with this

array of strings called names we're

going to say for each name in names so

for each

name in the array names we are going to

perform on an operation here so what we

are actually what we're going to do here

is we are like in the previous video we

are going to display an alert so we are

going to say display alert method open

parenthesis and in nice again three

parameters so for the title and let's

actually it takes a string so let's just

do the actual name the element that

we're grabbing right now so it's gonna

grab the first one so it's going to

iterate this for each loop it's going to

iterate through this array and grab and

do an operation with each one

consecutively so the first iteration

through the first loop it's going to

grab this element here so name would be

what's inside there so name would be

some forms in this case so we'll be the

first parameter and then the second

parameter here will be a message so

let's just say I love xamarin forms so

there's a name and then for the third

parameter let's just say okay like the

button just to like get out of that does

a alert box display alert here it's

complaining because I wants the await

keyword because again when we have an

async method you need the await keywords

so this means that this method again

will continue opera executing before

continuing with the loop so until you

hit OK it will actually continue through

the loop so let's um let's actually run

this and let's see let's see what we did

moment bring it up on iOS so again we

have the same things on it but now when

we click the button see it brings up it

grabs the the name for so for each name

in names so for each element in the

array it's going to grab grab it which

is already a string so you don't need to

do any kind of converting here to a

string because it's already a string

that's the kind of parameters that

display alert takes so you have the name

as a title salmon forms some forms and

then for the set for the message you I

love and then

we append we put these two strings

together so I love naina xamarin forms

the same thing and the button says okay

then you hit okay so and the loop is

going to run three times because your

array has three elements so you hit okay

then you get Samer and iOS right your

second one I loves em on iOS and you'll

get a more Android for the last one

which you actually do and if we run this

on Android we'll get the same thing

except with the Android specific look

hit okay and that's it so your for each

went three times around and grabbed the

elements here so that's how that's a

very simple example of how to use a for

each loop to iterate through an array

now let's stop running it and then we'll

do one last array here let's delete this

one here and let's actually do an array

of integers so the same thing integer

and then we open the brackets to the to

say that it's an array and we'll simply

we'll just call it integers here and

then again you create a new instance and

and here we want let's say five integers

just give it that and here we're

actually not going to set them manually

as in here two to the array the values

we're not going to add them manually we

are going to use a different kind of

loop here which is called a for loop and

in visuals to you you can just type for

hit tap twice and it's going to bring up

this the code snippet here so let's just

do that okay so we have the array so it

it's this is like for each this also

iterates but this one iterates a certain

number of times and you can tell it like

to stop at a certain point and this is

what max so you started with a

iteration variable that we that is just

here by default to find us I which

starts at 0 so this will be the first

loop and then it's going to run certain

loops until it hits a maximum value and

here you can just say whatever you want

10 3 whatever but here we just want to

iterate as far as the length of this

integers array so we're going to say

here in integers dot

length and the length property here in

the in the integer integers array gives

you the length of of this so the length

is 5 as we've declared it here and now

this is going to start at 0 so it's

going to is going to go like this it's

going to go 1 2 0 1 2 3 4 and then I

less than the length so the length is 5

so when I is less than 5 it's going to

stop so so when it hits 4 it's going to

run that loop and then it stops so 1 2 3

4 5 so it's going to run the 5 loops for

each one here and the reason that this

is like an offset you know 5 & 4 is just

because the iteration variables

typically start here at 0 but that's

that's fine and then as you run through

each loop at once once each loop its

finished this I plus plus means it's

going to increase this I integer

variable plus plus means it increases it

by 1 so it's gonna run once it starts at

0 and then it's going to increase it to

2 1 and then 2 3 4 and then the the loop

is going to to end so let's say what do

we want to do to do here so we want to

add them to the array so 4 so let's say

we'll grab integers and again and here

last time we explicitly said 0 1 etc for

adding here we'll just pass I and I the

first time is going to be 0 the second

time is going to be 1 and so on so it's

going to be what we needed to be every

time so integers I and here we are going

to assign at an integer right this is an

array of integers so we're going to add

an integer and I is already an integer

so we'll just add right just like that

and we also want a let's just also

display alert again just so we see and

for the title and let's say just nothing

let's just leave the title empty and

then for the message will just say

integer and then now this is taking a

string and then

and here we have an integer so what

we're going to do is we're going to grab

I and say dot and grab the two string

method that's in inside of I I contains

a two string method that it's it's a

c-sharp method that's going to convert

the integer type to a string type so if

it's zero for example or one is going to

convert it to the same zero or one but

now in string type so we can actually I

put it here so integer like this and

then let's just say add it to the array

let's just say let's just keep it simple

let's just say that and then okay for

the last one close and then we're good

to go let's actually run this and let's

see what we get

just on iOS again just to keep it simple

for now so once you click the button is

good it's going to go it's going to

start iterating through this loop so the

first time okay

integer zero add it to the array integer

zero matter to your array here we added

zero of this spot okay and then here in

the display alert and then the actual

message we say integer plus and then

this is where the zero comes from so you

have your I which is your integer to

string so this is just strain but you

know it just looks it just looks the

same it's just so though so it's type

compatible here that they're all strings

so click OK and it's going to go again

now integer one add it to the array

again integer two added three four and

then that's it so now we've added all

the spa all the integers here to our

array which only contain which contain

five and we added two five zero one two

and three and four which are the five

integers now what happens if you

actually try to add another one let's

say so your array contains can only hold

five elements so what if you say I less

than or equals so your so this will

cause the loop to run one extra time and

try to add another element a sixth

element to this array but it can only

hold five so what's gonna happen if we

actually do this let's stop and run

zero one two three four so so far we're

good but now it's going to try to add an

element and to the array but the array

is fold it has doesn't have any spots

left so we are going to get an error and

this is what we call a an index out of

range exception and in in in another

video we'll actually get into how to

handle exceptions except this index out

of range exception is a type of

exception exception is just a fancy word

that we use for errors of runtime errors

so runtime just because they happen

while the application is running at the

time that it's running that's why it's a

runtime exception so we'll just stop it

right here but in another video we'll

take a look at handling exceptions and

debugging here so let's just close this

and let's just go back and that it works

so thank you for watching I hope you

liked it this is again just an

introduction to erase and loops and

we'll keep building on this knowledge

and the subsequent videos so if you have

any questions any thoughts any comments

please leave them leave leave them at

the bottom also please subscribe so

you're the first to be notified and I'll

see you next time