Last post Feb 25, 2009 09:06 AM by RichardDavidLee
Feb 25, 2009 07:23 AM|RichardDavidLee|LINK
Hello everyone, can you help me figure out how to overload the String * operator, so that it behaves as a repeater,
I want to be able to have the code: msgbox("Hello," * 3)
popup a message box on screen, and the message display: "Hello,Hello,Hello"
I know how to create classes that exhibit this type of behavour, but I don't know how to go about overloading the String datatype directly.
is this possible?
Feb 25, 2009 08:38 AM|simon.hatchard|LINK
You cannot override simple types such as strings, but if you are using .net 3.5 you can achieve what you are after in a nice simple way by creating an extension method which you could use to do this, something like (VB.net)
Public Function Repeat(ByVal value as string, count as integer)
... Repeater code here
Then this could be used in your application like
Dim s as String = "Hello"
Feb 25, 2009 08:43 AM|DkUltra|LINK
Hey, I found the following site that might be helpfull
Define the operator behavior for the * symbol
Feb 25, 2009 08:43 AM|yrajasekhar|LINK
Hi, You can use extension methods to include a new method for already existing type like string. See this article for details.
Feb 25, 2009 09:06 AM|RichardDavidLee|LINK
Thanks for the replies, unfortunatly I am stuck with using asp.net 2.0, orcas is out of my reach. so I guess its just not possible. thanks anyway.