Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I would like to create a class/method that allows me to check if the value is within limits. I cannot figure out how to define the class/method for this. Here is some pseudo code:
VB
Class T
  Public a as integer
  Public b as integer
  Public c as integer

  Function check as Boolean
    return value>0 
  End Function
End Class

Use above like:
VB
dim myT as new T
  myT.a=9
  debug.writeline (myT.a.check)
  myT.b=-1
  debug.writeline (myT.b.check)
  myT.c=1
  debug.writeline (myT.c.check)

Display should be:
true
false
true

Please help me. Thank you
Posted
Updated 11-Nov-14 6:40am
v4

Hi there, here are two examples that may provide you an answer. The second Class T2 uses an extension method for type Integer as mentioned before this post. The first Class example uses a custom class to hold the integer value and supplies the functionality to check the value.

Imports System.Runtime.CompilerServices

Module Module1
    'This is my custom class to hold and check integer value
    Friend Class T
        Property A As New myInteger
        Property b As New myInteger
        Property c As New myInteger

        Friend Class myInteger
            Private _value As Integer
            Public Property Value() As Integer
                Get
                    Return _value
                End Get
                Set(ByVal value As Integer)
                    _value = value
                End Set
            End Property
            Friend Function Check() As Boolean
                Return CBool(Value > 0)
            End Function
        End Class
    End Class

    'This is your class from pseudo code.
    Friend Class T2
        Property A As Integer
        Property B As Integer
        Property C As Integer
    End Class

   'Run Code here
    Sub Main()

        Debug.WriteLine("Testing Class T")
        Dim myT As New T
        myT.A.Value = 9
        Debug.WriteLine(myT.A.Check)
        myT.b.Value = -1
        Debug.WriteLine(myT.b.Check)
        myT.c.Value = 1
        Debug.WriteLine(myT.c.Check)

        Debug.WriteLine("Testing Class T2")
        Dim myT2 As New T2
        myT2.A = 9
        Debug.WriteLine(myT2.A.ExtCheck)
        myT2.B = -1
        Debug.WriteLine(myT2.B.ExtCheck)
        myT2.C = 1
        Debug.WriteLine(myT2.C.ExtCheck)
    End Sub
    'Needed Extension method
    <Extension()>
    Public Function ExtCheck(ByVal value As Integer) As Boolean
        Return CBool(value > 0)
    End Function

End Module


I hope that helps.
 
Share this answer
 
Comments
Member 11183408 12-Nov-14 15:55pm    
Thank you so much! That is what I was looking for.
You don't need to have a function inside your class. You can have a function outside your class, like this:
VB.NET
Function Check(valueToCheck As Integer) As Boolean ' put this method outside your class
	Return valueToCheck > 0
End Function

Use the above method like this:
VB.NET
dim myT as new T
myT.a=9
debug.writeline (Check(myT.a))
myT.a=-1
debug.writeline (Check(myT.a))
myT.a=0
debug.writeline (Check(myT.a))

You can define the Check method in your class (and then you have to all it like myT.Check(myT.a)), but there is not really a reason for doing that (unless you really need it for a purpose, but there doesn't come one to my mind immediately).
 
Share this answer
 
v5
Comments
Member 11183408 11-Nov-14 12:53pm    
Thank you for your reply. I'm new to classes and was hoping to define the method in the class.

I know how to do it otherwise.
Thomas Daniels 11-Nov-14 13:14pm    
You can define it in a class, and then you have to call it like myT.Check(myT.a), but there is not really a reason for doing that (unless you really need it for a purpose, but there doesn't come one to my mind immediately). I'll add that to my answer.
Member 11183408 11-Nov-14 13:22pm    
Thank you. I just need to figure what benefit methods really have in classes.
Thomas Daniels 11-Nov-14 13:24pm    
They have benefits if they access variables defined in the same class.
Member 11183408 11-Nov-14 13:51pm    
For example. I would like to program a custom method (upper case) that would return the upper case string for any of the variables defined in my class. How would this work?

PS. I know that VB has upper case functions. This is just an example

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900