How can I create a method in C# that behaves like the String.ToString()?
I know (or at least I think I do) need to extend or create a new class, but I don't know how to retrieve the object value before the .ToString().
Something like I used to do in ActionScripts a few years ago... - MovieClip.prototype.MyFunction(){
-
trace (this.name);
-
}
Then I'd use just like... myClip.MyFunction();
Im trying to achieve the same result in C# (2.0) but haven't got any good results here.
It should looks like this: - public string Add1() {
-
// method constructor here;
-
}
The I could use it like -
string aVar = "word";
-
string result = aVar.Add1(); // resulting "word1"
Any help is welcome, since I tried to google about it, but I dont know who exatcly this kind of method is called.
Ricardo
9 1309
How can I create a method in C# that behaves like the String.ToString()?
I'm not sure if I understand what you want to do, so I'll restate the problem as I understood:
You have a class (I'll call it Toot). You want to override the ToString method of that class so that it provides some useful information. (in my code I'll return Toot from kkk, where kkk is the Name property of the class that created the Toot).
To test my code, create a Windows App with a label and a button. Double-click on the button and replace all of the code with: -
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Text;
-
using System.Windows.Forms;
-
using System.Reflection;
-
-
namespace WindowsApplication1
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
private void button1_Click(object sender, EventArgs e)
-
{
-
Toot t = new Toot(this);
-
label1.Text = t.ToString();
-
}
-
}
-
public class Toot
-
{
-
private object mParent;
-
public Toot(object parent)
-
{
-
mParent = parent;
-
}
-
public override string ToString()
-
{
-
Type t = mParent.GetType();
-
PropertyInfo pi = t.GetProperty("Name");
-
if (pi != null)
-
return ("Toot from " + pi.GetValue(mParent, new object[] { }).ToString());
-
else
-
return ("Toot from unnamed source");
-
}
-
}
-
}
-
Notice that I added System.Reflection and used it to see if the parent had a Name property. If you only want to display Toot's class variables, then you don't need any of my code. In your class you just type "Public override" and Intellisense does the rest. HTH --Sam
I'm not sure if I understand what you want to do, so I'll restate the problem as I understood:
You have a class (I'll call it Toot). You want to override the ToString method of that class so that it provides some useful information. (in my code I'll return Toot from kkk, where kkk is the Name property of the class that created the Toot).
To test my code, create a Windows App with a label and a button. Double-click on the button and replace all of the code with:
[code..]
Notice that I added System.Reflection and used it to see if the parent had a Name property. If you only want to display Toot's class variables, then you don't need any of my code. In your class you just type "Public override" and Intellisense does the rest. HTH --Sam
I dont wanna to override the ToString method... it was just an example.
[Edit]
It should be something like -
int a = 10;
-
string b = "test";
-
-
public MyMethod(){
-
// get the object and add 1;
-
}
-
-
string output1 = a.MyMethod(); // 101
-
string output2 = b.MyMethod(); // test1
-
Can that be done?
Thanks SammyB for your help!
-
int a = 10;
-
string b = "test";
-
-
public MyMethod(){
-
// get the object and add 1;
-
}
-
-
string output1 = a.MyMethod(); // 101
-
string output2 = b.MyMethod(); // test1
-
I am not sure if this will help, but based on the example above, you need to write it as: -
int a = 10;
-
string b = "test";
-
-
public string MyMethod(object o){
-
// get the object and add 1;
-
return o.ToString() + "1";
-
}
-
-
string output1 = MyMethod(a); // 101
-
string output2 = MyMethod(b); // test1
-
I am not sure if this will help, but based on the example above, you need to write it as: -
int a = 10;
-
string b = "test";
-
-
public string MyMethod(object o){
-
// get the object and add 1;
-
return o.ToString() + "1";
-
}
-
-
string output1 = MyMethod(a); // 101
-
string output2 = MyMethod(b); // test1
-
Thanks for your reply jjvainav. But this is how I'm currently doing it.
I'm looking for something like this: http://weblogs.asp.net/dwahlin/archi...n-methods.aspx
But for C# 2.0.
If you look the first example - using StringExtensions;
-
-
-
-
....
-
string phone = "123-123-1234";
-
string newPhone = phone.RemoveNonNumeric();// <- This is what I'm trying to do.
It'll output 1231231234.
Thanks again.
have a look at this link
Extension methods seems to be more of a compiler trick.
I did not read into the details of it, but I am subscribing to this thread to refer to it later.
Hope it helps
have a look at this link
Extension methods seems to be more of a compiler trick.
I did not read into the details of it, but I am subscribing to this thread to refer to it later.
Hope it helps
It works only when creating a project using the new VS 2008 and targeting the 2.0 (I'm still using VS 2005).
I'll download the C# 2008 Express Edition and see how my library works.
Thanks a lot for this link Shashi.
Edit: oops.
Ok, so you wanted to implictly extend the base class prototypes (like in javascript). I gotcha.
I'm not sure why you needed to do that instead of just doing this -
public static string RemoveNonNumeric(string somestring)
-
type of function, but at least .NET3.0 accomodates it.
Edit: oops.
Ok, so you wanted to implictly extend the base class prototypes (like in javascript). I gotcha.
I'm not sure why you needed to do that instead of just doing this -
public static string RemoveNonNumeric(string somestring)
-
type of function, but at least .NET3.0 accomodates it.
I'm writing a .dll so I'm trying to make things easier for those who will use it. Of course it can be done like
string a = MyMethod(something), but having something similar to ToString() is way better (IMO).
I'll try this C# 2008 Express to see how it works... The link Shashi gave is what I was looking for, except for the 2008 version. But it's not a big issue.
Just to post a feedback here.
Wrote a small sample app in C# VS 2008 Express and tried the extension method. It worked fine, except that, to keep this functionality, anyone using my library would need to work with 2008 as well.
I think it can't be done in 2.0.
Anyway, thanks everyone who posted here, got a great support from you guys.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Edvard Majakari |
last post by:
Greetings, fellow Pythonistas!
I'm about to create three modules. As an avid TDD fan I'd like to create
typical 'use-cases' for each of these modules. One of them is rather large,
and I wondered...
|
by: Davinci_Jeremie |
last post by:
Hi Newbee here to C# I have a simple questions...
In a Hello world example how does the class object Hello exist with out
creating it? I come from object pascal where everything object is...
|
by: Carlos Lozano |
last post by:
Hi,
What is the right way to create an OCX COM component. The component is
already registerred, but can't create an instance. I am using the reference
to the interop module created.
If I use...
|
by: Ken Varn |
last post by:
I am just starting the process of creating ASP.NET server controls. I have
created controls for .NET applications, but have just started with ASP.NET.
I am a little confused about some areas that...
|
by: EnglishMan69 |
last post by:
Hello All,
I am using VB2005 Beta 2 in VS 2005 and am running into a small
problem. I need to be able to add a picture box to the main form from
within a thread.
The program goes to a web...
|
by: Richard Bysouth |
last post by:
I'm sure this is something pretty trivial but can't seem to figure out how
to do it.
I'm using Generic Lists in quite a few places in my app and for some of them
want to be able to create a...
|
by: wcc |
last post by:
Hello,
How do I create a class using a variable as the class name?
For example, in the code below, I'd like replace the line
class TestClass(object):
with something like
class...
|
by: nyathancha |
last post by:
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is...
|
by: Dan |
last post by:
Hi,
I use the <asp:CreateUserWizardcontrol for creating memberusers. In that
windows, one must provide an emailaddress.
My question is: how can an user later change his emailaddress?
Thanks...
|
by: Motoma |
last post by:
This article is cross posted from my personal blog. You can find the original article, in all its splendor, at http://motomastyle.com/creating-a-mysql-data-abstraction-layer-in-php/.
Introduction:...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |