7 Replies - 491 Views - Last Post: 09 March 2012 - 02:27 PM Rate Topic: -----

#1 solorize  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 28-February 12

If statement to check if value is +-1 of initial value held in a var

Posted 09 March 2012 - 01:06 AM

Hi

I need to run an If Statement that will check a few conditions.

Basically I want to get it to check a value held in a variable "Temperature"
and if the value is within "1" eaither way i.e. +-1 of the value held within
the variable to then to output a text string to Label200.Text Else if the
condition is not met then out put another text string to Label200.Text.

Example:

Say if Temperature value held in the variable Temperature is 8.5
the if statement checks to see if the DewPoint value is "between" +-1 of the
temperature value. i.e. 7.5 to 9.5 as well as the value in
Humidity is equal to or greater than 90 and Pressure is Less than or Equal
to 1013.25 then the conditon is met, else the conditon is not met.

I hope the above makes sence.

I have put some code together below in NotePad++ to see if what I am trying to
do is the correct way to go about it (please note I have not yet tried this code
in VB.NET as I don't have it at work).

Could someone pass comment on the code and if it is wrong explain what I should
do to get it to work.


Dim DewPoint as Single
Dim Temperature as Single
Dim Humidity as Single
Dim Pressure as Single


DewPoint = Label5.Text
Temperature = Label3.Text
Humidity = Label4.Text
Pressure = Label11.Text


If DewpPoint = Temperature -1 Or DewPoint = Temperature +1 And Humidity >=90 And Pressure <= 1013.25 then
        Label200.Text = "Precipitation Possible"
  
   Else
        Label200.Text = "No Precipitation likely"

End If



I am not sure that the code: Temperature -1 Or DewPoint = Temperature +1 will check for
the values inbetween minus 1 of the Temperature value up to plus 1 of the Temperature Value.
i.e. 7.5, 7.6, 7.7, 7.8 ........ 9.1, 9.2, 9.3, 9.4, 9.5

Any help would be appriciated.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: If statement to check if value is +-1 of initial value held in a var

#2 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: If statement to check if value is +-1 of initial value held in a var

Posted 09 March 2012 - 01:48 AM

first of all let me ask you one question. From where are you assigning the values to the variable declared above? are the values hardcoded?

This post has been edited by nK0de: 09 March 2012 - 01:49 AM

Was This Post Helpful? 0
  • +
  • -

#3 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: If statement to check if value is +-1 of initial value held in a var

Posted 09 March 2012 - 02:36 AM

If DewpPoint >= (Temperature -1) Or DewPoint =< (Temperature +1) And Humidity >=90 And Pressure <= 1013.25 Then

This is worth a try. It hasn't been tested so it may not work. But it might. Let's hope so :)
Was This Post Helpful? 0
  • +
  • -

#4 solorize  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 28-February 12

Re: If statement to check if value is +-1 of initial value held in a var

Posted 09 March 2012 - 06:13 AM

nKode,

The values are NOT hardcoded. These values for the variables are populated
from values which are shown on various Labels, which in turn are updated
at a set interval from an array, who's data is populated from readings a
remote txt file that has raw weather data in it.

So all variable values can/will change at a set 30second interval.

I hope that makes sense.


DimitriV,

Thanks for the code, I will give that a go when I get home, and will
let you know if it works.
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: If statement to check if value is +-1 of initial value held in a var

Posted 09 March 2012 - 06:15 AM

Separate it out it a distinct function

Public Module Exts

 <Extension()>
 Public Function WithIn(ByVal thisValue As Integer, ByVal ofValue As Integer, ByVal Range As Integer) As Boolean
   Return ((ofValue-range) <= Value) AndAlso (Value <= (ofValue + Range))
 End Function

End Module


Example
If DewPoint.WithIn( Temperature, 1 )  Then



WithIn can also be built on top of another function call _IsBetween(Of T)

This post has been edited by AdamSpeight2008: 09 March 2012 - 06:22 AM

Was This Post Helpful? 1
  • +
  • -

#6 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: If statement to check if value is +-1 of initial value held in a var

Posted 09 March 2012 - 06:21 AM

well, if it was me, I would have done something like this.

  
Dim minVal, maxVal As Single
minVal = Temperature - 1
maxVal = Temperature + 1


If DewPoint > minVal And DewPoint < maxVal And Humidity >= 90 And Pressure <= 1013.25 Then
     MessageBox.Show("Precipitation Possible")
Else
     MessageBox.Show("No Precipitation likely")
End If


But I like AdamSpeight2008's way better.

This post has been edited by nK0de: 09 March 2012 - 06:23 AM

Was This Post Helpful? 1
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: If statement to check if value is +-1 of initial value held in a var

Posted 09 March 2012 - 06:26 AM

I recommend using AndAlso over And

And evaluates both sides before doing the comparision

AndAlso does is does short-circuiting of the arguments, if the first evaluates to False it doesn't evaluate the second.


Think about functions.

Function PercipitationLikely(DewPoint As Double,Temperature As Double, Humidity As Double, Pressure As Double) As Boolean
  Return Dewpoint.WithIn(Temperature,1) AndAlso ( Humidity >= 90 ) AndAlso ( Pressure < 1013.25 )
End Function


It makes your source-code more readable (Named Parameters)
If IsPercipitationLikely(DewPoint:= , Temperature:= , Humidity:= , Pressure:= ) Then 

This post has been edited by AdamSpeight2008: 09 March 2012 - 06:35 AM

Was This Post Helpful? 1
  • +
  • -

#8 solorize  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 28-February 12

Re: If statement to check if value is +-1 of initial value held in a var

Posted 09 March 2012 - 02:27 PM

Thank you all for your replies.

I tried the code that nKode, suggested and tweaked it a little bit as shown below
and it works a treat. I had to amend it slightly to include brackets and adjusted the
+ and - for the MinVal and MaxVal to 1.1 so it would work correctly
i.e. if the temperature was 10 and the DewPoint was 9, it would accept 9 as being 1 off of 10
but by setting the + and - for the MinVal and MaxVal to 1.1, it would accept it =)



Public Class Form1
    Dim Temperature As Double
    Dim DewPoint As Double
    Dim Humidity As Double
    Dim Pressure As Double

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        DewPoint = TextBox1.Text
        Temperature = TextBox2.Text
        Humidity = TextBox3.Text
        Pressure = TextBox4.Text


        Dim minVal, maxVal As Single
        minVal = (Temperature - 1.1)
        maxVal = (Temperature + 1.1)


        If DewPoint > minVal And DewPoint < maxVal And Humidity >= 90 And Pressure <= 1013.25 Then
            MessageBox.Show("Precipitation Possible")
        Else
            MessageBox.Show("No Precipitation likely")
        End If
    End Sub

End Class


I will also take a look at Functions as stated by AdamSpeight2008 to see if I can
get that to work within my program as well as I am alway looking to learn

Thanks again for everyone who has given me advice on how to achieve what I needed to do.
It is a real help for me as I am only just starting out learning VB.NET (after a 7 year
break from self learning how to programming in VB6, after my HDD died and I lost all
my code), I am looking forward to getting back into programming =)

Thank you all for your replies.

This post has been edited by AdamSpeight2008: 09 March 2012 - 02:29 PM
Reason for edit:: Fixing Double Posting

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1