locked
Passing Variables RRS feed

  • Question

  •  

    I'm trying to be a good oop programmer by making small subroutines and passing variables instead of declaring every variable as Public but I'm struggling with how to pass variables as you can see in the following:

     

    Public Class Form1

     

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

    Dim FirstNbr As Integer = 0

    Dim SecondNbr As Integer = 0

    FirstNbr = CInt(TextBox1.Text)

    SecondNbr = CInt(TextBox2.Text)

    Call DoSomething(FirstNbr, SecondNbr)

    TextBox3.Text = answer

    Call DoAnything(FirstNbr, SecondNbr)

    TextBox4.Text = theanswer

    End Sub

     

    Private Sub DoSomething(ByVal firstnbr, ByVal secondnbr)

    Dim Answer As Integer = 0

    Answer = firstnbr * secondnbr

    MessageBox.Show(Answer)

    End Sub

     

    Private Function DoAnything(ByVal Firstnbr, ByVal Secondnbr)

    Dim TheAnswer As Integer = 0

    TheAnswer = Firstnbr * Secondnbr

    MessageBox.Show(TheAnswer)

    Return TheAnswer

    End Function

     

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    End

    End Sub

     

    End Class

     

    Can somebody set me on the right path?

     

    Thanks.

    Sunday, August 19, 2007 7:42 PM

Answers

  • Hi,

     

    Below are the changes

    highlighted in light blue.

     

    Comments are in this green.

     

    I have left the Sub and the Function as handling INTEGER values only.

     

     

     

    Regards,

     

    S_DS

    ______________________________________________________________

     

     

    Public Class Form1

     

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

     

    ' I have made the values as a default value of 1 

    'so that if you miss type one number then you will still get 

    ' 1 * something = something. 

    Dim FirstNbr As Integer = 1

    Dim SecondNbr As Integer = 1

     

    ' Is it a Number?

    If IsNumeric(TextBox1.Text) Then FirstNbr = CInt(TextBox1.Text)

     

    'Is it a number?

    If IsNumeric(TextBox2.Text) Then SecondNbr = CInt(TextBox2.Text)

     

    'Just calls the SUB.

    Call DoSomething(FirstNbr, SecondNbr)

     

    'Calls the function, returns the result and converts

    'it to a STRING for the TextBox.

    TextBox4.Text = (DoAnthing(FirstNbr,SecondNbr)).ToString

     

    End Sub

     

    Private Sub DoSomething(ByVal firstnbr As Integer, ByVal secondnbr As Integer)

    Dim Answer As Integer = 0

    Answer = firstnbr * secondnbr

    MessageBox.Show(Answer.ToString)

    End Sub

     

    Private Function DoAnything(ByVal Firstnbr As Integer, ByVal Secondnbr As Integer ) As Integer

     

    Return Firstnbr * Secondnbr

     

    End Function

     

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

     

    Application.Exit()

     

     'Is better than END for multi-FORM applications and is the 

     'preferred method ( alledgedly ) according to what I have read in these forums.

     

    End Sub

     

    End Class

     

    Sunday, August 19, 2007 8:26 PM
  • Well you're missing out on a few things here.  In the case of your method DoSomething(), its a sub routine so you don't return a value.  Therefore, you don't have a value of your variable answer (in the case of TextBox3.Text = answer ).

    As well, the "Call" keyword is optional in VB.NET.

    This leads to your other method DoAnything() which is a function and SHOULD return a value.  Now when functions return a value, it returns a value of the return datatype specified in the Function Header (in your case, you're missing that as well)

    So, your function ought to look a bit like the following:

    Private Function DoAnything(ByVal Firstnbr, ByVal Secondnbr) As Integer

    Dim TheAnswer As Integer = 0

    TheAnswer = Firstnbr * Secondnbr

    MessageBox.Show(TheAnswer)

    Return TheAnswer

    End Function


    and calling the function should be in the form of:


    TextBox4.Text = CStr(DoAnything(FirstNbr, SecondNbr))

    notice the conversion.  DoAnything returned an integer, but TextBox4's property Text accepts strings.  It is more correct to convert the value to a string before assigning it the value.  As you have already done previously by converting the string values to integers by use of the CInt().



    Back to the Sub routine.  You need to understand Angus, that a variable does not necessarily have to be public for its value to be visible between methods.  A private variable would work just as well, so long as it was defined in a scope that is more public than that of the Method, basically outside of the method.


    Assume the following:


    Private answer as
    Integer = 0

    Public Sub DoSomething(ByVal val1 as Integer, ByVal val2 as Integer)

    answer = val1 + val2

    End Sub



    In this scenario, answer will still have the value of whatever calculation occured in DoSomething().


    So, a revised version of your code would be as follows:


    Public Class Form1

     

    Private _answer As Integer = 0


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

    Dim FirstNbr As Integer = 0

    Dim SecondNbr As Integer = 0


    FirstNbr = CInt(TextBox1.Text)

    SecondNbr = CInt(TextBox2.Text)


    DoSomething(FirstNbr, SecondNbr)

    TextBox3.Text = _answer


    TextBox4.Text = Do Anything(FirstNbr,  SecondNbr)


    End Sub

     

    Private Sub DoSomething(ByVal firstnbr, ByVal secondnbr)

    _answer = firstnbr * secondnbr

    MessageBox.Show(_answer)

    End Sub

     

    Private Function DoAnything(ByVal Firstnbr, ByVal Secondnbr)

    Dim TheAnswer As Integer = 0

    TheAnswer = Firstnbr * Secondnbr

    MessageBox.Show(TheAnswer)

    Return TheAnswer

    End Function

     

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    End Sub

     

    End Class


    One more note.  You might want to put that private variable _answer in a Property as so

    Public Property Answer() as Integer
    Get
    return _answer
    End Get
    Set
    _answer = value
    End Set
    End Property

    Properties is one of the better OOP ways of coding, because you can centralize the control of HOW and WHAT gets assigned to your variable.  You can set it to be ReadOnly for example or you can do checks on the value being assigned and so on.


    I hope this helped


    Mustafa Ismail







    Sunday, August 19, 2007 8:43 PM
  •  Mustafa Ismail wrote:
    Well you're missing out on a few things here.  In the case of your method DoSomething(), its a sub routine so you don't return a value.  Therefore, you don't have a value of your variable answer (in the case of TextBox3.Text = answer ).

    As well, the "Call" keyword is optional in VB.NET.

    This leads to your other method DoAnything() which is a function and SHOULD return a value.  Now when functions return a value, it returns a value of the return datatype specified in the Function Header (in your case, you're missing that as well)

    So, your function ought to look a bit like the following:

     

     


    Code Snippet


    Private Function DoAnything(ByVal Firstnbr, ByVal Secondnbr) As Integer

    Dim TheAnswer As Integer = 0

    TheAnswer = Firstnbr * Secondnbr

    MessageBox.Show(TheAnswer)

    Return TheAnswer

    End Function


     

     

    and calling the function should be in the form of:


    TextBox4.Text = CStr(DoAnything(FirstNbr, SecondNbr))

    notice the conversion.  DoAnything returned an integer, but TextBox4's property Text accepts strings.  It is more correct to convert the value to a string before assigning it the value.  As you have already done previously by converting the string values to integers by use of the CInt().



    Back to the Sub routine.  You need to understand Angus, that a variable does not necessarily have to be public for its value to be visible between methods.  A private variable would work just as well, so long as it was defined in a scope that is more public than that of the Method, basically outside of the method.


    Assume the following:

     

     

    Code Snippet


    Private answer as Integer = 0

    Public Sub DoSomething(ByVal val1 as Integer, ByVal val2 as Integer)

    answer = val1 + val2

    End Sub



     

     

     

    In this scenario, answer will still have the value of whatever calculation occured in DoSomething().


    So, a revised version of your code would be as follows:

    Code Snippet


    Public Class Form1

     

    Private _answer As Integer = 0


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

    Dim FirstNbr As Integer = 0

    Dim SecondNbr As Integer = 0


    FirstNbr = CInt(TextBox1.Text)

    SecondNbr = CInt(TextBox2.Text)


    DoSomething(FirstNbr, SecondNbr)

    TextBox3.Text = _answer


    TextBox4.Text = Do Anything(FirstNbr,  SecondNbr)


    End Sub

     

    Private Sub DoSomething(ByVal firstnbr, ByVal secondnbr)

    _answer = firstnbr * secondnbr

    MessageBox.Show(_answer)

    End Sub

     

    Private Function DoAnything(ByVal Firstnbr, ByVal Secondnbr)

    Dim TheAnswer As Integer = 0

    TheAnswer = Firstnbr * Secondnbr

    MessageBox.Show(TheAnswer)

    Return TheAnswer

    End Function

     

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

     

    End Sub

     

    End Class


     

     

     

    One more note.  You might want to put that private variable _answer in a Property as so

     

     


    Code Snippet


    Public Property Answer() as Integer

    Get
    return _answer
    End Get
    Set
    _answer = value
    End Set

    End Property

     

     

    Properties is one of the better OOP ways of coding, because you can centralize the control of HOW and WHAT gets assigned to your variable.  You can set it to be ReadOnly for example or you can do checks on the value being assigned and so on.


    I hope this helped


    Mustafa Ismail

     

     

    Hi Mustafa,

     

    Some of that was very hard to read.

     

    So I am posting your post again with larger text.

     

     

     

    Regards,

     

    S_DS

     

    Monday, August 20, 2007 10:29 PM

All replies

  • Hi,

     

    Below are the changes

    highlighted in light blue.

     

    Comments are in this green.

     

    I have left the Sub and the Function as handling INTEGER values only.

     

     

     

    Regards,

     

    S_DS

    ______________________________________________________________

     

     

    Public Class Form1

     

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

     

    ' I have made the values as a default value of 1 

    'so that if you miss type one number then you will still get 

    ' 1 * something = something. 

    Dim FirstNbr As Integer = 1

    Dim SecondNbr As Integer = 1

     

    ' Is it a Number?

    If IsNumeric(TextBox1.Text) Then FirstNbr = CInt(TextBox1.Text)

     

    'Is it a number?

    If IsNumeric(TextBox2.Text) Then SecondNbr = CInt(TextBox2.Text)

     

    'Just calls the SUB.

    Call DoSomething(FirstNbr, SecondNbr)

     

    'Calls the function, returns the result and converts

    'it to a STRING for the TextBox.

    TextBox4.Text = (DoAnthing(FirstNbr,SecondNbr)).ToString

     

    End Sub

     

    Private Sub DoSomething(ByVal firstnbr As Integer, ByVal secondnbr As Integer)

    Dim Answer As Integer = 0

    Answer = firstnbr * secondnbr

    MessageBox.Show(Answer.ToString)

    End Sub

     

    Private Function DoAnything(ByVal Firstnbr As Integer, ByVal Secondnbr As Integer ) As Integer

     

    Return Firstnbr * Secondnbr

     

    End Function

     

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

     

    Application.Exit()

     

     'Is better than END for multi-FORM applications and is the 

     'preferred method ( alledgedly ) according to what I have read in these forums.

     

    End Sub

     

    End Class

     

    Sunday, August 19, 2007 8:26 PM
  • Well you're missing out on a few things here.  In the case of your method DoSomething(), its a sub routine so you don't return a value.  Therefore, you don't have a value of your variable answer (in the case of TextBox3.Text = answer ).

    As well, the "Call" keyword is optional in VB.NET.

    This leads to your other method DoAnything() which is a function and SHOULD return a value.  Now when functions return a value, it returns a value of the return datatype specified in the Function Header (in your case, you're missing that as well)

    So, your function ought to look a bit like the following:

    Private Function DoAnything(ByVal Firstnbr, ByVal Secondnbr) As Integer

    Dim TheAnswer As Integer = 0

    TheAnswer = Firstnbr * Secondnbr

    MessageBox.Show(TheAnswer)

    Return TheAnswer

    End Function


    and calling the function should be in the form of:


    TextBox4.Text = CStr(DoAnything(FirstNbr, SecondNbr))

    notice the conversion.  DoAnything returned an integer, but TextBox4's property Text accepts strings.  It is more correct to convert the value to a string before assigning it the value.  As you have already done previously by converting the string values to integers by use of the CInt().



    Back to the Sub routine.  You need to understand Angus, that a variable does not necessarily have to be public for its value to be visible between methods.  A private variable would work just as well, so long as it was defined in a scope that is more public than that of the Method, basically outside of the method.


    Assume the following:


    Private answer as
    Integer = 0

    Public Sub DoSomething(ByVal val1 as Integer, ByVal val2 as Integer)

    answer = val1 + val2

    End Sub



    In this scenario, answer will still have the value of whatever calculation occured in DoSomething().


    So, a revised version of your code would be as follows:


    Public Class Form1

     

    Private _answer As Integer = 0


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

    Dim FirstNbr As Integer = 0

    Dim SecondNbr As Integer = 0


    FirstNbr = CInt(TextBox1.Text)

    SecondNbr = CInt(TextBox2.Text)


    DoSomething(FirstNbr, SecondNbr)

    TextBox3.Text = _answer


    TextBox4.Text = Do Anything(FirstNbr,  SecondNbr)


    End Sub

     

    Private Sub DoSomething(ByVal firstnbr, ByVal secondnbr)

    _answer = firstnbr * secondnbr

    MessageBox.Show(_answer)

    End Sub

     

    Private Function DoAnything(ByVal Firstnbr, ByVal Secondnbr)

    Dim TheAnswer As Integer = 0

    TheAnswer = Firstnbr * Secondnbr

    MessageBox.Show(TheAnswer)

    Return TheAnswer

    End Function

     

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    End Sub

     

    End Class


    One more note.  You might want to put that private variable _answer in a Property as so

    Public Property Answer() as Integer
    Get
    return _answer
    End Get
    Set
    _answer = value
    End Set
    End Property

    Properties is one of the better OOP ways of coding, because you can centralize the control of HOW and WHAT gets assigned to your variable.  You can set it to be ReadOnly for example or you can do checks on the value being assigned and so on.


    I hope this helped


    Mustafa Ismail







    Sunday, August 19, 2007 8:43 PM
  •  Mustafa Ismail wrote:
    Well you're missing out on a few things here.  In the case of your method DoSomething(), its a sub routine so you don't return a value.  Therefore, you don't have a value of your variable answer (in the case of TextBox3.Text = answer ).

    As well, the "Call" keyword is optional in VB.NET.

    This leads to your other method DoAnything() which is a function and SHOULD return a value.  Now when functions return a value, it returns a value of the return datatype specified in the Function Header (in your case, you're missing that as well)

    So, your function ought to look a bit like the following:

     

     


    Code Snippet


    Private Function DoAnything(ByVal Firstnbr, ByVal Secondnbr) As Integer

    Dim TheAnswer As Integer = 0

    TheAnswer = Firstnbr * Secondnbr

    MessageBox.Show(TheAnswer)

    Return TheAnswer

    End Function


     

     

    and calling the function should be in the form of:


    TextBox4.Text = CStr(DoAnything(FirstNbr, SecondNbr))

    notice the conversion.  DoAnything returned an integer, but TextBox4's property Text accepts strings.  It is more correct to convert the value to a string before assigning it the value.  As you have already done previously by converting the string values to integers by use of the CInt().



    Back to the Sub routine.  You need to understand Angus, that a variable does not necessarily have to be public for its value to be visible between methods.  A private variable would work just as well, so long as it was defined in a scope that is more public than that of the Method, basically outside of the method.


    Assume the following:

     

     

    Code Snippet


    Private answer as Integer = 0

    Public Sub DoSomething(ByVal val1 as Integer, ByVal val2 as Integer)

    answer = val1 + val2

    End Sub



     

     

     

    In this scenario, answer will still have the value of whatever calculation occured in DoSomething().


    So, a revised version of your code would be as follows:

    Code Snippet


    Public Class Form1

     

    Private _answer As Integer = 0


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

    Dim FirstNbr As Integer = 0

    Dim SecondNbr As Integer = 0


    FirstNbr = CInt(TextBox1.Text)

    SecondNbr = CInt(TextBox2.Text)


    DoSomething(FirstNbr, SecondNbr)

    TextBox3.Text = _answer


    TextBox4.Text = Do Anything(FirstNbr,  SecondNbr)


    End Sub

     

    Private Sub DoSomething(ByVal firstnbr, ByVal secondnbr)

    _answer = firstnbr * secondnbr

    MessageBox.Show(_answer)

    End Sub

     

    Private Function DoAnything(ByVal Firstnbr, ByVal Secondnbr)

    Dim TheAnswer As Integer = 0

    TheAnswer = Firstnbr * Secondnbr

    MessageBox.Show(TheAnswer)

    Return TheAnswer

    End Function

     

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

     

    End Sub

     

    End Class


     

     

     

    One more note.  You might want to put that private variable _answer in a Property as so

     

     


    Code Snippet


    Public Property Answer() as Integer

    Get
    return _answer
    End Get
    Set
    _answer = value
    End Set

    End Property

     

     

    Properties is one of the better OOP ways of coding, because you can centralize the control of HOW and WHAT gets assigned to your variable.  You can set it to be ReadOnly for example or you can do checks on the value being assigned and so on.


    I hope this helped


    Mustafa Ismail

     

     

    Hi Mustafa,

     

    Some of that was very hard to read.

     

    So I am posting your post again with larger text.

     

     

     

    Regards,

     

    S_DS

     

    Monday, August 20, 2007 10:29 PM
  • All three replies were very helpful.  I'm not sure about the properties thing but I'll do some reading and see if I can get there.  I have to pick up on this stuff a little at a time.

     

    Thanks again.

    Tuesday, August 21, 2007 12:19 AM