user445714
user445714

Reputation: 393

Using the returned Value of a getter method in another method

Just playing about with some vb.net and i dont understand why when i enter Dog into the text box the label continues to say not dog?

Public Class Form1
    Dim dogAnswer As Boolean

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub

    Public Function getText() As Boolean
        dogAnswer = False
        If TextBox1.Text = "Dog" Then
            Return dogAnswer = True
        End If
        Return dogAnswer
    End Function

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If  dogAnswer = True Then
            Label1.Text = "dog"
        Else
            Label1.Text = "Not dog"
        End If
    End Sub
End Class

Upvotes: 0

Views: 506

Answers (3)

Mr Surfy
Mr Surfy

Reputation: 61

Just like to say first that I appreciate you are just playing around and I know how messy code can get when you're just trying to get your head around something new.

There's lots of things wrong here...

Looks to me like you have a module (form) level variable called dogAnswer which is only set to True during a call to function getText. This is a function with a side effect (returns a result and also changes state) which some would say is bad practice. Others would slate me for saying this.

The functions is never called inside your class (form) and unless it is called the value of dogAnswer will remain False (it's initial value - and some would say you should explicitly set the intitial value rather than rely on the default)

So even if TextBox1.Text does contain "Dog", because the function getText is never called, dogAnswer will always be False and Label1.Text is doomed to remain as "Not dog".

But even if you do call it, the function is never going to work, because you are trying to set the value of dogAnswer with the following line...

 Return dogAnswer = True

... this will compare dogAnswer (initialised to False and never changed) with True. If dogAnswer is True then (dogAnswer = True) is will return True, but until this line is executed dogAnswer must be False since no other line in your code sets it's value and it is initialised to False. So dogAnswer will ALWAYS be False

What I think you need on this line is:

dogAnswer = True

... but you would still need to call getText somewhere before dogAnswer will change

Finally, I notice that you're comparing the value of TextBox1.Text with "Dog", but setting Label1.Text to "dog". You probably want a case insensitive comparison, otherwise, if you enter "dog" in TextBox1 the equality test will fail.

I would also say that a function called getText which returns a boolean value is a bit obscure

Here's a fixed version of your code

Public Class Form1
    Dim dogAnswer As Boolean

    Public Sub New()
        ' This call is required by the designer. 
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call. 
    End Sub

    Public Function getText() As Boolean
        dogAnswer = False
        If TextBox1.Text.Equals("Dog", StringComparison.CurrentCultureIgnoreCase) Then
            dogAnswer = True
        End If
        Return dogAnswer
    End Function

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dummy As Boolean

        dummy = getText()
        If dogAnswer = True Then
            Label1.Text = "dog"
        Else
            Label1.Text = "Not dog"
        End If
    End Sub
End Class

... but here's a better way ...

Public Class Form1
    Dim dogAnswer As Boolean

    Public Sub New()
        ' This call is required by the designer. 
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call. 
    End Sub

    Public Sub CheckDogStatus()
        dogAnswer = TextBox1.Text.Equals("Dog", StringComparison.CurrentCultureIgnoreCase)
    End Sub

    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CheckDogStatus()
        If dogAnswer = True Then
            Label1.Text = "dog"
        Else
            Label1.Text = "Not dog"
        End If
    End Sub
End Class

Good Luck!

Upvotes: 0

competent_tech
competent_tech

Reputation: 44931

There are three problems:

1) You never call GetText

2) Even if you did call GetText, it will always return false.

3) You are performing a case-sensitive comparison, so value of dog and DOG, for example, will return false.

Changing your code to be something like the following will get what you are looking for:

Public Function getText() As Boolean
    If TextBox1.Text.Equals("Dog", StringComparison.InvariantCultureIgnoreCase) Then
        dogAnswer = True
    Else
        dogAnswer = False
    End If

    Return dogAnswer
End Function

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If getText() Then
        Label1.Text = "dog"
    Else
        Label1.Text = "Not dog"
    End If
End Sub

Note that getText can eliminated and you don't need the dogAnswer member:

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If TextBox1.Text.Equals("Dog", StringComparison.InvariantCultureIgnoreCase) Then
        Label1.Text = "dog"
    Else
        Label1.Text = "Not dog"
    End If
End Sub

Upvotes: 2

Ric
Ric

Reputation: 13248

Default boolean values are false!

Dim dogAnswer As Boolean

Upvotes: 1

Related Questions