Writing overrides

D

David

Hi all,

I am having a few questions today.

Anyway,

I want to create a class file, within the class file, I want to have
functions that can give you optional paramaters...

Such like...

public string AddText(int mynumber)

public string AddText(string MyText)

public string AddText(DataTable MyTableOfText)

public string AddText(StringBuilder MyStringOfText)

How do I really do this?

Probably really simple but an example would go down a treat.

Thanks.
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 
M

Mythran

David said:
Hi all,

I am having a few questions today.

Anyway,

I want to create a class file, within the class file, I want to have
functions that can give you optional paramaters...

Such like...

public string AddText(int mynumber)

public string AddText(string MyText)

public string AddText(DataTable MyTableOfText)

public string AddText(StringBuilder MyStringOfText)

How do I really do this?

Probably really simple but an example would go down a treat.

Thanks.
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

Overloading, not overriding :)

public overloads string AddText(int MyNumber)
{
}

public overloads string AddText(string MyText)
{
}

HTH,
Mythran
 
D

David

Overloading, not overriding :)

public overloads string AddText(int MyNumber)
{
}

public overloads string AddText(string MyText)
{
}

HTH,
Mythran

Thank you.

I suppose then that I can easily pass from one to the other, such like...

MyString = AddText(25);

public overloads string AddText(int MyNumber)
{
return AddText(MyNumber.ToString());
}

public overloads string AddText(string MyText)
{
return MyText + " is not a prime";
}

would output "25 is not a prime"

Thanks.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 
K

Kevin Spencer

Hi David,

This is called Overloading, and in VB.Net, you would use the Overloads
keyword in your Function declaration. Example:

Public Overloads String AddText(int mynumber)
....

Public Overloads String AddText(String MyText)
....


--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
The sun never sets on
the Kingdom of Heaven
 
M

Mythran

Shawn said:
Could you explain when you should use Overloading and when not to?

Shawn

Let's say you have a method that requires a single parameter, Text ...

Public Sub WriteLine(ByVal Line As String)
Console.WriteLine(Line)
End Sub

Now you have this line, but lets now say you would like to have a similar
method that writes a line to a file given a format and arguments:

Public Overloads Sub WriteLine(ByVal Line As String)
Console.WriteLine(Line)
End Sub

Public Overloads Sub WriteLine( _
ByVal Format As String, _
ByVal ParamArray Args As String() _
)
Console.WriteLine(Format, Args)
End Sub

And now you have need to write a numeric line:

Public Overloads Sub WriteLine(ByVal Line As String)
Console.WriteLine(Line)
End Sub

Public Overloads Sub WriteLine( _
ByVal Format As String, _
ByVal ParamArray Args As String() _
)
Console.WriteLine(Format, Args)
End Sub

Public Overloads Sub WriteLine(ByVal Number As Integer)
Console.WriteLine(Number.ToString())
End Sub


HTH,
Mythran
 
S

Shawn

The Overloads keyword I mean. Take this example.
Are there any differeces between this way of writing it:
Private Overloads Sub test2(ByVal input As Integer)
...
End Sub

Private Overloads Sub test2(ByVal input As String)
...
End Sub

And this one:
Private Sub test2(ByVal input As Integer)
...
End Sub

Private Sub test2(ByVal input As String)
...
End Sub

Thanks,
Shawn
 
J

Juan T. Llibre

When you use Overloads, you can use any of the data types
that you included in the function overload(s).


Public Overloads Function writetext(ByVal h As String)Response.write(h)End FunctionPublic
Overloads Function writetext(ByVal h As Integer)Response.write(h.ToString)End
FunctionPublic Overloads Function writetext(ByVal h As Long)Response.write(h.ToString)End
FunctionBy using the Overloads methods above, your overloaded writetext function
will return the correct output, regardless of whether the returned data
is a string, an integer or a long.

In essence, you overload to allow different outputs for the same function.
 
J

Juan T. Llibre

Aargh!

The formatting was messed up.

When you use Overloads, you can use any of the data types
that you included in the function overload(s).

Public Overloads Function writetext(ByVal h As String)
Response.write(h)
End Function

Public Overloads Function writetext(ByVal h As Integer)
Response.write(h.ToString)
End Function

Public Overloads Function writetext(ByVal h As Long)
Response.write(h.ToString)
End Function

By using the Overloads methods above, your overloaded writetext function
will return the correct output, regardless of whether the returned data
is a string, an integer or a long.

In essence, you overload to allow different outputs for the same function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top