user6415785
user6415785

Reputation:

Return a value from a class into a form

I am calling a function from a class where it will return on a label different colors depending on the value that shows on the label. Below is the method:

Function GetLabelColor(value As Integer) As Color
    If value <= 3.9 Then
        Return Color.Green
    ElseIf (value >= 4) And (value <= 6.9) Then
        Return Color.Orange
    ElseIf value >= 7 Then
        Return Color.Red
    End If
End Function

And then in the form I am using this code to call it:

Dim uicommon = New CommonUI()
Dim labelColor = uicommon.GetLabelColor(Integer.Parse(lblResultadoTotal.Text))

lblResultadoTotal.ForeColor = labelColor
lblGB.ForeColor = labelColor

But for some reason it will return me only the Green color even if the value is greater then 3.9. Do you have any solution who might help me?

Upvotes: 1

Views: 115

Answers (3)

Cody Gray
Cody Gray

Reputation: 244732

But for some reason it will return me only the Green color even if the value is greater then 3.9. Do you have any solution who might help me?

"Greater than 3.9" is not the same thing as ">= 4". In number theory, there are an infinite number of values between 3.9 and 4.0. The realities of binary floating-point representation changes that a little so that there are not actually infinitely many, but there are still a damned lot. Under no circumstances will the value 3.9 compare as >= 4.0.

If you want 3.9 to act like 4.0, then you should be rounding the values to whole numbers (integers). There is a built-in method to do that, named Math.Round. There is an overload that allows you to specify a value from the MidpointRounding enumeration, which controls how a number that ends in .5 should be treated. For example, AwayFromZero would round 2.5 to 4.0, while ToEven would round it to 3.0. For 3.5, the results would be the same in both cases.

Function GetLabelColor(value As Double) As Color
    Dim roundedValue As Integer = Math.Round(value, MidpointRounding.AwayFromZero)
    If (roundedValue >= 4) AndAlso (roundedValue < 7) Then
        Return Color.Orange
    ElseIf roundedValue >= 7 Then
        Return Color.Red
    Else
        Return Color.Green
    End If
End Function

Note: You probably want to use AndAlso in your condition, rather than And. The results will be the same in both cases, but AndAlso and OrElse are the preferred logical operators. The And and Or operators are reserved for bitwise operations, which is not what you're doing here. And hardly ever what you will be doing.

Upvotes: 0

mark_h
mark_h

Reputation: 5477

The function below does the same thing as the one you provided but accepts a double that will allow for decimals such as 3.9 to be passed to it

Function GetLabelColor(value As Double) As Color

    If (value >= 4) And (value < 7) Then
        Return Color.Orange
    ElseIf value >= 7 Then
        Return Color.Red
    End If 
    Return Color.Green
End Function

If you only want integers (whole numbers) passed to it then you should not do comparisons like value <= 3.9 because an integer will never equal 3.9. Instead you could use this version;

Function GetLabelColor(value As Integer) As Color

    If (value >= 4) And (value <= 6) Then
        Return Color.Orange
    ElseIf value > 6 Then
        Return Color.Red
    End If 
    Return Color.Green
End Function

If you use a Double version of the method then you will need to parse your string value using this;

Double.Parse(lblResultadoTotal.Text)

If you are not getting the value you expect it is not a problem with your method but the value being passed to it. Try hard-coding a value to test this; i.e. replace Integer.Parse(lblResultadoTotal.Text) with an actual number e.g. 5 and see if you get the color you expect.

This shows a simple console application that can demonstrate it working for the value 10.4

Imports System.Drawing

Module Module1

Sub Main()

    Console.WriteLine(GetLabelColor(Double.Parse("10.4")))

    Console.ReadLine()
End Sub

Function GetLabelColor(value As Double) As Color

    If (value >= 4) And (value <= 6) Then
        Return Color.Orange
    ElseIf value > 6 Then
        Return Color.Red
    End If
    Return Color.Green
End Function

End Module

Upvotes: 1

hdkhardik
hdkhardik

Reputation: 662

I guess you must use Double instead of Integer.

Upvotes: 0

Related Questions