Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
C#
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
Updated 31-Oct-12 1:21am
v3
Comments
Sergey Alexandrovich Kryukov 9-Apr-13 11:38am    
Nothing like "function" is listed in an array.
—SA

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
 
Share this answer
 
v2
Comments
Maciej Los 9-Apr-13 11:57am    
+5 and +5 for article ;)
Sergey Alexandrovich Kryukov 9-Apr-13 13:43pm    
Thank you very much, Maciej.
—SA
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

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

C#
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
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900