Pass Class Property to method as parameter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mironline
    New Member
    • May 2008
    • 24

    Pass Class Property to method as parameter

    dear friends ,

    Is it possible to pass the class property to method as parameter ?
    for e.g

    Code:
    class foo {
    int x;
    
    public int number {
    set { set x = value;}
    get { return x;}
    }
    
    }
    
    public class _foo 
    {
    foo f = new foo();
    test(f.numer);
    
    void test( ? ) {
    // get the property name here 
    }
    
    }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Normally I would say "The best to answer that is to suggest you try it. Experimentation is the best way to learn."

    But I see in your sample code that you're not sure how to pass it. So I think your question really is "How do I pass it?" more than "Is it possible?"

    First: Fix how you spelled "number" on line 14. You can't expect it to work correctly if you try to pass f.numer instead of f.number.

    Second: Line 16 should just expect to receive it as an int since that is how you created it in line 4
    void test(int nIncomingIntege r)

    Comment

    • mironline
      New Member
      • May 2008
      • 24

      #3
      Dear tlhintoq

      Thank you for spelling suggestion.
      I think you did not get a picture.

      please check this code :

      Code:
      foo f = new foo();
      f.number  = 13;
      test(f.number);
      
      void test(int _number)
      {
         Console.WriteLine (_number.ToString()); // result : 13
      }

      BUT I am looking for code like this :


      Code:
      foo f = new foo();
      f.number  = 13;
      test(f.[B]number[/B]);
      
      void test( [B][U]???? [/U][/B])
      {
         PropertyInfo info = foo.GetType().GetProperties();
      
          // for example 
          string PropertyType  =  info[[B][U]?????[/U][/B]].PropertyType.ToString();
         Console.WriteLine ("Name" + [B][U]????? [/U][/B]+ " : Type" +  PropertyType); 
            // result : Name : [B]number[/B] : Type :  [B]Int32[/B]
         
      }

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Code:
        foo f = new foo();
        f.number  = 13;
        test(f.number);
         
        void test( ???? )
        {
        In your line 3 - you have indicated you are passing just the int of 'number' to the method "Test".

        Code:
        foo f = new foo();
        f.number  = 13;
        test(f.[B]number[/B]);
         
        void test(int [B]IncomingInteger [/B])
        {
        Therefore in line 5 where you declare the method "test" you need to receive the int you are passing it.

        If you want to pass the entire class object of the foo named 'f' then that is what you pass in line 3 and receive in line 5.

        Code:
        foo f = new foo();
        f.number  = 13;
        test([B]f[/B]);
         
        void test( foo  [B]IncomingFoo [/B])
        {
        Once you have an entire class instead of just an int from inside it, you can study it and pick apart its properties all you like. But just passing the int won't give your method any knowledge of its source, for you to do that with.

        Comment

        Working...