1
I have 4 text entries and a button:
<Entry x:Name="Densidade"
Keyboard="Numeric"/>
<Entry x:Name="Volume"
Keyboard="Numeric"/>
<Entry x:Name="Area"
Keyboard="Numeric"/>
<Entry x:Name="Custo"
Keyboard="Numeric"/>
<Button x:Name="Button"
Text="Calcular"
HorizontalOptions="FillAndExpand"
VerticalOptions="EndAndExpand"
Clicked="Handle_Clicked"/>
In the code Behind I convert them to double, make a calculation and send the result to a Page2 through Classe2
//Botão que transforma os dados em variáveis e passa para a página de resultados
private async void Handle_Clicked(object sender, EventArgs e)
{
class2 = new Class2();
//Converte os valores obtidos na entrada em tipo double
double a, d, r, v;
a = Double.Parse(Area.Text);
d = Double.Parse(Densidade.Text);
r = Double.Parse(Custo.Text);
v = Double.Parse(Volume.Text);
//calculo
class2.Calculo1 = d * a * (v / 1000) * r;
//chama Page1 passando objeto class2
await Navigation.PushAsync(new Page1(class2));
}
However, if any of the entries is not filled in, the following error occurs:
I’ve tried solutions like:
private async void Handle_Clicked(object sender, EventArgs e)
{
class2 = new Class2();
double a, d, r, v;
if (Densidade.Text == "")
d = 0;
else
d = Double.Parse(Densidade.Text);
if (Volume.Text == "")
v = 0;
else
v = Double.Parse(Volume.Text);
if (Area.Text == "")
a = 0;
else
a = Double.Parse(Area.Text);
if (Custo.Text == "")
r = 0;
else
r = Double.Parse(Custo.Text);
//calculo
class2.Calculo1 = d * a * (v / 1000) * r;
//chama Page1 passando objeto class2
await Navigation.PushAsync(new Page1(class2));
}
or
private async void Handle_Clicked(object sender, EventArgs e)
{
if ((Densidade.Text == "") || (Volume.Text == "")
|| (Area.Text == "") || (Custo.Text == ""))
{
await Navigation.PushAsync(new MainPage());
await DisplayAlert("Atenção", @"Todos os campos devem ser preenchidos", "Ok");
}
else
{
class2 = new Class2();
//Converte os valores obtidos na entrada em tipo double
double a, d, r, v;
a = Double.Parse(Area.Text);
d = Double.Parse(Densidade.Text);
r = Double.Parse(Custo.Text);
v = Double.Parse(Volume.Text);
//calculos
class2.Calculo1 = d * a * (v / 1000) * r;
//chama Page1 passando objeto class2
await Navigation.PushAsync(new Page1(class2));
}
}
Instead of
a = Double.Parse(Area.Text);
try to useDouble.TryParse(Area.Text, a);
– Matheus Ribeiro
I tried, appears "Argument 2 should not be transmitted with the keyword 'out'". I do not know where the error is, nor how to solve it.
– Homero Kemmerich
Sorry, I forgot a little something, the right thing is
Double.TryParse(Area.Text, out var a);
– Matheus Ribeiro