Convert String to Decimal

3

And I'm having trouble correctly converting this string to decimal .

decimal.Parse(txtValor.Text)=meuVen.Valor;

When I place the conversion in front, it shows error after = . I do not know how to convert the second part, or if I should change the second part instead of the first.

The teacher code is:

private void Leitura2() 
{ 
    txtCod2.Text = dgvRegistros2.Rows[dgvRegistros2.CurrentRow.Index].Cells["codigo_servico"].Value‌​.ToString(); 
    Vendas meuVen = new Vendas(int.Parse(txtCod2.Text)); 
    txtNome.Text = meuVen.Nome; 
    decimal.Parse(txtValor.Text) = meuVen.Valor; 
    txtEntrega.Text = meuVen.Entrega.ToString("dd/MM/yyyy"); 
}
    
asked by anonymous 13.09.2015 / 09:02

4 answers

3

You may not have found the right method to do the conversion. It even works but it is not usually the most indicated. If something is invalid, the conversion will generate an exception. It is generally a mistake to try to catch an exception just because the value is invalid. So the best method is this:

decimal valorDecimal;
if (decimal.TryParse(txtValor.Text, out valorDecimal)) {
    meuVen.Valor = valorDecimal; //note que você quer atribuir o resultado a uma variável
} else {
    //faz um tratamento de erro aqui
}

See the difference between the two methods and how it works.

To assign a value to a variable, it must always be on the left. So in this case you read the opposite: "you are assigning valorDecimal to meuVen.Valor ."

We could have helped more with a larger context.

Doubt is indeed basic and this is worrying because it is not by the use of the method but by how to write an assignment. With such a basic error it will be difficult to do more sophisticated things, such as handling the error. And worse, identify when there is potential for error, test correctly to ensure that the program works correctly in all situations and not just in the ideal condition.

    
13.09.2015 / 13:54
0

convert this to a class and start creating libraries from all the code you implement for each project you do. so excuse me from writing the same code several times

public void ConvertStringDecimal(string stringVal) {
        decimal decimalVal = 0;

        try {
            decimalVal = System.Convert.ToDecimal(stringVal);
            System.Console.WriteLine(
                "The string as a decimal is {0}.", decimalVal);
        } 
        catch (System.OverflowException){
            System.Console.WriteLine(
                "The conversion from string to decimal overflowed.");
        }
        catch (System.FormatException) {
            System.Console.WriteLine(
                "The string is not formatted as a decimal.");
        }
        catch (System.ArgumentNullException) {
            System.Console.WriteLine(
                "The string is null.");
        }

        // Decimal to string conversion will not overflow.
        stringVal = System.Convert.ToString(decimalVal);
        System.Console.WriteLine(
            "The decimal as a string is {0}.", stringVal);
    }   
    
13.09.2015 / 20:45
0

At the end of the day, I discovered that there was no need to convert to that form, I have re-formatted all "cs" and "form" for sales, and only one of them with the help of a button was able to do the sum. I ended up using the following method:

private void btnCalc_Click(object sender, EventArgs e)
    {
        decimal vl1, vl2, result;
        vl1 = decimal.Parse(txtValor.Text);
        vl2 = decimal.Parse(txtValProd.Text);

        result = vl1 + vl2;
        txtValtotal.Text = (result.ToString());

    }
    
14.09.2015 / 07:48
-1
   System.Convert.ToDecimal(stringVal);
    
16.09.2015 / 16:24