Results 1 to 8 of 8

Thread: [RESOLVED] [2005] toString() equiv for an object in VB.net

  1. #1

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Resolved [RESOLVED] [2005] toString() equiv for an object in VB.net

    Hi there,

    I have the following class

    Code:
    Public Class Server
        Private _realmname As String
        Private _isEU As Boolean
        Private _isUS As Boolean
        Private _isUp As Boolean
        Private _type As String
        Private _population As String
    
        ' Method to set/get the realm name, eg Stormrage
        Public Property RealmName()
            Get
                Return _realmname
            End Get
            Set(ByVal value)
                _realmname = value
            End Set
        End Property
    
        ' Method to set/get whether the realm is hosted in Europe
        Public Property isEU()
            Get
                Return _isEU
            End Get
            Set(ByVal value)
                _isEU = value
                _isUS = Not value
            End Set
        End Property
    
        ' Method to set/get whether the realm is hosted in the US
        Public Property isUp()
            Get
                Return _isUp
            End Get
            Set(ByVal value)
                _isUp = value
            End Set
        End Property
    
        ' Method to set/get what kind of realm this is, eg PvP, RP, Normal, RPPVP
        Public Property Type()
            Get
                Return _type
            End Get
            Set(ByVal value)
                _type = value
            End Set
        End Property
    
        ' Method to set/get the population load on the server, eg New, Full, Medium
        Public Property Population()
            Get
                Return _population
            End Get
            Set(ByVal value)
                _population = value
            End Set
        End Property
    End Class
    When I add instances of this class to a ListBox, I'd like the ListBox to print the "_realmname" attribute, sort of like the way you have the toString() function for Java (iirc).
    Example, I have a listbox named ListBox_AllServers, now when I add a Server instance with _realmname set to "Stormrage", I'd like the following code

    Code:
    Dim StormrageServerObj as Server
    StormrageServerObj.RealmName = "Stormrage"
    ListBox_AllServers.Items.Add(StormrageServerObj)
    To result in the name "Stormrage" appearing in the listbox.

    What function do I need to implement in my Server class to make this happen?

    Thanks in advance.
    Mightor

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: [2005] toString() equiv for an object in VB.net

    Just like in Java. Override the ToString method in your object and make it return whatever you want.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] toString() equiv for an object in VB.net

    vb.net Code:
    1. Public Overrides Function ToString() As String
    2.     Return _realmname
    3. End Function
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  4. #4

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] toString() equiv for an object in VB.net

    Quote Originally Posted by crptcblade
    Just like in Java. Override the ToString method in your object and make it return whatever you want.
    Heh, too obivous :P Thanks it works a treat now!
    Code:
        Public Overrides Function ToString() As String
            Return _realmname
        End Function
    That seems correct, right?

  5. #5
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] toString() equiv for an object in VB.net

    If that is the value that you want to return, then yes.

    Just a side note, why are you not declaring your property types? For instance if RealmName is always going to be a string, why not declare it as such?
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  6. #6

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] toString() equiv for an object in VB.net

    Thanks peeps! Seems next time I should hit refresh a few more times before posting a final post! It seems whenever I think something is going to be complicated in VB.net, the forums prove me wrong!

  7. #7

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] toString() equiv for an object in VB.net

    Quote Originally Posted by bmahler
    Just a side note, why are you not declaring your property types? For instance if RealmName is always going to be a string, why not declare it as such?
    A small oversight. Thanks for pointing it out. I will amend my code.

  8. #8
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [RESOLVED] [2005] toString() equiv for an object in VB.net

    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width