Creating a method in C# (2.0)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • skipbr
    New Member
    • Jul 2007
    • 13

    Creating a method in C# (2.0)

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

    Code:
    MovieClip.prototype.MyFunction(){
          trace (this.name);
    }
    Then I'd use just like... myClip.MyFuncti on();
    Im trying to achieve the same result in C# (2.0) but haven't got any good results here.
    It should looks like this:
    Code:
    public string Add1() {
         // method constructor here; 
    }
    The I could use it like
    Code:
    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
  • SammyB
    Recognized Expert Contributor
    • Mar 2007
    • 807

    #2
    Originally posted by skipbr
    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:
    [code=c]
    using System;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows. Forms;
    using System.Reflecti on;

    namespace WindowsApplicat ion1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeCompo nent();
    }
    private void button1_Click(o bject 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(mPa rent, new object[] { }).ToString());
    else
    return ("Toot from unnamed source");
    }
    }
    }
    [/code]
    Notice that I added System.Reflecti on 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

    Comment

    • skipbr
      New Member
      • Jul 2007
      • 13

      #3
      Originally posted by SammyB
      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.Reflecti on 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
      Code:
      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!

      Comment

      • jjvainav
        New Member
        • Feb 2008
        • 25

        #4
        Code:
        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:

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

        Comment

        • skipbr
          New Member
          • Jul 2007
          • 13

          #5
          Originally posted by jjvainav
          I am not sure if this will help, but based on the example above, you need to write it as:

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

          But for C# 2.0.

          If you look the first example
          [code=c]using StringExtension s;



          ....
          string phone = "123-123-1234";
          string newPhone = phone.RemoveNon Numeric();// <- This is what I'm trying to do.[/code]
          It'll output 1231231234.

          Thanks again.

          Comment

          • Shashi Sadasivan
            Recognized Expert Top Contributor
            • Aug 2007
            • 1435

            #6
            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

            Comment

            • skipbr
              New Member
              • Jul 2007
              • 13

              #7
              Originally posted by Shashi Sadasivan
              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.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                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
                Code:
                public static string RemoveNonNumeric(string somestring)
                type of function, but at least .NET3.0 accomodates it.

                Comment

                • skipbr
                  New Member
                  • Jul 2007
                  • 13

                  #9
                  Originally posted by Plater
                  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
                  Code:
                  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(someth ing)
                  , 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.

                  Comment

                  • skipbr
                    New Member
                    • Jul 2007
                    • 13

                    #10
                    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.

                    Comment

                    Working...