 |
 |

01-31-2006, 11:11 AM
|
|
Regular
|
|
Join Date: Aug 2004
Posts: 61
|
|
Overloading Problem
|
I have a string that I use to search a group of objects. There are three types of object, each have a property of name. I compare my string to the names and once I have a match I return the object.
I declared my function to return an object. This function works fine.
Once I have the object I pass it to an overloaded function, there is one for each of the types. When I run the code it complains that the object returned doesnt match a method in the class. If I turn on Option Strict it gives that object cant be converted to any of the types. I look at the returned object and it is one of the types, I dont get it.
Code:
Public Sub Load_FormAttributes(ByRef DBCnode As TreeNode, ByRef FormListBox As ListBox)
Dim strName() As String
strName = DBCnode.FullPath.Split()
strName(0) = strName(CInt(strName.LongLength) - 1)
Load_Attributes(FindDatabaseItem(strName(0)), FormListBox)
End Sub
Private Function FindDatabaseItem(ByVal strName As String) As Object
...
End Funtion
Private Overloads Sub Load_Attributes(ByRef objModule As CModule, ByRef FormListBox As ListBox)
End Sub
Private Overloads Sub Load_Attributes(ByRef objMessage As CMessage, ByRef FormListBox As ListBox)
End Sub
Private Overloads Sub Load_Attributes(ByRef objSignal As CSignal, ByRef FormListBox As ListBox)
End Sub
|
|

01-31-2006, 11:19 AM
|
|
Regular
|
|
Join Date: Aug 2004
Posts: 61
|
|
|
Shouldnt the overloaded functions see what the new type is once the function returns? Not always use a generic object.
|
|

01-31-2006, 11:31 AM
|
 |
Web Junkie
Retired Moderator * Expert *
|
|
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
|
|
|
What I'd do is create an Interface that all of your Classes Implement. That will require the name property.
Then you can have the FindObject method return an instance of the Interface, and have a single Load_Attributes that accepts an instance of that interface. It won't matter which of the actual 3 types it is, because the interface guaruntees that the properties you are working with are there.
|
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
|

01-31-2006, 12:05 PM
|
|
Regular
|
|
Join Date: Aug 2004
Posts: 61
|
|
Im not sure I get what your suggesting.
I am using a Find Method for each of the class types:
Code:
Dim objModule As CModule = DBC.Modules.Find(AddressOf DBC.FindModuleBy_Name) 'Find the Module
Dim objMessage As CMessage = objModule.Messages.Find(AddressOf DBC.FindMessageBy_Name) 'Find the Message
Are you saying that I need only one Find Method for all of the class's?
|
Last edited by Pickle136; 01-31-2006 at 12:17 PM.
|

01-31-2006, 12:22 PM
|
|
Regular
|
|
Join Date: Aug 2004
Posts: 61
|
|
|
Sorry, I think i did misunderstand. I have never used Interface's before, I am looking up information to see I can understand what you meant.
|
|

01-31-2006, 01:15 PM
|
|
Regular
|
|
Join Date: Aug 2004
Posts: 61
|
|
|
Can you give an example? I am starting to understand what interfaces do, but im not sure how it will help in my problem.
|
|

01-31-2006, 04:30 PM
|
 |
Unashamed geek
Retired Moderator * Expert *
|
|
Join Date: Jul 2003
Location: London, England
Posts: 8,988
|
|
Simple example (totally untested)
Code:
Public Interface ILoadable
Property Name () As String
End Interface
Class CModule
Implements ILoadable
Public Property Name() As String Implements ILoadable.Name
'property implementation goes here...
End Property
End Class
'same for the other classes... CMessage and whatnot
'Then use the interface like this:
Private Overloads Sub Load_Attributes(ByRef objToLoad [b]As ILoadable[/b], ByRef FormListBox As ListBox)
End Sub
Also, do you have a particular need for ByRef? The default ByVal is usually a better (definitely safer!) choice unless you especially need to pass a variable by reference.
|
|

02-01-2006, 07:31 AM
|
|
Regular
|
|
Join Date: Aug 2004
Posts: 61
|
|
I dont see how this will work.
My data is a group of class's.
CDBC is the main object which has List of CModules each of which has a List of CMessages each of which has a List of CSignals each of which could have a List of CMultiSignals.
I view the object through a treeview by creating nodes with names from the name property in the objects.
If an node is selected I want to display other properties in a list box.
So I search the objects, and use a find method heres a portion:
Code:
Private Function FindDatabaseItem(ByVal strName As String) As Object
strModuleSearchPar = strName
Dim objModule As CModule = DBC.Modules.Find(AddressOf DBC.FindModuleBy_Name) 'Find the Module
If objModule Is Nothing Then
For Each objModule In DBC.Modules
strMessageSearchPar = strName
Dim objMessage As CMessage = objModule.Messages.Find(AddressOf DBC.FindMessageBy_Name) 'Find the Message
If objMessage Is Nothing Then
This function returns the object with the matching name. At this point was when I was going to use the overloads from above to send data to the list box.
Can I still use the interface even though I have objects nested within objects?
|
|

02-04-2006, 06:29 AM
|
 |
Unashamed geek
Retired Moderator * Expert *
|
|
Join Date: Jul 2003
Location: London, England
Posts: 8,988
|
|
|
You can certainly use an interface for nested classes.
I'm not sure I really understand your problem... I've created a quick example based on what I think you might be doing. See if that helps make things clearer.
(It sounds like you're using VB 2005 so you should be able to open this.)
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|