Solved

C#

Posted on 2012-04-09
4
210 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
[X]
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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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