Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# function
ok so what im trying to do is return a value from method 1 "public double calculateAmout(int age, string policyTypeId)" to another method "public string startQuote"
 
So currently the "public string startQuote" requires the user to input all data when its called
 
what i want to do is take the 'age' and 'policyTypeId' variables perform a calculation and return a value to the 'double amount' variable in "public string startQuote".
 
these methods are using other objects, but these are functioning correctly
 
this is for an insurance company project (if any one is interested)
 
my code is below
 
 
 
public string startQuote(DateTime quoteDate, string policyTypeId, double amount, DateTime obtainedLicenseDate, int numberOfFines, string carRego, int age, string make, string model, DateTime regoExpiryDate, string additionalAccessories, string carUse)
        {
            
 
            Car newCar = new Car(additionalAccessories,model,regoExpiryDate,make,carRego,carUse);
            string newCarResult = createNewCar(ref newCar);
 
            PolicyType policy = new PolicyType();
            policy = policy.getPolicyType(policyTypeId);
 
            Quote newQuote = new Quote("", quoteDate, policy, amount, obtainedLicenseDate, numberOfFines, newCar, age);
            string newQuoteResult = createNewQuote(ref newQuote);
 

            if (newQuoteResult == "Success" && newCarResult == "Success")
            {
                return "Success";
            }
            else
            {
                return "Fail";
            }
               
 
        }
 

public double calculateAmout(int age, string policyTypeId)
        {
 
            double amount;
 
            PolicyType policy = new PolicyType();
            policy = policy.getPolicyType(policyTypeId);
 

 
            if (policyTypeId == "PT+1001" & age > 25)
            {
                amount = 1500 + 0;
            }
 
            if (policyTypeId == "PT+1002" & age > 25)
            {
                amount = 500;
            }
 
            if (policyTypeId == "PT+1003" & age > 25)
            {
                amount = 1000;
            }
 
            if (policyTypeId == "PT+1001" & age < 25)
            {
                amount = 2500;
            }
 
            if (policyTypeId == "PT+1002" & age < 25)
            {
                amount = 1500;
            }
 
            if (policyTypeId == "PT+1003" & age < 25)
            {
                amount = 2000;
            }
 
            return amount;
 
        }
 

 
i know a case statement should be used for the public double calculateAmout method ... but this is still being built :P
 
cheers
Posted 25-Apr-12 6:33am
sg_90193
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You don't return value from a method to a method. You return a value to a caller, which looks like assignment operator:
SomeType someVariable = SomeMethod(someParameterValue, anotherParameterValue);
where SomeType is a return type of SomeMethod.
 
Another way is out parameters:
SomeType SomeMethod(SomeParameterType byValueParameter, out SomeOtherType byReferenceOutParameter) { /* ... */ }
 
//...
SomeParameterType myInput = //...
SomeOtherType actualByReferenceOutParameter; //not initialized, perfectly OK, but only for out parameters
SomeType returnedValue = SomeMethod(myInput, out actualByReferenceOutParameter);
//at this point, actualByReferenceOutParameter is initialized and a value is assigned
 
If looks like you need to read about C# and programming nearly from the very beginning, it can greatly help you.
 
Your code has too many problems. Hard-coded immediate constants like "PT+1001", "PT+1002", 25, 2000, "Success" makes code practically unsupportable, design is very ad-hoc, instead of "", string.Empty should be used, too many parameters, all is public (why), no signs of OOP…
 
—SA
  Permalink  
Comments
sg_90 at 25-Apr-12 11:54am
   
this is just one class .. its a 3 tier design
 
"PT+1001", "PT+1002" are values in an object that need to be used for this purpose
SAKryukov at 25-Apr-12 12:31pm
   
Not an excuse for hard-coding them...
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

So what's the problem? I can't see it.
 
All you need to is something like:
double output = calculateAmount(age, policyTypeId);
Now, it's normal practice to use Pascal casing for method names, and you have a redundant call to getPolicyType in calculateAmount.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

what i am trying to do is
 
use the 'age' and 'policyTypeId' variables in "public string startQuote".
 
and what ever value they have ... use that value and perform a calculation and return something to 'double amount'
 
getPolicyType returns the string policyTypeId
 
its like this
 
if your age is over 25 and choose a certian policy ... it will cost you x amount of dollars
 
thats what im trying to do
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 406
1 /\jmot 180
2 Suraj Sahoo | Coding Passion 170
3 BillWoodruff 159
4 Afzaal Ahmad Zeeshan 154
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 25 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100