C#如何获取方法的返回值并将其放入变量中

时间:2018-06-10 02:49:36

标签: c#

我是C#的新手,我正在尝试通过解决问题来学习。我有一个名为StatusController.Check()的方法,该方法将返回一个名为EnumStatus的枚举和一个名为VehicleIdentifier的整数。

该方法工作正常,我现在在另一个类中调用该方法,并想知道如何在HomeController.Main() ActionResult中获取EnumStatus和VehicleIdentifier的返回值,以便我可以将其放入分成两个独立的变量。

namespace MyHome.Controllers 
{
    public class HomeController
    {
        public ActionResult Main()
        {
            var getStatus = new StatusController();
            getStatus.Check("V2Yq7"); 

            // How can I get the return value of the Check method
            // It will return an Enum of type string and an Integer called
            // VehicleIdentifier
            // I would like to put that values inside the variables below .

            var EnumStatus = "";
            var VehicleIdentifier = ;       
        }   
    }
}  


namespace BTree.Controllers
{
    public class StatusController : AllStatusController
    {
        [HttpPost]
        public Models.StatusCheck Check(string uniqueID)
        {
            var statusRepository = new Repository.StatusRepository<StatusModel>();
            var myModel = statusRepository.GetStatus(uniqueID);

            if (myModel.CurrentStatus == EnumStatus.Sold)
            {
                var VehicleRepository = new VehicleRepository();
                var getVehicle = VehicleRepository.GetVehicle(myModel.MakeID, myModel.IDType);
                return new Models.StatusCheck
                {
                    EnumStatus = EnumStatus.Sold,
                    VehicleIdentifier = getVehicle(Make, uniqueID)
                };
            }
            else
            {
                return new Models.StatusCheck { EnumStatus = myModel.EnumStatus };
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您只需将结果分配给另一个变量,然后引用该变量实例的属性:

CSVPrinter printer = CSVFormat.RFC4180.withHeader( rs ).print( writer ); // Write header row, using database column header names.
printer.printRecords( rs ); // Write each row of the entire result set.

答案 1 :(得分:1)

尝试创建一个类..

项目&gt;&gt;添加班级

//set your class to public here
public class yourclassname
{
    public static string getStatus
    {
        get;
        set;
    }
    public static string vehicleIdentifier
    {
        get;
        set;
    }
}

然后只需将状态保存到您的班级以保存值...

yourclassname.getStatus = EnumStatus.Sold; //if it is sold
yourclassname.vehicleIdentifier = getVehicle(Make, uniqueID);

获取或使用这些值只需简单地调用它们

var status = yourclassname.getStatus;
var vehicleIdent = yourclassname.vehicleIdentifier;

希望它有所帮助..