C#将值从方法传递到方法 [英] C# pass values from a method to a method

查看:69
本文介绍了C#将值从方法传递到方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我想做的是将方法1"public double computeAmout(int age,字符串policyTypeId)"中的值返回到另一个方法"public string startQuote"中.

因此,当前"public string startQuote"要求用户在调用它时输入所有数据

我想做的是对"age"和"policyTypeId"变量进行计算,然后将值返回到"public string startQuote"中的"double amount"变量.

这些方法正在使用其他对象,但是它们运行正常

这是用于保险公司的项目(如果有兴趣的话)

我的代码在
下面

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;

        }



我知道应该为公共double computeAmout方法使用一个case语句...但这仍在构建中:P

cheers



i know a case statement should be used for the public double calculateAmout method ... but this is still being built :P

cheers

推荐答案

您不会将方法中的值返回给方法.您向调用方返回一个值,该值类似于赋值运算符:
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);


其中SomeTypeSomeMethod的返回类型.
另一种方法是out参数:


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



如果看起来您几乎需要从一开始就阅读C#和编程,那么它对您有很大的帮助.

您的代码有太多问题.硬编码的立即常量(例如"PT + 1001","PT + 1002",25、2000,成功")使代码实际上不被支持,设计是非常特殊的,而不是",string.Empty应该使用,参数太多,全部是公共的(为什么),没有OOP的迹象…

—SA



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


那是什么问题?我看不到.

您只需要像这样:
So what''s the problem? I can''t see it.

All you need to is something like:
double output = calculateAmount(age, policyTypeId);

现在,它通常使用Pascal大小写作为方法名称的方法,并且您在calculateAmount中对getPolicyType进行了多余的调用.

Now, it''s normal practice to use Pascal casing for method names, and you have a redundant call to getPolicyType in calculateAmount.


我想做的是

在公共字符串startQuote"中使用"age"和"policyTypeId"变量.

以及他们拥有的值...使用该值并进行计算,然后将结果返回两倍"

getPolicyType返回字符串policyTypeId

就像这样

如果您的年龄超过25岁,并选择了证书政策 ...,这将使您花费x 金额美元

那就是我想做的事
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


这篇关于C#将值从方法传递到方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆