I use SetItemChecked method to automatically check the checklistbox but it does not work. Anyone have a look to my code and please give a solution?
Private Sub Check(value As Double)
If 0 <= value < 20 Then
Me.CheckedListBox1.SetItemChecked(0, True)
ElseIf 20 <= value < 40 Then
Me.CheckedListBox1.SetItemChecked(1, True)
ElseIf 40 <= value < 60 Then
Me.CheckedListBox1.SetItemChecked(2, True)
ElseIf 60 <= value < 80 Then
Me.CheckedListBox1.SetItemChecked(3, True)
Else
Me.CheckedListBox1.SetItemChecked(4, True)
End If
End Sub
Then I call check(60) and it returns the 1st item check. It is wrong?
You need to correct your conditions as shown below also Clear
Private Sub Check(value As Double)
Me.CheckedListBox1.ClearSelected()
If 0 <= value And value < 20 Then
Me.CheckedListBox1.SetItemChecked(0, True)
ElseIf 20 <= value And value < 40 Then
Me.CheckedListBox1.SetItemChecked(1, True)
ElseIf 40 <= value And value < 60 Then
Me.CheckedListBox1.SetItemChecked(2, True)
ElseIf 60 <= value And value< 80 Then
Me.CheckedListBox1.SetItemChecked(3, True)
Else
Me.CheckedListBox1.SetItemChecked(4, True)
End If
End Sub
The first If-statement will always evaluate to True.
Because True is converted to 1 and False is converted to 0.
It's getting clearer if you look at this:
If (0 <= value) < 20 Then
So no matter which number is contained in value, the condition will be either...
True < 20
..or...
False < 20
For more information see here: Convert Boolean to Integer in VB.NET
So you need to change the conditions like following:
If 0 <= value And value < 20 Then ...
Related
I have the below code which returns the premium amount but i am not sure how to get this to display in another function. The sub routine i want to get the value into just displays text about the options selected then i need to display the value calculated based on those results.
Function ProcessClaims(ClaimsList As claimsList, PremiumIn As Decimal) As Decimal
Dim adjustedPremium, originalPremium As Decimal
Dim declined As Decimal
originalPremium = ClaimsList.claimValue * 100 \ 5
If ClaimsList.claimValue <= 5000 And ClaimsList.isPersonalInjury = False Then
adjustedPremium = originalPremium
ElseIf ClaimsList.claimValue > 5000 And ClaimsList.claimValue <= 10000 And ClaimsList.isPersonalInjury = False Then
adjustedPremium = originalPremium * 100 / 10
ElseIf ClaimsList.claimValue > 10000 Or ClaimsList.isPersonalInjury = False Then
adjustedPremium = -1.0
declined = -1.0
End If
If adjustedPremium = -1.0 Then
PremiumIn = declined
Else
PremiumIn = adjustedPremium
End If
Return PremiumIn
End Function
Any help would be appreciated, thank you
It's pretty straightforward ..
Your sub/function that displays the result should be defined as something like
Private Sub DisplayStuff(premiumInResult as decimal)
'display the value premiumInResult in here
End Sub
You can then either do this which is clearer
Dim result As Decimal = ProcessClaims(yourClaimsList , yourPremiumIn)
DisplayStuff(result)
or
Displaystuff(ProcessClaims(yourClaimsList , yourPremiumIn))
which is shorter but less clear, and makes debugging more difficult
I am trying to achieve the following:
The user is shown an excel spread sheet with a list of assumption which they can change.
Title | Value |
Input01 | 10 | =
Input02 | 2 | >=
Input03 | 800 | >=
Input04 | 4 | >=
Input05 | 2 | <=
There is an If .. Then Statement that pulls in data if the assumption are met. However if an assumption is blanc, it should not be included in the If .. Then Statement.
If x = Input01Value And y >= Input02Value _
And z >= Input03Value And a >= Input04Value _
And b <= Input05Value Then
User ommits Input03
If x = Input01Value And y >= Input02Value _
And a >= Input04Value And b <= Input05Value Then
Now I could check to see if each value exist, and then follow it by another If statement with the appropriate variables. But this seems a bit redundant.
I was wondering if something like the following is possible:
Input 01 = ""
If Input01Value != "" Then Input01 = "x = " & Input01Value
'Then use join or something similar to join all of them ..
And Then use this Input01 directly in the If .. Then statement. This way when a variable is empty the And .. are not included and the If statement will not fail.
Eg. (I know this doesn't work, just illustrating the scenario)
VBA: If Input01 Then
Result while compiling: If x = Input01Value Then
Please Note, I know I could do something like the following:
If Boolean And Variable2 > 4 Then and then have Boolean and Variable2 populate with a value in the cell, however the issue with this is that if the user, for example, decides to omit the Variable2 (which is reasonable) it will fail. eg. If (Boolean = True) And > 4 Then.
Hope my question is clear, thanks for the help.
What about using a function which operates on a select case depending on a string operator and two values?
Function conditionalString(condition As String, x As Variant, y As Variant) As Boolean
Select Case condition
Case "="
If (x = y) Then
conditionalString = True
Else
conditionalString = False
End If
Exit Function
Case ">="
conditionalString = (x >= y)
Exit Function
Case "<="
conditionalString = (x <= y)
Exit Function
Case ">"
conditionalString = (x > y)
Exit Function
Case "<"
conditionalString = (x < y)
Exit Function
Case Else
conditionalString = False
End Select
End Function
You could then just have another function, say "check if value isn't blank" before calling all assumptions.
Expanding on my comment, you can use something like this to test each row of input.
Function TestIt(testValue,inputOperator,inputValue) As Boolean
If Len(inputValue)=0 Then
TestIt=True 'ignore this test: no value supplied
Else
TestIt=Application.Evaluate(testValue & inputOperator & inputValue)
End If
End function
I have written a private sub which gets the current time and works out which time bracket (from a timespan array) the current time falls into. Given that there are ten time brackets I'm testing for, the code is repetitive and only the integer index of the array and the result integer change between each if statement.
If DateTime.Now.TimeOfDay >= tmeToday(0) AndAlso DateTime.Now.TimeOfDay <= tmeToday(1) Then
intPeriodFrid = 0 'ext morning; 8am - 9am
intNextBell = 1
ElseIf DateTime.Now.TimeOfDay >= tmeToday(1) AndAlso DateTime.Now.TimeOfDay <= tmeToday(2) Then
intPeriodFrid = 1 'morning break; 9am - 9:05am
intNextBell = 2
... ridiculous amounts of repetition later
ElseIf DateTime.Now.TimeOfDay >= tmeToday(10) AndAlso DateTime.Now.TimeOfDay <= tmeToday(11) Then
intPeriodFrid = 10 'ext afternoon; 3:10pm - 4:10pm
intNextBell = 11
End If
Then it hit me I could probably do it like this:
For value As Integer = 0 To 10
If DateTime.Now.TimeOfDay >= tmeToday(value) AndAlso DateTime.Now.TimeOfDay <= tmeToday(value + 1) Then
intPeriodFrid = value
intNextBell = (value + 1)
Exit For
End If
Next
This would/will be my first ever use of a loop (I'm ten weeks into a VB.NET based course), so I lack confidence in this as a solution, will it work? Any weaknesses/problems I need to look out for if I implement this?
I wrote code in VBA to extract data from Access database to Excel based on some input parameters. When it comes to If statement, one of the criteria which exam the range of "Speed" variable is true, however, I checked that this criteria should be wrong.
For example, speed=49, VSP=1.5, 1<=Speed<25 in 1st if condition indicates true which is ridiculous, and VSP<0 indicates false, so it goes to 1st elseif condition, 1<=Speed<25 still indicates true and 0<= VSP<3 is also true, then function returns the value from Access database. Else, if speed=49, VSP=6.5, the function still executes the "ElseIf (1 <= Speed < 25) And (0 <= VSP < 3) Then, statement 2" part. It seems it only always regard 1st elseif condition as true.
What's wrong with my if statement? Any advice?
Code:
Function F (ByVal Speed, VSP as single)
.............
If (1 <= Speed < 25) And (VSP < 0) Then
statement 1
ElseIf (1 <= Speed < 25) And (0 <= VSP < 3) Then
statement 2
ElseIf (25<= Speed < 50)) And (0<= VSP <3) Then
statement 3
End if
End function
Nobody has yet pointed how 1 <= Speed < 25 is evaluated. First, 1 <= Speed is evaluated as True or False, then that value is compared with 25. This requires interpreting True or False as an integer. True is interpreted as -1; False is interpreted as 0. Both of these are less than 25, so the expression will always evaluate to True.
This should be the following:
Function F(ByVal Speed, VSP As Single)
If (1 <= Speed And Speed < 25) And (VSP < 0) Then
' statement 1
ElseIf (1 <= Speed And Speed < 25) And (0 <= VSP And VSP < 3) Then
' statement 2
ElseIf (25 <= Speed And Speed < 50) And (0 <= VSP And VSP < 3) Then
' statement 3
End If
End Function
Your if is totally wrong.. you can't write the if in that way...
You should check one condition at each time, or at least, check one condition and then other and then other...
So
If (1 <= Speed < 25) And (VSP < 0) Then
Should be
If (1 <= Speed) and (Speed < 25) And (VSP < 0) Then
I could not understand why you are not getting any error on this...
What you've done, has check for 1<= speed (that could be true) and then has check true < 25... and so on... no good...
What is the best method to determine if the current time is AM or PM using VB.NET?
Currently I'm using If Date.Today.ToString.Contains("AM") but I'm sure there is a better method.
Good <%If Date.Today.ToString.Contains("AM") Then Response.Write("Morning") Else Response.Write("Afternoon")%>
If Date.Now.Hour < 12 Then ... perhaps?
Now.ToString("t") == "A" //or //"P"
How about this:
Now.Hour > 17 ' Night
Now.Hour < 5 ' Day
HTH
The afternoon check needs to be <= otherwise at hour 17 str nevers gets set
dim hours as int = DateTime.Now.Hour
dim str as string
if hours < 12 Then
str = "Morning"
else if hours <= 17 then
str = "Afternoon"
else if hours > 17 then
str = "Evening"
End If
How about this:
dim hours as int = DateTime.Now.Hour
dim str as string
if hours < 12 Then
str = "Morning"
else if hours < 17 then
str = "Afternoon"
else if hours > 17 then
str = "Evening"
End If
Private Function IsMidnight(ByVal value As DateTime) As Boolean
Return value.Hour = 0 And _
value.Minute = 0 And _
value.Second = 0 And _
value.Millisecond = 0
End Function
Usage:
Dim isAM as Boolean = (IsMidnight(current) Or current.Hour < 12)
Dim dateValue = Dtp.Value.ToString()
If dateValue.EndsWith("AM") Then
MsgBox("Its AM")
Else
MsgBox("Its PM")
End If
You can do something like this.