[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
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.
Re: [2005] toString() equiv for an object in VB.net
vb.net Code:
Public Overrides Function ToString() As String
Return _realmname
End Function
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?
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?
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!
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.
Re: [RESOLVED] [2005] toString() equiv for an object in VB.net