Use funtion point to condense your "switch"
We usually use "switch" to solve the problem when we're developing an application and need to choose the plural options.
But your "switch" would become very long when you modified codes or added some options after a lot of times.
Especially your options of "switch" were included.
I have a solution to condense you codes.
Maybe you can try it.
Original.
#include "stdafx.h" extern void AppPause(); const int METHOD_MAX = 3; void Method1(int iParam) { ::printf("\n%s = %d","Method1", iParam); } void Method2(int iParam) { ::printf("\n%s = %d","Method2", iParam); } void Method3(int iParam) { ::printf("\n%s = %d","Method3", iParam); } int _tmain(int argc, _TCHAR* argv[]) { for(int i=0; i<METHOD_MAX; i++) { switch(i) { case 0: ::Method1(i); break; case 1: ::Method2(i); break; case 2: ::Method3(i); break; } } ::AppPause(); return 0; } void AppPause() { char cPause; do { cPause = getchar(); if(cPause == EOF) break; } while(cPause != '\n'); }
Used a function point.
#include "stdafx.h" extern void AppPause(); typedef void (*pfnMethod)(int iParam); const int METHOD_MAX = 3; void Method1(int iParam) { ::printf("\n%s = %d","Method1", iParam); } void Method2(int iParam) { ::printf("\n%s = %d","Method2", iParam); } void Method3(int iParam) { ::printf("\n%s = %d","Method3", iParam); } pfnMethod arr_pfnMethod[] = {Method1, Method2, Method3}; int _tmain(int argc, _TCHAR* argv[]) { for(int i=0; i<METHOD_MAX; i++) arr_pfnMethod[i](i); ::AppPause(); return 0; } void AppPause() { char cPause; do { cPause = getchar(); if(cPause == EOF) break; } while(cPause != '\n'); }
A sample in C#.
using System; namespace PointFunctionCS { class Program { private delegate void delMethod(int iParam_); private static delMethod[] arr_delMethod = { new delMethod(Method1), new delMethod(Method2), new delMethod(Method3) }; private static void Main(string[] args) { for (int i = 0; i < arr_delMethod.Length; i++) arr_delMethod[i](i); Console.ReadLine(); } private static void Method1(int iParam_) { Console.WriteLine("Method1 = " + iParam_.ToString()); } private static void Method2(int iParam_) { Console.WriteLine("Method2 = " + iParam_.ToString()); } private static void Method3(int iParam_) { Console.WriteLine("Method3 = " + iParam_.ToString()); }//method }// class }// namespace