If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Adding decimal in textbox and calculation

Imagine someone who has never seen your code before. What would you expect the name 'txtPTextBox' to indicate to them? I am such a person and it indicates to me that it refers to a TextBox but not what the purpose of that TextBox is. Both of those are bad things. If the variable is of a numeric data type then the name should reflect that and the name should also indicate what that number represents.

Re: Adding decimal in textbox and calculation

I understand, txtPTextbox indicates a Pressure whilst txtVTextBox indicates a volume.
if someone enters a value of < 1.5 bar (1,5 bar) the program needs to display a text if it is above this value it should display an other text. It works fine when i use integers, but not with decimal numbers.

Re: Adding decimal in textbox and calculation

As a trained chemist and chemical engineer, I thought that that was likely. I thought that there might be some other possibilities too though.

Given that you know the specific purpose of your app, it may seem obvious to you what they are but they may not be to others or even to you if you leave this project and come back to it later. I would suggest not using Hungarian nothation at all but, if you are going to use it, make sure that you use it properly. "txt" is an appropriate prefix for a TextBox variable but definitely not for an 'int' or 'decimal'. The original prefix for integers was 'n' but most people use 'int' now. 'dec' is what most people use for decimals. Personally, I would use 'pressureTextBox' and 'pressure' as the variable names for the TextBox containing the pressure value and the actual pressure value as they tell you explicitly and unambiguously what they are. They don't rely on prior knowledge or additional comments to be clear and, most importantly, they don't mislead. If you want to stick with Hungarian notation and abbreviation though, use 'txtP' and 'decP'.

Originally Posted by rakker

if someone enters a value of < 1.5 bar (1,5 bar) the program needs to display a text if it is above this value it should display an other text. It works fine when i use integers, but not with decimal numbers.

Just telling us that something doesn't work is rarely enough. You need to tell us what actually happens. If you did it right then it would work so you must have done it wrong. Show us what you did and tell us what happened when you did it and then we can most likely tell you what's wrong and what needs to change. If an exception is thrown, point out exactly where. We should never have to figure stuff out that you already know. If you provide all the relevant info, we'll probably spot the error pretty quickly, as we'll have seen (and done) similar things many times.

The error points to: PressureTextBox = decimal.Parse(Pressure.Text);
I understand that it points to this textbox since i tried the input value "12.5" and the system does not understand this value as far as i can see.

Re: Adding decimal in textbox and calculation

I appreciate that you have tried to use the variable names I suggested but you've got things backwards. I would have hoped that it would be obvious (particularly given that I openly stated it) that 'pressureTextBox' would be used for the TextBox and 'pressure' would be used for the decimal.

As for the issue, it is pretty much certain that the exception is NOT occurring when trying to parse "12.5" from a TextBox. That would work without issue. If you stop ignoring the information that the IDE is trying to provide and look at the error message then you would see what the actual problem is. It will be telling you that some String that doesn't look like a number (probably an empty String) cannot be converted to a decimal.

The Parse method requires that the input be valid and yours clearly isn't. What you really should do is call TryParse instead. It will validate the data first and then only convert if it is valid. That will allow you to display a message to the user, telling them exactly what they did wrong and asking them to correct it. Make sure that you research the TryParse method before calling it. It will require a small restructure of your code but there are plenty of examples around, including in the MSDN documentation.

Re: Adding decimal in textbox and calculation

I don't think that that's going to help. A decimal point is allowed by default when parsing a String to a Decimal so you'd only use a NumberStyles value to exclude something that was usually allowed or to include something like the ability to parse hex or currency. The only reason that I can think of that Decimal.Parse would fail on "12.5" is if the system culture was using a comma as a decimal separator, but I would have assumed that the OP would already know if that was the case.

Re: Adding decimal in textbox and calculation

Originally Posted by jmcilhinney

I don't think that that's going to help. A decimal point is allowed by default when parsing a String to a Decimal so you'd only use a NumberStyles value to exclude something that was usually allowed or to include something like the ability to parse hex or currency. The only reason that I can think of that Decimal.Parse would fail on "12.5" is if the system culture was using a comma as a decimal separator, but I would have assumed that the OP would already know if that was the case.

Good point, I think I was confusing it failing when dealing with currency symbols - in my defence it was early and I hadn't had a coffee.

I do not understand how i can get the system to work with the for example "<" and ">=" symbols.
It gives an error. while it works fine with integers.

Firstly, if there's an error then there's an error message. Keeping that error message a secret from those whom you want to help you is not a great strategy. Pointing out WHERE the error occurs would a good idea too, don't you think? There's only one line in there that uses a comparison operator and it's commented out, so the code you've actually shown us will generate no errors at all related to comparison operators. We are not psychic. We only know what you tell us so you have to tell us everything that is relevant and preferably clearly. Telling us that errors occur but not what they are or where they occur is not generally of much value and showing us code other than what you're actually running is also of little value.

That said, if you have a Double variable named DoublePallow and you have populated that with the number that results from converting a String, it seems reasonable to you would then compare that DoublePallow variable to another Double value. If that's your number then there's no point comparing something else and there's no good reason to compare a Double to a literal Decimal when you can compare it to a literal Double. If what you are actually trying to achieve is check that a TextBox contains a valid Double value and that that Double value is less than 0.5 then doing so is very easy. You use Double.TryParse to validate and convert and then compare the converted value:

csharp Code:

if(double.TryParse(myTextBox.Text, out myDouble) && myDouble < 0.5)

{

// A valid number has been entered and it is less then 0.5.

}

There is absolutely no difference between that and how you would properly do the equivalent for integers.