Shadows/Overloads Issue

Mike_R

Junior Contributor
Joined
Oct 20, 2003
Messages
316
Location
NYC
Hi guys, I'm trying to make a very simple 'Convert' class. The idea is to Inherit & extend the System.Convert class, but, technically, that class is not Inheritable... Anyway, I'll "inherit" this functionality by using System.Convert when necessary...

My current problem is far simpler, I can't even get on first base! My code is as follows:
Visual Basic:
Friend Class xoConvert
    Friend Function ToString(ByVal TheObject As Object) As String

    End Function
End Class
The result of the above is a squiggly line below the word 'ToString' and the error message reads:
Function 'ToString' shadows an overloadable member declared in the base class 'Object'. If you want to overload the base method, this method must be declared 'Overloads'.
I was puzzled by this, esp. given that the same code in C# appears to be fine:
Code:
	public class xoConvert
	{
		public String ToString(Object TheObject)
		{
		}				 
	}
Any ideas with what I'm doing wrong here. I'm neither trying to Overload nor Shadow anything. Heck, this Class, as constructed so far, isn't even Inheriting anything...

Thanks in advance :),
Mike
 
I am assumming you want a static methid, similar to the other converts. . .

Visual Basic:
[/color]
[color=black]Public [/color][/color][/size][color=black][size=2]Class[/size][size=2] CustomConvert[/size][/color]
[color=black][size=2]Public [/size][size=2]Overloads [/size][size=2]Shared [/size][size=2]Function[/size][size=2] ToString([/size][size=2]ByVal[/size][size=2] anObject [/size][size=2]As[/size][size=2]Object[/size][size=2]) [/size][size=2]As[/size][size=2]String[/size][/color]
[color=black][color=black][size=2][/size][/color][/color] 
[color=black][color=black][size=2]End [/size][/color][/color][color=black][size=2]Function

Frankly, for ToString, I would redefine ToString in your projects classes
 
Thanks Joe, I really appreciate it...

(1) Yes, it is to be Static ("Shared"), but in my confusion over the compiler complaining/requiring that I use the 'Overloads' keyword, I tried making changes, including (a) making it a Friend Method and (b) removing 'Shared'. (No real reason for the latter, but I was willing to try anything!)

(2) Ok, so you used 'Overloads' and that seems natural to you... Can you explain the mentality here? This Class is within it's own NameSpace; there should be no conflicts, I would think, and so no need for Overloads? Clearly I'm not quite understanding something (basic!) here. Could you help me out?

(3) My goal for this (currently), is to create a method that can convert an Array to print out it's contents. Something that would work like this:
Visual Basic:
Dim MyArray(,) As Integer = {{1, 2, 3,},{3, 4, 5}}
CustomConvert.ToString(MyArray) ' <-- Returns: "{{1, 2, 3,},{3, 4, 5}}"
The logic of going through this is fairly easy (and I've done it for VB6 before), do you know if I'm re-inventing the wheel here? That is, is there a .Net method that already has tis capablity?

Much thanks for help on any of the above! :)
Mik
 
everything derives from the class Object

Object has a Virtual Method ToString()

the default ToString() returns, I believe the name of the class.

given:
Public Class MyClass
end class

then
dim mc as MyClass = New MyClass()
dim s as string = MyClass.ToString()

s is now equal to "MyClass"

in order to get different behavior, you must overload/override
 
I see.. I'm "implicitly" inheriting from the Object class. I had not thought about that. Thanks very much!

Huh, this is odd... 'Object.ToString' does not show up under IntelliSense/AutoComplete within VB.Net, but it can be called. And it does show up as a public memeber within the Object Browser. Strange, eh? How can it be Public and yet not show up under IntelliSense?
 
Ok, I found it where you said it would be. Thank you very much. :)

I can see why they did this and I don't mind them hiding them. It shortens the IntelliSense list when using my own Classes, reducing the clutter. However, in this case, that they considered '.ToString' to be "Advanced" is a little silly, eh?

Anyway, I thank you much for the info, it's a big help.
 
Back
Top