Click here to Skip to main content
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 1:52am
Edited 31-Oct-12 2:21am
ridoy52K
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 OriginalGriff 230
1 PIEBALDconsult 150
2 DamithSL 125
3 Andreas Gieriet 90
4 Jochen Arndt 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2014
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