将变量传递给另一种方法

时间:2020-05-12 08:38:37

标签: c#

我又在这里再次提出有关C#基础知识的愚蠢问题。我喜欢在创建自己感兴趣的东西时学习,也许这就是问题所在。

在这里,我尝试将int变量CompanySearchIDSearchForCompanyAndCreateIfNotFound转换为FilesAction。所以

我正在为CompanySearchID获取正确的值,但是如何将其传递给另一种方法呢?我已经看过一些例子,但是没有一个有帮助。例如:C# referencing a variable from another method。 使用该问题的示例,我得到了指向FilesAction(MCountry, MTopicShort, MCompany, MDeadline);的错误,并且我不知道如何将所有三种方法连接在一起。有人可以给我提示吗?

这是我的代码(注意!注释“而不是4应该是CompanySearchID”):

    static void Main(string[] args)
    {

                    ...                        

                    FilesAction(MCountry, MTopicShort, MCompany, MDeadline);

                    ...
        }
    }

   public static void FilesAction(string MCountry, string MTopicShort, string MCompany, DateTime MDeadline)
    {

                ...

                SearchForCompanyAndCreateIfNotFound(MCountry, MCompany);
                // I need to get CompanySearchID variable from below method SearchForCompanyAndCreateIfNotFound here so I can use it later in this method
                int total;
                total = CompanySearchID + 6;
                Console.WriteLine(total);
                // So I should get 12 in Console
                ...

            }
    }

    public static int SearchForCompanyAndCreateIfNotFound(string MCountry, string MCompany)
    {
            ...
            // I am performing some actions here and as an ouput I am getting a number to CompanySearchID
            int CompanySearchID = 6;
            return CompanySearchID;

            ...
        }
    }

1 个答案:

答案 0 :(得分:3)

您的意思是保存方法调用的返回值吗?

//[Route("[connote]")] < --remove route and call your method GET to verride httpget .. it will match the api/Consignments [GET]
    //[HttpGet]
    public async Task<ActionResult<IEnumerable<ConsignmentNote>>> Get()
    {
        var connote = await _context.Consignment
            .Include(x => x.ConsignmentNote)
            .ToListAsync();
        return connote.Select(xx=>xx.ConsignmentNote).ToList();
    }