Solved

C#

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

691 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