Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Overloading Problem


Reply
 
Thread Tools Display Modes
  #1  
Old 01-31-2006, 11:11 AM
Pickle136 Pickle136 is offline
Regular
 
Join Date: Aug 2004
Posts: 61
Default 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
Reply With Quote
  #2  
Old 01-31-2006, 11:19 AM
Pickle136 Pickle136 is offline
Regular
 
Join Date: Aug 2004
Posts: 61
Default

Shouldnt the overloaded functions see what the new type is once the function returns? Not always use a generic object.
Reply With Quote
  #3  
Old 01-31-2006, 11:31 AM
wayneph's Avatar
wayneph wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

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
Reply With Quote
  #4  
Old 01-31-2006, 12:05 PM
Pickle136 Pickle136 is offline
Regular
 
Join Date: Aug 2004
Posts: 61
Default

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.
Reply With Quote
  #5  
Old 01-31-2006, 12:22 PM
Pickle136 Pickle136 is offline
Regular
 
Join Date: Aug 2004
Posts: 61
Default

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.
Reply With Quote
  #6  
Old 01-31-2006, 01:15 PM
Pickle136 Pickle136 is offline
Regular
 
Join Date: Aug 2004
Posts: 61
Default

Can you give an example? I am starting to understand what interfaces do, but im not sure how it will help in my problem.
Reply With Quote
  #7  
Old 01-31-2006, 04:30 PM
herilane's Avatar
herilane herilane is offline
Unashamed geek

Retired Moderator
* Expert *
 
Join Date: Jul 2003
Location: London, England
Posts: 8,988
Default

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.
Reply With Quote
  #8  
Old 02-01-2006, 07:31 AM
Pickle136 Pickle136 is offline
Regular
 
Join Date: Aug 2004
Posts: 61
Default

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?
Reply With Quote
  #9  
Old 02-04-2006, 06:29 AM
herilane's Avatar
herilane herilane is offline
Unashamed geek

Retired Moderator
* Expert *
 
Join Date: Jul 2003
Location: London, England
Posts: 8,988
Default

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.)
Attached Files
File Type: zip InterfacesExample.zip (5.6 KB, 1 views)
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->