Solved

C#

Posted on 2012-04-09
4
208 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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