locked
VB 2005??? PT 2 RRS feed

  • Question

  • Public Function Plus(ByVal ad As Integer)

    Dim id As Integer

    Dim cm As Integer

     

    End Function

     

    I want to pass this fuction a varible that when i call it in the code i can enter any number and add it to another number any help with that, i know it sounds rather simple but i am not sure if i should pass two varibles or one i am thinking i should pass two and declare a local var and i dont know thats where i draw the blank....any help would be great

    Thursday, April 6, 2006 12:15 AM

Answers

  • For a console application - this should work....

    As for you class Model, I think your overthinking things a little - Objects are meant to try and make things easier and for the math functionality your talking about I think its probably a little over the top for such a trivial task.

     

    Module Module1

        Sub Main()
            Dim obj As New Mathe

            Console.WriteLine("Enter a number")

            obj.Num1 = Console.ReadLine

            Console.WriteLine("Enter second Number")

            obj.Num2 = Console.ReadLine()

     'CAN Use either of the following lines as the Plus function is overloaded
            '       Console.WriteLine(obj.Plus)
            ' Console.WriteLine(obj.Plus(obj.Num1, obj.Num2))

            Console.ReadLine()

     

        End Sub

    End Module


    Class Mathe
        Private sngnum1 As Single
        Private Sngnum2 As Single

        Public Property Num1() As Single
            Get
                Return sngnum1
            End Get
            Set(ByVal value As Single)
                sngnum1 = value
            End Set
        End Property


        Public Property Num2() As Single
            Get
                Return sngnum2
            End Get
            Set(ByVal value As Single)
                sngnum2 = value
            End Set
        End Property


        Public Function Plus() As Single
            Return Me.Num1 + Me.Num2
        End Function

        Public Function Plus(ByVal a As Single, ByVal b As Single) As Single
            Me.Num1 = a
            Me.Num2 = b

            Return Plus()
        End Function
    End Class

    Thursday, April 6, 2006 5:03 PM
  • A number of issues with you code that this should correct.

    • Your property set statements were incorrect.
    • The variables used in the property get/set where not correct.

    For these issues you should be able to put a breakpoint on your code and debug it - looking at the values that are contained in the variables by looking at the Debug locals window

     

    Option Strict Off

    Imports System.Math

    Imports System

    Module Module1

        Sub Main()

            Dim obj As New Mathe
            Console.WriteLine("Enter a number")
            obj.num = Console.ReadLine()
            Console.WriteLine("Enter second Number")
            obj.num1 = Console.ReadLine
            Console.WriteLine(obj.Plus(obj.num, obj.num1))
            Console.ReadLine()

        End Sub

    End Module


    Public Class Mathe

        Private d_num1 As Single
        Private d_num2 As Single
        Private d_sum As Single
        Private IsAdd As Boolean = False
        Private str As String

        Public Property num() As Single
            Get
                Return d_num1
            End Get

            Set(ByVal value As Single)
                d_num1 = value
            End Set
        End Property

        Public Property num1() As single
            Get
                Return d_num2
            End Get

            Set(ByVal value As single)
                d_num2 = value
            End Set
        End Property

        Public ReadOnly Property sum() As single
            Get
                Return d_sum
            End Get
        End Property

        Public Function Plus() As Single
            d_sum = Me.num1 + Me.num
            Return d_sum
        End Function

        Public Function Plus(ByVal ad As single, ByVal da As single) As Single
            Me.num = ad
            Me.num1 = da
            Return Plus()
        End Function

    End Class

     

     

    Thursday, April 6, 2006 10:34 PM

All replies

  • You can do it something like

    Public Function Plus (Byval A as integer, B as integer) as integer

    Return A + B

    End Function

     

    Then just call using

    Dim a as integer = 1

    dim b as integer = 11

    Dim Result as integer = Plus(a, b)

     

    The value in result would be 12

     

     

     

    Thursday, April 6, 2006 12:21 AM
  • I want it sort of like this but i am not getting the right result...

     

    Option Strict On

    Imports System.Math

    Imports System

    Module Module1

    Sub Main()

     

    Dim obj As New Mathe

    Dim show As String

    Console.WriteLine("Enter a number")

    If IsNumeric(obj.num) Then

    Console.WriteLine("Enter a second Number")

    Console.ReadLine()

    Else

    MsgBox("You aint do it right, try again", MsgBoxStyle.Critical)

    obj.var = True

    If IsNumeric(obj.num1) Then

    show = obj.Plus(obj.num, obj.num1).ToString

    Console.WriteLine(show)

    Console.ReadLine()

    End If

    End If

    Thursday, April 6, 2006 12:32 AM
  • Dim obj As New Mathe

     

    Console.WriteLine("Enter a number")

    Console.ReadLine(obj.num).ToString()

    Console.WriteLine("Enter second Number")

    Console.ReadLine(obj.num1).ToString()

    Console.WriteLine(obj.Plus(obj.num, obj.num1))

    Console.ReadLine()

     

    I also tried this way but i dont get the results i need any help out there thanks......

    Thursday, April 6, 2006 1:34 AM
  • This is the class i was working with i eventually want to see if i can build a class of math computations like addtion subtraction division and gaining an average but i cant get the addtion part exactly how i need it to work...i hope this help you help me.....

    Please give me all insight on my OOD is it bad do i have the right idea or what???

     

    Public Class Mathe

    Private d_num1 As Integer

    Private d_num2 As Integer

    Private d_sum As Integer

    Private IsAdd As Boolean = False

    Private str As String

    Public Property num() As Integer

    Get

    Return d_num1

    End Get

    Set(ByVal value As Integer)

    value = num

    End Set

    End Property

    Public Property num1() As Integer

    Get

    Return d_num2

    End Get

    Set(ByVal value As Integer)

    value = num1

    End Set

    End Property

    Public Property sum() As Integer

    Get

    Return d_sum

    End Get

    Set(ByVal value As Integer)

    value = sum

    End Set

    End Property

    Public Property var() As Boolean

    Get

    Return IsAdd

    End Get

    Set(ByVal value As Boolean)

    value = var

    End Set

    End Property

    Public Property sstring() As String

    Get

    Return str

    End Get

    Set(ByVal value As String)

    value = sstring

    End Set

    End Property

    Public Function Plus(ByVal ad As Integer, ByVal da As Integer)

    Return ad + da

    End Function

    Thursday, April 6, 2006 1:36 AM
  • For a console application - this should work....

    As for you class Model, I think your overthinking things a little - Objects are meant to try and make things easier and for the math functionality your talking about I think its probably a little over the top for such a trivial task.

     

    Module Module1

        Sub Main()
            Dim obj As New Mathe

            Console.WriteLine("Enter a number")

            obj.Num1 = Console.ReadLine

            Console.WriteLine("Enter second Number")

            obj.Num2 = Console.ReadLine()

     'CAN Use either of the following lines as the Plus function is overloaded
            '       Console.WriteLine(obj.Plus)
            ' Console.WriteLine(obj.Plus(obj.Num1, obj.Num2))

            Console.ReadLine()

     

        End Sub

    End Module


    Class Mathe
        Private sngnum1 As Single
        Private Sngnum2 As Single

        Public Property Num1() As Single
            Get
                Return sngnum1
            End Get
            Set(ByVal value As Single)
                sngnum1 = value
            End Set
        End Property


        Public Property Num2() As Single
            Get
                Return sngnum2
            End Get
            Set(ByVal value As Single)
                sngnum2 = value
            End Set
        End Property


        Public Function Plus() As Single
            Return Me.Num1 + Me.Num2
        End Function

        Public Function Plus(ByVal a As Single, ByVal b As Single) As Single
            Me.Num1 = a
            Me.Num2 = b

            Return Plus()
        End Function
    End Class

    Thursday, April 6, 2006 5:03 PM
  • obj.num1 = Console.Readline.....It is drawing an error saying option strict on does not conversion from int to string...should i turn it off???
    Thursday, April 6, 2006 9:12 PM
  • If you've got option strict on then you are going to need to do type conversions

     

    Obj1.num1 = Ctype(Console.Readline, Single)

    or you can turn option strict off - in which case conversion of string to single will be attempted.

     

    Thursday, April 6, 2006 9:25 PM
  • This is what i have right now but i am not getting any result but zero......check it out....good look spotty

    here is the class.......

    Public Class Mathe

    Private d_num1 As Integer

    Private d_num2 As Integer

    Private d_sum As Integer

    Private IsAdd As Boolean = False

    Private str As String

    Public Property num() As Integer

    Get

    Return d_num1

    End Get

    Set(ByVal value As Integer)

    value = num

    End Set

    End Property

    Public Property num1() As Integer

    Get

    Return d_num2

    End Get

    Set(ByVal value As Integer)

    value = num1

    End Set

    End Property

    Public Property sum() As Integer

    Get

    Return d_sum

    End Get

    Set(ByVal value As Integer)

    value = sum

    End Set

    End Property

    Public Property var() As Boolean

    Get

    Return IsAdd

    End Get

    Set(ByVal value As Boolean)

    value = var

    End Set

    End Property

    Public Property sstring() As String

    Get

    Return str

    End Get

    Set(ByVal value As String)

    value = sstring

    End Set

    End Property

    Public Function Plus() As Single

    Return Me.num1 + Me.num

    End Function

    Public Function Plus(ByVal ad As Integer, ByVal da As Integer) As Single

    Me.num = ad

    Me.num1 = da

    Return Plus()

    End Function

    End Class

     

    and here is the client

     

    Option Strict Off

    Imports System.Math

    Imports System

    Module Module1

    Sub Main()

     Dim obj As New Mathe

     Console.WriteLine("Enter a number")

    obj.num = Console.ReadLine()

     Console.WriteLine("Enter second Number")

    obj.num1 = Console.ReadLine

    Console.WriteLine(obj.Plus(obj.num, obj.num1))

    Console.ReadLine()

    End Sub

    End Module

    Thursday, April 6, 2006 9:34 PM
  • A number of issues with you code that this should correct.

    • Your property set statements were incorrect.
    • The variables used in the property get/set where not correct.

    For these issues you should be able to put a breakpoint on your code and debug it - looking at the values that are contained in the variables by looking at the Debug locals window

     

    Option Strict Off

    Imports System.Math

    Imports System

    Module Module1

        Sub Main()

            Dim obj As New Mathe
            Console.WriteLine("Enter a number")
            obj.num = Console.ReadLine()
            Console.WriteLine("Enter second Number")
            obj.num1 = Console.ReadLine
            Console.WriteLine(obj.Plus(obj.num, obj.num1))
            Console.ReadLine()

        End Sub

    End Module


    Public Class Mathe

        Private d_num1 As Single
        Private d_num2 As Single
        Private d_sum As Single
        Private IsAdd As Boolean = False
        Private str As String

        Public Property num() As Single
            Get
                Return d_num1
            End Get

            Set(ByVal value As Single)
                d_num1 = value
            End Set
        End Property

        Public Property num1() As single
            Get
                Return d_num2
            End Get

            Set(ByVal value As single)
                d_num2 = value
            End Set
        End Property

        Public ReadOnly Property sum() As single
            Get
                Return d_sum
            End Get
        End Property

        Public Function Plus() As Single
            d_sum = Me.num1 + Me.num
            Return d_sum
        End Function

        Public Function Plus(ByVal ad As single, ByVal da As single) As Single
            Me.num = ad
            Me.num1 = da
            Return Plus()
        End Function

    End Class

     

     

    Thursday, April 6, 2006 10:34 PM