?
Solved

Overloading a built-in function

Posted on 2007-12-03
18
Medium Priority
?
337 Views
Last Modified: 2013-11-07
I have created a custom role provider, in it there is:
 Public Overrides Function GetRolesForUser(ByVal username As String) As String()

I want to create my own function, so I have created an Overloaded one:

Public Overloads Function GetRolesForUser(ByVal level As Integer) As String()

However, when I call it and pass in an integer, it always goes to the first one, not my overloaded one.

Dim myroles As Array = Roles.GetRolesForUser(Level)

What am I missing?

thanks.



0
Comment
Question by:Starr Duskk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 4
18 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20400440
What type is "Level"?
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20400455
Public Overrides Overloads Function GetRolesForUser(ByVal level As Integer) As String()
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20400472
First version should have the Overloads keyword too.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20400480
oops, imitchie has already stated that
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20400494
No, my new function is not in the base class with an Integer variable, so I can't override it.... I had already tried that.

Compiler Error Message: BC30284: function 'GetRolesForUser' cannot be declared 'Overrides' because it does not override a function in a base class.
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20400506
I meant the string one. Jaime pointed out the right one. It both overrides the virtual, and also overloads the other one.
0
 
LVL 25

Accepted Solution

by:
imitchie earned 1000 total points
ID: 20400512
The catch is that you cannot get to the Int version from the Base class, i.e.

:base: (a)
Public Virtual Function GetRolesForUser(ByVal username As String)

:derived: (b), (c)
Public Overloads Overrides Function GetRolesForUser(ByVal username As String) As String()
Public Overloads Function GetRolesForUser(ByVal level As Integer) As String()

Dim a as base
a = new derived();
dim s as string
base.GetRolesForUser(s) - calls (b)
((derived)a).GetRolesForUser(s) - calls (b)
((derived)a).GetRolesForUser(123) - calls (c)
a.GetRolesForUser(123) - calls (b) [ or throws invalid cast exception ]
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 1000 total points
ID: 20400564
as an alternative you can forget to derive but compose instead, so you will have total freedom to define your methods:


Public Class A 
    Private obj As B 
    
    Public Sub New() 
        obj = New B() 
    End Sub 
 
     ' override is not needed
     Public Overloads Function GetRolesForUser(ByVal username As String) As String()
             ' call obj.GetRolesForUser() here
     End function
     Public Overloads Function GetRolesForUser(ByVal level As Integer) As String()
             ' call obj.GetRolesForUser() here
     End function
 
     ' Implement other methods and properties needed, just wrap the members from class B
End Class 

Open in new window

0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20400673
Sorry, ya'll have totally lost me.

I have an overloaded GetUser method in my custom provider and it simply has me pass in a username, without the logged in value, and it hits it every time.

I dont' understand why this one doesn't and I don't understand what you're asking me to do. Are you asking me to just forget the custom roles and create my own class which has methods using the same name? Cause that's what it looks like.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20400688
I am talking about forgetting the inheritance approach and use a wrapper instead.
A wrapper will give you a better freedom to define your overloaded methods and expose just the methods and properties you want.
But as I mentioned, this is just an alternative.
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20402612
Do I put that derived class in my customMembershipProvider?

And if so, how do I call it?

Remember, I'm a noob. I don't know what wrapping is.

thanks.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20402643
inheriting:

class baseClass
{
}

class derivedClass : baseClass
{
}

wrapping:

class baseClass
{
}

class wrapperClass
{
        baseClass instance;
}

Please read my previous comments about the advantages of a wrapper class for your case.
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20402854
Sooo.....

In answer to my questions?

Do I put that derived class in my customMembershipProvider?

And if so, how do I call it?

0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20402960
Okay, I have the class written within my customMembershipProvider file at the bottom of the main class.

But I still don't know how to call it?

Because if I use:
myUser = Membership.GetUser(userID)

It only offers me the original two methods.

How do I call it please?

thanks.

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20403123
could you post the full source code?
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20403349
I am unable to post my source code. It's proprietary.

Whether it's GetRolesforUser or GetUser, there has to be a way to all it and tell it to use the wrapped class you suggest.

How does one call a wrapped class?




0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20403374
I cannot help you if I don't know where you derived your class from.
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 20403750
Nevermind. I asked it a different way in another question and got my answer.
thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question