Solved

class bave a blank property

Posted on 2006-07-21
1
170 Views
Last Modified: 2010-04-23
I have a class called status which is pretty much just a status boolean, and a message.

So I can return, false, and "Failed for this reason"

Is there a way to return a value when you reference the object, without a property?


        Dim saveTestResults As Status
        MsgBox(saveTestResults.status) 'would pop up true or false

        MsgBox(saveTestResults) 'I would like this to also return true or false
        if (saveTestResults) then 'this is what I really want to do
        end if

Public Class Status

    Private myStatus As Boolean = False
    Private myMessage As String = ""

    Public Property status() As Boolean
        Get
            status = myStatus
        End Get
        Set(ByVal value As Boolean)
            myStatus = value
        End Set
    End Property

    Public Property message() As String
        Get
            message = myMessage
        End Get
        Set(ByVal value As String)
            myMessage = value
        End Set
    End Property

    Public Sub setStatus(ByVal message$, ByVal status As Boolean)

        myStatus = status
        myMessage = message

    End Sub

End Class
0
Comment
Question by:jackjohnson44
1 Comment
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 17155065
This is possibly a little crude, but you could achieve the effect you want by overriding the = and <> operators (in v2.0 at least):

Public Class Status

    Private myStatus As Boolean = False
    Private myMessage As String = ""

    Public Property status() As Boolean
        Get
            status = myStatus
        End Get
        Set(ByVal value As Boolean)
            myStatus = value
        End Set
    End Property

    Public Property message() As String
        Get
            message = myMessage
        End Get
        Set(ByVal value As String)
            myMessage = value
        End Set
    End Property

    Public Sub setStatus(ByVal message$, ByVal status As Boolean)

        myStatus = status
        myMessage = message

    End Sub

    Public Shared Operator =(ByVal lhs As Status, ByVal rhs As Object) As Boolean
        Return lhs.status
    End Operator

    Public Shared Operator <>(ByVal lhs As Status, ByVal rhs As Object) As Boolean
        Return Not lhs.status
    End Operator

End Class

Then use it like:

        Dim o As New Status
        o.status = True

        If o = True Then
            MessageBox.Show("Its True")
        End If

You still need to explicitly check True or False, because you can't just make a property Default unless it has an index otherwise you cause an ambiguity with the assignment operator.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now