Solved

C#

Posted on 2012-04-09
5
230 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

911 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

19 Experts available now in Live!

Get 1:1 Help Now