Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C#

Posted on 2012-04-09
4
209 Views
Last Modified: 2012-04-09
I am having function calls like below,


string LstrCurrentVP = string.Empty;
string LstrPriorVP = string.Empty;
LstrCurrentVP = BuildVP1Str(LobjCurrentVehiclePremiumsList, LstrCurrentVP, "Current");
LstrPriorVP = BuildVP1Str(LobjPriorVehiclePremiumsList, LstrPriorVP, "Prior");

string LstrCurrentVP = string.Empty;
string LstrPriorVP = string.Empty;
LstrCurrentVP = BuildVP2Str(LobjCurrentVehiclePremiumsList, LstrCurrentVP, "Current");
LstrPriorVP = BuildVP2Str(LobjPriorVehiclePremiumsList, LstrPriorVP, "Prior");

string LstrCurrentVP = string.Empty;
string LstrPriorVP = string.Empty;
LstrCurrentVP = BuildVP3Str(LobjCurrentVehiclePremiumsList, LstrCurrentVP, "Current");
LstrPriorVP = BuildVP3Str(LobjPriorVehiclePremiumsList, LstrPriorVP, "Prior");

I have methods like, BuildVP1Str, BuildVP2Str, BuildVP3Str ......BuildVP16Str.

Is there is any way, to call the method dynamically? I mean, since I have the method as 1, 2 etc...

Is it possible to dynamically frame method names and call inside the loop?

for eg,

for(int1=o;i<=16;i++)
{
string LstrCurrentVP = string.Empty;
string LstrPriorVP = string.Empty;
LstrCurrentVP = "BuildVP" + 1 + "Str"(LobjCurrentVehiclePremiumsList, LstrCurrentVP, "Current");
LstrPriorVP = "BuildVP" + 1 + "Str"(LobjPriorVehiclePremiumsList, LstrPriorVP, "Prior");
}


Since I am using vs 2005, i can't pass function names as parameters.

I thought of delegates but i don't hink for my scenario, I can go for it.

I know, I can't call a function (or use function name) like this "BuildVP" + 1 + "Str"(),

I just want to be clear that I want to do something of that sort. Is ther is any concepts for this in VS 2005.

Please help.
0
Comment
Question by:RameshLathu
  • 2
  • 2
4 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37824616
It could be something like this using Reflection.  BTW, it assumes all BuildVP methods are on the same class and they are all public.
for (int i = 1; i <= 16; i++)
{
    string LstrCurrentVP = string.Empty;
    string LstrPriorVP = string.Empty;

    MethodInfo buildVP = this.GetType().GetMethod("BuildVP" + i + "Str");

    LstrCurrentVP = (string)buildVP.Invoke(this, new object[] { LobjCurrentVehiclePremiumsList, LstrCurrentVP, "Current" });
    LstrPriorVP = (string)buildVP.Invoke(this, new object[] { LobjCurrentVehiclePremiumsList, LstrCurrentVP, "Prior" });
}

Open in new window

0
 

Author Comment

by:RameshLathu
ID: 37824978
Thankyou....so much....It's working fine.

I have another quesry related to this, in the same for loop,
I want to concat both string and assing to one variable.
Again, I have 16 variables of "Ref" type, (Like  ref string PstrVP1, ref string PstrVP2, ref string PstrVP3)

  "PstrVP" + i = LstrCurrentVP + LstrPriorVP;


   for (int i = 1; i <= 16; i++)
                {
                    string LstrCurrentVP = string.Empty;
                    string LstrPriorVP = string.Empty;

                    MethodInfo buildVP = this.GetType().GetMethod("BuildVP" + i + "Str");

                    LstrCurrentVP = (string)buildVP.Invoke(this, new object[] { LobjCurrentVehiclePremiumsList, LstrCurrentVP, "Current" });
                    LstrPriorVP = (string)buildVP.Invoke(this, new object[] { LobjCurrentVehiclePremiumsList, LstrCurrentVP, "Prior" });

                    "PstrVP" + i = LstrCurrentVP + LstrPriorVP;

                   
                }

Please help...
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 37825038
It's best to create an array in this case.
string[] PstrVP = new string[17];

for (int i = 1; i <= 16; i++)
{
    string LstrCurrentVP = string.Empty;
    string LstrPriorVP = string.Empty;

    MethodInfo buildVP = this.GetType().GetMethod("BuildVP" + i + "Str");

    LstrCurrentVP = (string)buildVP.Invoke(this, new object[] { LobjCurrentVehiclePremiumsList, LstrCurrentVP, "Current" });
    LstrPriorVP = (string)buildVP.Invoke(this, new object[] { LobjCurrentVehiclePremiumsList, LstrCurrentVP, "Prior" });

    PstrVP[i] = LstrCurrentVP + LstrPriorVP;
}

Open in new window


Or
string[] PstrVP = new string[16];

for (int i = 1; i <= 16; i++)
{
    string LstrCurrentVP = string.Empty;
    string LstrPriorVP = string.Empty;

    MethodInfo buildVP = this.GetType().GetMethod("BuildVP" + i + "Str");

    LstrCurrentVP = (string)buildVP.Invoke(this, new object[] { LobjCurrentVehiclePremiumsList, LstrCurrentVP, "Current" });
    LstrPriorVP = (string)buildVP.Invoke(this, new object[] { LobjCurrentVehiclePremiumsList, LstrCurrentVP, "Prior" });

    PstrVP[i - 1] = LstrCurrentVP + LstrPriorVP;
}

Open in new window

0
 

Author Closing Comment

by:RameshLathu
ID: 37825062
Very Good Solution for my scenario....
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question