473,327 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Creating a method in C# (2.0)

13
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...

Expand|Select|Wrap|Line Numbers
  1. MovieClip.prototype.MyFunction(){
  2.       trace (this.name);
  3. }
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:
Expand|Select|Wrap|Line Numbers
  1. public string Add1() {
  2.      // method constructor here; 
  3. }
The I could use it like
Expand|Select|Wrap|Line Numbers
  1. string aVar = "word";
  2. 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
Feb 27 '08 #1
9 1309
SammyB
807 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Reflection;
  9.  
  10. namespace WindowsApplication1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.         private void button1_Click(object sender, EventArgs e)
  19.         {
  20.             Toot t = new Toot(this);
  21.             label1.Text = t.ToString();
  22.         }
  23.     }
  24.     public class Toot
  25.     {
  26.         private object mParent;
  27.         public Toot(object parent)
  28.         {
  29.             mParent = parent;
  30.         }
  31.         public override string ToString()
  32.         {
  33.             Type t = mParent.GetType();
  34.             PropertyInfo pi = t.GetProperty("Name");
  35.             if (pi != null)
  36.                 return ("Toot from " + pi.GetValue(mParent, new object[] { }).ToString());
  37.             else
  38.                 return ("Toot from unnamed source");
  39.         }
  40.     }
  41. }
  42.  
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
Feb 27 '08 #2
skipbr
13
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
Expand|Select|Wrap|Line Numbers
  1. int a = 10;
  2. string b = "test";
  3.  
  4. public MyMethod(){
  5.    // get the object and add 1;
  6. }
  7.  
  8. string output1 = a.MyMethod(); // 101
  9. string output2 = b.MyMethod(); // test1
  10.  
Can that be done?

Thanks SammyB for your help!
Feb 29 '08 #3
Expand|Select|Wrap|Line Numbers
  1. int a = 10;
  2. string b = "test";
  3.  
  4. public MyMethod(){
  5.    // get the object and add 1;
  6. }
  7.  
  8. string output1 = a.MyMethod(); // 101
  9. string output2 = b.MyMethod(); // test1
  10.  
I am not sure if this will help, but based on the example above, you need to write it as:

Expand|Select|Wrap|Line Numbers
  1. int a = 10;
  2. string b = "test";
  3.  
  4. public string MyMethod(object o){
  5.    // get the object and add 1;
  6.     return o.ToString() + "1";
  7. }
  8.  
  9. string output1 = MyMethod(a); // 101
  10. string output2 = MyMethod(b); // test1
  11.  
Feb 29 '08 #4
skipbr
13
I am not sure if this will help, but based on the example above, you need to write it as:

Expand|Select|Wrap|Line Numbers
  1. int a = 10;
  2. string b = "test";
  3.  
  4. public string MyMethod(object o){
  5.    // get the object and add 1;
  6.     return o.ToString() + "1";
  7. }
  8.  
  9. string output1 = MyMethod(a); // 101
  10. string output2 = MyMethod(b); // test1
  11.  

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
Expand|Select|Wrap|Line Numbers
  1. using StringExtensions;
  2.  
  3.  
  4.  
  5. ....
  6. string phone = "123-123-1234";
  7. string newPhone = phone.RemoveNonNumeric();// <- This is what I'm trying to do.
It'll output 1231231234.

Thanks again.
Feb 29 '08 #5
Shashi Sadasivan
1,435 Expert 1GB
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
Feb 29 '08 #6
skipbr
13
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.
Feb 29 '08 #7
Plater
7,872 Expert 4TB
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
Expand|Select|Wrap|Line Numbers
  1. public static string RemoveNonNumeric(string somestring)
  2.  
type of function, but at least .NET3.0 accomodates it.
Feb 29 '08 #8
skipbr
13
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
Expand|Select|Wrap|Line Numbers
  1. public static string RemoveNonNumeric(string somestring)
  2.  
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.
Feb 29 '08 #9
skipbr
13
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.
Feb 29 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
6
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...
15
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...
3
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...
3
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...
2
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...
6
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...
26
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...
5
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...
17
Motoma
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:...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
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...
0
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...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
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
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.