CanerGures
CanerGures

Reputation: 331

How to read int value from entry in xamarin?

I am trying to get int values from entry. .Text gets strings but I don't know how to hold int values. Kullanicilarr.Telefonno and Kullanicilarr.GrupID are restricted to get long and int values, because of that i cant use Phone.Text or GrupID.Text. Are there any parameter name like ".Text" i can use for getting int values ?

<StackLayout Spacing="20" Padding="15">
            <Label Text="Name" FontSize="Medium" />
            <Entry Text="Enter Name" FontSize="Small" x:Name="Name" />
            <Label Text="Phone" FontSize="Medium" />
            <Entry Text="Enter Phone Number" FontSize="Small" x:Name="Phone"/>
            <Label Text="Group Id" FontSize="Medium" />
            <Entry Text="Enter Group id" FontSize="Small" x:Name="Group"/>
            <Button x:Name="btn" Text="Add" Clicked="btn_Clicked"/>
        </StackLayout>
async void btn_Clicked(object sender, EventArgs e)
        {
            Kullanicilarr.Kullaniciadi = Name.Text;
            Kullanicilarr.Telefonno = Phone.Text;
            Kullanicilarr.GrupID = Group.Text;

            await Navigation.PopModalAsync();
        }

I expect to hold values in that Kullanicilar."something" with Group."something"

Upvotes: 0

Views: 1003

Answers (2)

CanerGures
CanerGures

Reputation: 331

I figured it out. Seems that there is a Convert paramater there.

async void btn_Clicked(object sender, EventArgs e)
    {
        Kullanicilarr kullanici = new Kullanicilarr();
        kullanici.Kullaniciadi = Name.Text;
        kullanici.Telefonno = Convert.ToInt64(Phone.Text);
        kullanici.GrupID = Convert.ToInt32(Group.Text);
        await Navigation.PopModalAsync();
    }

Upvotes: 1

Cheesebaron
Cheesebaron

Reputation: 24470

You will need to convert them into ints. Entry texts are always strings, but you could probably restrict input to be integers.

However, you can easily try converting the values with:

var parseOk = int.TryParse(Phone.Text, out int result);

Then you can test whether the value was an int before trying to assign it to the model:

if (parseOk)
    Kullanicilarr.Telefonno = result;

Upvotes: 2

Related Questions