|
-
May 2nd, 2007, 01:02 AM
#1
Thread Starter
Lively Member
[RESOLVED] [2005] ToString() not working for custom class
Hi there,
I have created a custom class called XmlSource which has a ToString() function .
vb Code:
Public Class XmlSource
Inherits System.Net.WebClient
Private _name As String
Private _uri As Uri
Private _xmldoc As System.Xml.XmlDocument
Public Sub New(ByVal name As String, ByVal uri As Uri)
MyBase.New()
_name = name
_uri = uri
End Sub
Public Sub New(ByVal name As String, ByVal uri As String)
MyBase.New()
_name = name
_uri = New Uri(uri)
End Sub
Public Overrides Function ToString() As String
Return _name
End Function
Public ReadOnly Property XmlDoc() As System.Xml.XmlDataDocument
Get
Return _xmldoc
End Get
End Property
Public Property Uri() As Uri
Get
Return _uri
End Get
Set(ByVal value As Uri)
_uri = value
End Set
End Property
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Protected Overrides Sub OnDownloadStringCompleted(ByVal e As System.Net.DownloadStringCompletedEventArgs)
'If the string request went as planned and wasn't cancelled:
If e.Cancelled = False AndAlso e.Error Is Nothing Then
'Use e.Result to get the String
Dim sr As New System.IO.StringReader(CStr(e.Result))
' The XmlTextReader
Dim xr As New System.Xml.XmlTextReader(sr)
' The XmlDocument
_xmldoc = New System.Xml.XmlDocument
_xmldoc.Load(xr)
End If
MyBase.OnDownloadStringCompleted(e)
End Sub
Public Sub fetchXML()
'Fetch HTML page
MyBase.DownloadStringAsync(_uri)
End Sub
End Class
In my MainForm_Load sub I am attempting to create a new instance and use it as follows:
vb Code:
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sourceUS As New XmlSource("US", "http://www.worldofwarcraft.com//realmstatus/status.xml")
MsgBox(sourceUS)
ComboBox_Continent.Items.Add(sourceUS)
The ComboBox_Continent.Items.Add() function doesn't complain but shows nothing in the control either. The MsgBox() does complain (I Added it for debug purposes). It tells me that Argument 'Prompt' cannot be converted to type 'String'. I was under the impression that was what the ToString() function was for.
Does anyone have any pointers?
Thanks,
Mightor
Last edited by mightor; May 2nd, 2007 at 01:08 AM.
Reason: fixed typo
What the world needs is more geniuses with humility, there are so few of us left.
If my post was accidentally useful, please rate it as such, thanks!
Mon aéroglisseur est plein des anguilles.
-
May 2nd, 2007, 01:26 AM
#2
Re: [2005] ToString() not working for custom class
Objects don't automatically converted to Strings. Message boxes require you to provide a String to display, plain and simple. Why would you want to display anything else in a MessageBox? The ComboBox is the same. When you add an object it isn't converted to a String. It's still the same type as it was so when you get an item from the ComboBox it will be that type. The ToString method is invoked to get a string representation to display in the UI, but the underlying object is still there in its entirety.
As for your issue, I can't see anything obvious. Have you placed a breakpoint on the ToString method to see that it's being executed and what value its returning? Debugging does not just consist of looking at code and hoping to see a problem. It involves observing your code in action and testing what values are being used.
-
May 2nd, 2007, 03:13 AM
#3
Thread Starter
Lively Member
Re: [2005] ToString() not working for custom class
Thanks for the breakpoint suggestion. I did as you suggested and it seems the ToString() function isn't being called at all. However, once I remove all references to the WebClient parent class by commenting out the Inherits line and the onDownloadStringCompleted() Sub, the ToString() *is* called when I .Add() the XmlSource instances to the ComboBox. Very odd indeed. The reason I tried it without the WebClient inheritance is because that is how the class used to be before I decided to move some of the Xml handling code from the main form to the XmlSource class. When XmlSource was not a child class of WebClient, adding an instance of it to the combobox worked as intended, with the output of ToString() neatly appearing in the ComboBox.
What would cause this? Has anyone seen behaviour like this before?
Yours puzzled,
Mightor
What the world needs is more geniuses with humility, there are so few of us left.
If my post was accidentally useful, please rate it as such, thanks!
Mon aéroglisseur est plein des anguilles.
-
May 2nd, 2007, 07:38 AM
#4
Thread Starter
Lively Member
Re: [2005] ToString() not working for custom class
More weirdness...
When I use a random different class to inherit from instead, such as WebResponse or WebRequest, the ToString() function is called as expected. It works fine unless I inherit from WebClient. What's going on?
Gr,
Mightor
What the world needs is more geniuses with humility, there are so few of us left.
If my post was accidentally useful, please rate it as such, thanks!
Mon aéroglisseur est plein des anguilles.
-
May 2nd, 2007, 10:18 AM
#5
Thread Starter
Lively Member
Re: [2005] ToString() not working for custom class
I did some more testing. I started a new project and created a new Class with a ToString function. I added them to the ComboBox, it displays the proper values in the combox dropdown list just fine. As soon as I add "Inherits System.Net.WebClient" to the class definition, it stops working in the ComboBox. However, calling the ToString() function directly, it returns the proper value as it did before.
What gives?
Gr,
Mightor
What the world needs is more geniuses with humility, there are so few of us left.
If my post was accidentally useful, please rate it as such, thanks!
Mon aéroglisseur est plein des anguilles.
-
May 2nd, 2007, 10:43 AM
#6
Re: [2005] ToString() not working for custom class
I believe this is because the ToString method is inherited from the Component base class. If you read the documentation on the ToString method for the Component class, it is recommended that it is not overridden. You should create your own function to return the name and call that whenever you need it.
http://msdn2.microsoft.com/en-us/lib...ng(VS.80).aspx
The best rule of thumb is don't rely on implicit calls to functions, always explicitly call the function you want to use.
Last edited by Negative0; May 2nd, 2007 at 10:57 AM.
-
May 2nd, 2007, 11:47 AM
#7
Thread Starter
Lively Member
Re: [2005] ToString() not working for custom class
The problem is that with every single other class I create it works fine. The ComboBox.Add method triggers the ToString() function everytime, it just doesn't when I inherit from the WebClient class. If I inherit from another class, it also works, just not WebClient. I can't call the ToString() directly, I have to rely on ComboBox.Add() to do that for me when a new Instance is passed to it.
Gr,
Mightor
What the world needs is more geniuses with humility, there are so few of us left.
If my post was accidentally useful, please rate it as such, thanks!
Mon aéroglisseur est plein des anguilles.
-
May 2nd, 2007, 12:11 PM
#8
Re: [2005] ToString() not working for custom class
Try other classes that are derived from the Componenet Class. You will see the same thing on all of them. 2 Examples:
System.Diagnostics.Eventlog
System.DirectoryServices.DirectoryEntry
So it has something to do with the way the ToString method of the Component class was implemented.
-
May 2nd, 2007, 12:52 PM
#9
Thread Starter
Lively Member
Re: [2005] ToString() not working for custom class
Strange... It's nothing if not inconsistent. Thanks for running some more tests for me. I've decided to now completely reimplement my XmlSource class. It will use a Private WebClient attribute and just generate custom events based on events thrown by the WebClient. This dead horse doesn't seem to be moving in any direction anyway so I may as well work around it. The end result will be pretty much the same anyway and as an added bonus I am learning how to generate custom events!
This has been a very educational experience if nothing else; new VB mechanisms and learning how to use the built-in debugger.
Thanks for the help, although there is no direct solution to my problem, this list never fails to make great suggestions and give me pointers!
Gr,
Mightor
What the world needs is more geniuses with humility, there are so few of us left.
If my post was accidentally useful, please rate it as such, thanks!
Mon aéroglisseur est plein des anguilles.
-
May 2nd, 2007, 06:41 PM
#10
Re: [RESOLVED] [2005] ToString() not working for custom class
Hang on. There's no reason to throw away your inheritance. You can still derive your class from WebClient. It just means that you can't add instances to a ComboBox directly and expect what you want to be displayed via the ToString method. That's not the only way to achieve your aim though. Just create a BindingList, add instances of your class to it, bind the list to the ComboBox and set the DisplayMember to "Name". Voila! You have a list to which you can add or remove items and the Name of each item will be displayed in the ComboBox.
-
May 2nd, 2007, 11:17 PM
#11
Thread Starter
Lively Member
Re: [RESOLVED] [2005] ToString() not working for custom class
 Originally Posted by jmcilhinney
Hang on. There's no reason to throw away your inheritance. You can still derive your class from WebClient. It just means that you can't add instances to a ComboBox directly and expect what you want to be displayed via the ToString method. That's not the only way to achieve your aim though. Just create a BindingList, add instances of your class to it, bind the list to the ComboBox and set the DisplayMember to "Name". Voila! You have a list to which you can add or remove items and the Name of each item will be displayed in the ComboBox.
You prolly won't believe when I say I actually spent a lot of my time last night investigating BindingSources and DataSources. They're really quite cool and would allow me to have one central list of servers and keep track of them being monitored in the objects themselves, rather than with a separate list. Only one thing kind of bugged me and that is that the CheckedListBox isnt able to deal with them. The MSDN pages say "This property is not relevant for this class." Does that mean it is not implemented for this class? I am not sure if it is possible since I have never done it, but you could "fake" a CheckedListBox with a DataViewGrid with a checked column and a text label column, no? I use the CheckedListBox to show the users which of the monitored servers are down. It wouldn't be a very big deal to add objects to that CheckedListBox manually since it is really only a consumer of info and not a producer so if a server status list is refreshed I could just wipe the CheckedListBox clear and add the servers all anew from the central list.
The thing that bugged me about the ToString() not working properly was the fact that I didn't know why and that I couldn't figure out why. My background is mostly Linux and OpenSource and I guess I've always been blessed that if something didn't work the way I wanted, I could (usually) at least figure out why it didn't and if I should choose to, amend the situation. That and the fact that I can be very stubborn, but I guess that's pretty obvious from some of my posts.
My new class with the WebClient as a private member and custom events works very well, btw, but I'd still like to use the BindingSource stuff just because it's a nice new feature to get to grips with.
Gr,
Mightor
What the world needs is more geniuses with humility, there are so few of us left.
If my post was accidentally useful, please rate it as such, thanks!
Mon aéroglisseur est plein des anguilles.
-
May 2nd, 2007, 11:51 PM
#12
Re: [RESOLVED] [2005] ToString() not working for custom class
The CheckedListBox indeed doesn't support complex data-binding. I'm guessing that this is because there is no guaranteed to be a property of the list that corresponds to the check box states. Some people would want to bind a Boolean property to the check boxes while others wouldn't and it would just be messy. You could, of course, implement your own CheckedListBox that does support complex data-binding in the way that works for you. I would do as you suggested and go with the DGV.
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
|