|
-
Apr 23rd, 2007, 02:17 PM
#1
Thread Starter
Lively Member
[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
-
Apr 23rd, 2007, 02:36 PM
#2
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
-
Apr 23rd, 2007, 02:36 PM
#3
Re: [2005] toString() equiv for an object in VB.net
vb.net Code:
Public Overrides Function ToString() As String
Return _realmname
End Function
-
Apr 23rd, 2007, 02:41 PM
#4
Thread Starter
Lively Member
Re: [2005] toString() equiv for an object in VB.net
 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?
-
Apr 23rd, 2007, 02:43 PM
#5
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?
-
Apr 23rd, 2007, 02:43 PM
#6
Thread Starter
Lively Member
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!
-
Apr 23rd, 2007, 02:49 PM
#7
Thread Starter
Lively Member
Re: [2005] toString() equiv for an object in VB.net
 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.
-
Apr 23rd, 2007, 02:52 PM
#8
Re: [RESOLVED] [2005] toString() equiv for an object in VB.net
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|