Click here to Skip to main content
11,642,216 members (62,962 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
public static void PrintRecord(ref Student std)
        {
string[] tname = { "Name : ", "Gender : ", "Age : ", "Marks : ", "Standard : ", "Divison : " };
            string [] fname={"std.GetName()", "std.GetGender()", "std.GetAge()", "std.GetMarks()", "std.GetStandard()", "std.GetDivision"};
            string funName;
            for (int index = 0; index < tname.Length; ++index)
            {
                funName=fname[index];
                Console.WriteLine(tname[index] + ":" + std.GetName());
            }
        }

Output :
Name : std.GetName();
Gender : std.GetGender(); ...

In above output ... these functions are directly printing .. I dont want this , i need to executed these function instead of this output.
Please show me way.....
Thanking you.
Posted 31-Oct-12 0:52am
Edited 31-Oct-12 1:21am
ridoy53.4K
v3
Comments
Sergey Alexandrovich Kryukov at 9-Apr-13 11:38am
   
Nothing like "function" is listed in an array.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The "functions" cannot really be listed in an array. You list the names of the functions, but this is really, really bad approach (and Solution 1 is a really bad answer). It is not maintainable. Think by yourself: should you misspell the name, a compiler would not be able to detect the problem. Should you rename the function, you will immediately break the functionality, and the compiler errors/warning which could help you to restore the consistency, would never appear. Such approaches are the real disaster.

At the same time, there is an apparent and really neat solution. You should store delegate instances is your array or some collection. I've developed a whole robust technique, which is generic and highly universal. Please see my CodeProject article:
Dynamic Method Dispatcher[^].

—SA
  Permalink  
v2
Comments
Maciej Los at 9-Apr-13 11:57am
   
+5 and +5 for article ;)
Sergey Alexandrovich Kryukov at 9-Apr-13 13:43pm
   
Thank you very much, Maciej.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please try to change
string [] fname={"std.GetName()", "std.GetGender()", "std.GetAge()", "std.GetMarks()", "std.GetStandard()", "std.GetDivision"};

as
string [] fname={std.GetName(), std.GetGender(), std.GetAge(), std.GetMarks(), std.GetStandard(), std.GetDivision};

and if your method not returning string value then please cast it into string

I have tried with your code

public  void PrintRecord(ref Student std)
        {
            string[] tname = { "Name : ", "Gender : ","Age : "};
            string[] fname = { std.GetName(), std.GetGender(), std.GetAge().ToString() };
            string funName;
            for (int index = 0; index < tname.Length; ++index)
            {
                funName = fname[index];
                Response.Write(tname[index] + ":" + funName);
            }
            }

demo student class

public class Student
    {
        public string GetName()
        {
            return "soumen";
        }
 
        internal string GetGender()
        {
            return "M";
        }
 
        internal int GetAge()
        {
            return 32;
        }
    }

The output is
Name : :soumen Gender : :M Age : :32

Is that the output you want to display or please explain
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 240
1 Sergey Alexandrovich Kryukov 184
2 Afzaal Ahmad Zeeshan 139
3 OriginalGriff 135
4 Mika Wendelius 80
0 Mika Wendelius 460
1 DamithSL 443
2 OriginalGriff 340
3 Afzaal Ahmad Zeeshan 228
4 Sergey Alexandrovich Kryukov 179


Advertise | Privacy | Mobile
Web01 | 2.8.150731.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100