Solved

C#

Posted on 2012-04-09
4
206 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Very Good Solution for my scenario....
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Dynamically create stored procedure   using c# 9 71
asp.net bundle 8 32
Pass through dll 2 33
JavaScript error 1 32
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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now