Solved

C#

Posted on 2012-04-09
5
236 Views
Last Modified: 2012-05-31
Hi Experts,

I have methods of same kind, which accept same type of parameters but inside the function it should get different html string and assign value.

Like this I have 16 fundtions, so I need to call the each and every function 16 times as given below,

Is there is any other concept, using which, I can have single line of statement to call the 16 methods.
(I mean, dynamically call 16 methods by using less number of lines).

I thought of delegate, but i don't think so, i can go for it, I need mulicaste delegate, but all my methods retung string.

Please suggesst.
//The below will repeat 16 times....want to avoid that..
string LstrPriorVP1 = string.Empty;
string LstrPriorVP2 = string.Empty;

string LstrCurrentVP1 = string.Empty;
string LstrCurrentVP2 = string.Empty;

LstrCurrentVP1 = BuildVP1Str(LobjCurrentVehiclePremiumsList, LstrCurrentVP1, "Current");
LstrPriorVP1 = BuildVP1Str(LobjPriorVehiclePremiumsList, LstrPriorVP1, "Prior");
PstrVP1 = LstrCurrentVP1 + LstrPriorVP1;

LstrCurrentVP2 = BuildVP2Str(LobjCurrentVehiclePremiumsList, LstrCurrentVP2, "Current");
LstrPriorVP2 = BuildVP2Str(LobjPriorVehiclePremiumsList, LstrPriorVP2, "Prior");                
PstrVP2 = LstrCurrentVP2 + LstrPriorVP2;

 private string BuildVP1Str(ArrayList PobjVPList, string PstrVP, string PstrPolicyInfoType)
        {
            int LiVehicleInfoResultID = 0;
            foreach (VehiclePremiums LobjVP in PobjVPList)
            {
               
                string LstrVP = HTMLResource.VehiclePremiumSection1;              
               
                LstrVP = LstrVP.Replace("VP_PolicyInfoTypeValue", PstrPolicyInfoType);
               
                LstrVP = LstrVP.Replace("VP_LiabilityPremiumValue", LobjVP.LiabilityPremium.ToString());
                .....

                PstrVP = PstrVP + LstrVP;
            }
            return PstrVP;
        }

   private string BuildVP2Str(ArrayList PobjVPList, string PstrVP, string PstrPolicyInfoType)
        {

            foreach (VehiclePremiums LobjVP in PobjVPList)
            {
             
                string LstrVP = HTMLResource.VehiclePremiumSection2;
             
                LstrVP = LstrVP.Replace("VP_PolicyInfoTypeValue", PstrPolicyInfoType);

                LstrVP = LstrVP.Replace("VP_ComprehensivePremiumValue", LobjVP.ComprehensivePremium.ToString());

                ...........

                PstrVP = PstrVP + LstrVP;
            }
            return PstrVP;
        }


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
  • 3
5 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 37823904
i changed the function to get the vehiclePremiumSection as parameter cause it differ for each one, also the replace string of "Current" and "Prior" is done at the same run inside the function one after the other and returns a concatenated string result.

   private string BuildVPStr(ArrayList PobjVPList, string vehiclePremiumSection)
        {
String result=String.Empty;
  foreach (VehiclePremiums LobjVP in PobjVPList)
            {
            result+=vehiclePremiumSection.Replace("VP_PolicyInfoTypeValue", "Current").Replace("VP_ComprehensivePremiumValue", LobjVP.ComprehensivePremium.ToString());


            }

  foreach (VehiclePremiums LobjVP in PobjVPList)
            {
            result+=vehiclePremiumSection.Replace("VP_PolicyInfoTypeValue", "Prior").Replace("VP_ComprehensivePremiumValue", LobjVP.ComprehensivePremium.ToString());


            }
            return result;
       }

Open in new window


instead of

LstrCurrentVP1 = BuildVP1Str(LobjCurrentVehiclePremiumsList, LstrCurrentVP1, "Current");
LstrPriorVP1 = BuildVP1Str(LobjPriorVehiclePremiumsList, LstrPriorVP1, "Prior");
PstrVP1 = LstrCurrentVP1 + LstrPriorVP1;

LstrCurrentVP2 = BuildVP2Str(LobjCurrentVehiclePremiumsList, LstrCurrentVP2, "Current");
LstrPriorVP2 = BuildVP2Str(LobjPriorVehiclePremiumsList, LstrPriorVP2, "Prior");                
PstrVP2 = LstrCurrentVP2 + LstrPriorVP2;

using the following approach u still need to call 16 times to this function, each time with different vehiclePremiumSection:
string vp1 = BuildVPStr(LobjCurrentVehiclePremiumsList, HTMLResource.VehiclePremiumSection1);
string vp2 = BuildVPStr(LobjCurrentVehiclePremiumsList, HTMLResource.VehiclePremiumSection2);

Open in new window


you could use reflection to get all VehiclePremiumSection properties from HTMLResource and for each one, call the function and pass the value of the parameter:

foreach (PropertyInfo info in HTMLResource.GetType().GetProperties())
{
   if (info.CanRead && info.Name.Contains("VehiclePremiumSection"))
   {
      string vehiclePremiumSection = propertyInfo.GetValue(myObject, null).ToString();
//now call the function with this parameter
string result = BuildVPStr(LobjCurrentVehiclePremiumsList, vehiclePremiumSection);

   }
} 

Open in new window

0
 

Author Comment

by:RameshLathu
ID: 37824255
Thanks for quick reply. But, for all pror and current function call, I won't be having same line of code in the method. I have 16 html vehiclepremium string and I have 16 build str methods. But each method is replacing different strings. Please check below. Ofcourse, every method will be called, twice one for "Prior" and one for "Current". But 16 different methods will be there.

For eg, it will like below,

 private string BuildVP1Str(ArrayList PobjVPList, string PstrVP, string PstrPolicyInfoType)
        {            
            int LiVehicleInfoResultID = 0;
            foreach (VehiclePremiums LobjVP in PobjVPList)
            {
                LiVehicleInfoResultID++;                
                string LstrVP = HTMLResource.VehiclePremiumSection1;
                //LstrVP = LstrVP.Replace("VP_VehicleInfoResultIDValue", LobjVP.VehicleInfoResultID.ToString());
                LstrVP = LstrVP.Replace("VP_VehicleInfoResultIDValue", LiVehicleInfoResultID.ToString());
                LstrVP = LstrVP.Replace("VP_PolicyInfoTypeValue", PstrPolicyInfoType);
                LstrVP = LstrVP.Replace("VP_VehicleSequenceNumberValue", LobjVP.VehicleSequenceNumber);
                LstrVP = LstrVP.Replace("VP_VehicleNumberValue", LobjVP.VehicleNumber.ToString());
                LstrVP = LstrVP.Replace("VP_VehicleClassValue", LobjVP.VehicleClass);
                LstrVP = LstrVP.Replace("VP_LiabilityPremiumValue", LobjVP.LiabilityPremium.ToString());
                LstrVP = LstrVP.Replace("VP_MedicalPaymentsPremiumValue", LobjVP.MedicalPaymentsPremium.ToString());
                LstrVP = LstrVP.Replace("VP_UMUIMPremiumValue", LobjVP.UMUIMPremium.ToString());
                LstrVP = LstrVP.Replace("VP_PIPPremiumValue", LobjVP.PIPPremium.ToString());
                LstrVP = LstrVP.Replace("VP_PIPHigherLimitsPremiumValue", LobjVP.PIPHigherLimitsPremium.ToString());

                PstrVP = PstrVP + LstrVP;          

            }

            return PstrVP;
        }

 private string BuildVP2Str(ArrayList PobjVPList, string PstrVP, string PstrPolicyInfoType)
        {
            int LiVehicleInfoResultID = 0;
            foreach (VehiclePremiums LobjVP in PobjVPList)
            {
                LiVehicleInfoResultID++;
                string LstrVP = HTMLResource.VehiclePremiumSection2;
                LstrVP = LstrVP.Replace("VP_VehicleInfoResultIDValue", LiVehicleInfoResultID.ToString());
                LstrVP = LstrVP.Replace("VP_PolicyInfoTypeValue", PstrPolicyInfoType);
                LstrVP = LstrVP.Replace("VP_VehicleSequenceNumberValue", LobjVP.VehicleSequenceNumber);
                LstrVP = LstrVP.Replace("VP_VehicleNumberValue", LobjVP.VehicleNumber.ToString());

                LstrVP = LstrVP.Replace("VP_ComprehensivePremiumValue", LobjVP.ComprehensivePremium.ToString());
                LstrVP = LstrVP.Replace("VP_CollisionPremiumValue", LobjVP.CollisionPremium.ToString());
                LstrVP = LstrVP.Replace("VP_IncreasedTEPremiumValue", LobjVP.IncreasedTEPremium.ToString());
                LstrVP = LstrVP.Replace("VP_AudioVisualPremiumValue", LobjVP.AudioVisualPremium.ToString());
                LstrVP = LstrVP.Replace("VP_TowingPremiumValue", LobjVP.TowingPremium.ToString());
                LstrVP = LstrVP.Replace("VP_VehiclePremiumValue", LobjVP.VehiclePremium.ToString());

                PstrVP = PstrVP + LstrVP;
            }
            return PstrVP;
        }
0
 

Author Comment

by:RameshLathu
ID: 37824274
and thid Buildstr method will be as below,

  private string BuildVP3Str(ArrayList PobjVPList, string PstrVP, string PstrPolicyInfoType)
        {
            int LiVehicleInfoResultID = 0;
            foreach (VehiclePremiums LobjVP in PobjVPList)
            {
                LiVehicleInfoResultID++;
                string LstrVP = HTMLResource.VehiclePremiumSection3;
                LstrVP = LstrVP.Replace("VP_VehicleInfoResultIDValue", LiVehicleInfoResultID.ToString());
                LstrVP = LstrVP.Replace("VP_PolicyInfoTypeValue", PstrPolicyInfoType);
                LstrVP = LstrVP.Replace("VP_VehicleSequenceNumberValue", LobjVP.VehicleSequenceNumber);
                LstrVP = LstrVP.Replace("VP_VehicleNumberValue", LobjVP.VehicleNumber.ToString());

                LstrVP = LstrVP.Replace("VP_VehicleRankValue", LobjVP.VehicleRank.ToString());
                LstrVP = LstrVP.Replace("VP_ClassicModelYearValue", LobjVP.ClassicModelYear.ToString());
                LstrVP = LstrVP.Replace("VP_AssignedDriverNumberValue", LobjVP.AssignedDriverNumber.ToString());
                LstrVP = LstrVP.Replace("VP_VehicleClassDescriptionValue", LobjVP.VehicleClassDescription);
                LstrVP = LstrVP.Replace("VP_AssignedDDDriverNumberValue", LobjVP.AssignedDDDriverNumber.ToString());

                PstrVP = PstrVP + LstrVP;
            }
            return PstrVP;
        }


and in the same, I will be having different set of code for each method.
0
 

Author Comment

by:RameshLathu
ID: 38031879
Thanks
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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