Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

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:
	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

Posting Guidelines

 

Avatar by Lebb

Posted

I am assumming you want a static methid, similar to the other converts. . .

 

[/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

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

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:

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

Posting Guidelines

 

Avatar by Lebb

Posted

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

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

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?

Posting Guidelines

 

Avatar by Lebb

Posted

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.

Posting Guidelines

 

Avatar by Lebb

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...