7 Replies - 632 Views - Last Post: 16 August 2019 - 01:37 AM Rate Topic: -----

#1 J54   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-December 18

Simple IF statement

Posted 15 August 2019 - 10:10 AM

I need help with simple IF statements making a calculator

  private void BtnCalculate_Click(object sender, EventArgs e)
        {
           
          if (txtNumberGrade.Text <= 100)

            {
                txtLetterGrade.Text = "A";
              


            }




I need the less and greater than sign but it doesn't allow in if statement. How can I fix this so it works?

Is This A Good Question/Topic? 0
  • +

Replies To: Simple IF statement

#2 J54   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-December 18

Re: Simple IF statement

Posted 15 August 2019 - 10:28 AM

This chapter im working on doesn't go over cases yet so im not using that method. The presentation slides use the if statement
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2754
  • View blog
  • Posts: 4,411
  • Joined: 21-June 11

Re: Simple IF statement

Posted 15 August 2019 - 10:29 AM

It is, of course, perfectly allowed to use <= in if statements, so you're misstating what the error is. It would have been better had you posted the actual error message.

You didn't say what the types of txtNumberGrade or txtNumberGrade.Text are, but it stands to reason that the latter would be a string. If so, you're getting a type error because you can't use <= on strings (neither inside nor outside of if statements). You'll want to convert your string to a number before comparing it to 100.
Was This Post Helpful? 0
  • +
  • -

#4 J54   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-December 18

Re: Simple IF statement

Posted 15 August 2019 - 10:31 AM

yes that's my error the string conversion

This post has been edited by Skydiver: 15 August 2019 - 06:12 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#5 J54   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-December 18

Re: Simple IF statement

Posted 15 August 2019 - 10:39 AM

can I be shown an example im pretty new to this
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,181
  • Joined: 12-June 08

Re: Simple IF statement

Posted 15 August 2019 - 11:20 AM

I suggest reading on up on casting and conversion.

https://docs.microso...ype-conversions
Was This Post Helpful? 0
  • +
  • -

#7 DarenR   User is offline

  • D.I.C Lover

Reputation: 634
  • View blog
  • Posts: 4,209
  • Joined: 12-January 10

Re: Simple IF statement

Posted 15 August 2019 - 11:40 AM

you will want to use decimal

ie
Convert.ToDecimal(something)

Was This Post Helpful? 0
  • +
  • -

#8 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7483
  • View blog
  • Posts: 15,510
  • Joined: 16-October 07

Re: Simple IF statement

Posted 16 August 2019 - 01:37 AM

A Convert will result in an error thrown if the convert fails. If you are 100% sure that string is an int, then maybe. If not, then you'll want to catch that inevitable error.

In almost all cases, you should to perfer TryParse.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1