Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overriding ToString 1

Status
Not open for further replies.

bmgmzp

Programmer
Aug 18, 2006
84
US
Say I have a boolean variable named RecurseSubFolders... how can I override the ToString method of this boolean variable?

If the boolean is True, I want the ToString to return -r
If the boolean is False, I want the ToString to return ""

Any ideas?
 
To do this, you need to create a wrapper around the class and use that instead.

An easier way would be to create a new class that inherits from the bool class but the bool class is sealed meaning not inheritable.

Greetz,

Geert


Geert Verhoeven
Consultant @ Ausy Belgium

My Personal Blog
 
I was going to suggest inheriting Boolean, but soon realised that Boolean is a data type not a class its self, and data types cannot be inherited or overriden.

You could create your own class that has simular properties and functions as the Boolean data type, but you couldn't use MyObject.MyBoolean = True, because the runtime engine wouldn't know if you wanted to change the MyBoolean object to True rather than giving your MyBoolean class the True enum. You could have a Value property: MyObject.MyBoolean.Value = True & MyObject.MyBoolean.ToString...

Unless you can put up with coding like that, I don't think that this can be done.
 
1. You can create a string function that will accept a boolean type and will return those you want.


2. This is a quick made. There must be a better way, but this also works.

Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim s As New RecurseSubFolders

        s.RecurseSubFolders = True
        MsgBox(s.ToString)

    End Sub

[COLOR=blue]
Public Class RecurseSubFolders

    Private RecurseSubFolders_ As Boolean


    Public Overrides Function ToString() As String
        If RecurseSubFolders_ = True Then
            Return ("-r")
        Else
            Return (String.Empty)
        End If
    End Function


    Public Property RecurseSubFolders() As Boolean
        Get
            Return (RecurseSubFolders_)
        End Get
        Set(ByVal Value As Boolean)
            RecurseSubFolders_ = Value
        End Set
    End Property

End Class[/color]
 
3. Using a structure instead of a class:

Code:
    Structure RecurseSubFolders

        Dim value_ As Boolean

        Property value() As Boolean
            Get
                Return (value_)
            End Get
            Set(ByVal Value As Boolean)
                value_ = Value
            End Set
        End Property

        Shadows Function ToString() As String
            If (value_ = True) Then
                Return ("-r")
            Else
                Return (String.Empty)
            End If
        End Function

    End Structure
 
Thanks TipGiver! Why should I use a class over a structure or vice versa?
 
Hi.

Not "over". Either the 1st, the 2nd or the 3rd solution provided. Your choice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top