Click here to Skip to main content
12,949,858 members (93,826 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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";
                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

Posted 25-Apr-12 5:33am
Rate this: bad
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…

sg_90 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 25-Apr-12 12:31pm
Not an excuse for hard-coding them...
Rate this: bad
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.
Rate this: bad
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

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,489
CHill60 3,380
Maciej Los 2,913
Jochen Arndt 1,935
ppolymorphe 1,820

Advertise | Privacy | Mobile
Web02 | 2.8.170524.1 | Last Updated 25 Apr 2012
Copyright © CodeProject, 1999-2017
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