如何调用返回列表的函数? [英] How do I call a function that is returning a list?

查看:100
本文介绍了如何调用返回列表的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





这是我的方法,它在列表中返回3个值并且工作正常:



class A.

Hi,

This is my method which returns 3 values in a list and is working fine:

class A.

public static List<string> ABC()  {
  return  _lstValues;
 }







从button_click上的windows表单中,我试图调用ABC( ):




From windows form on button_click ,I am trying to call the ABC() :

List<string> ListValues = ClassA.ABC();





但我收到错误:



But I am getting error:

ex = {"The type initializer threw an exception."}





任何人都可以帮我解决这个问题。我是否以正确的方式调用该函数?





谢谢。



Can anyone please help me to fix this. Am I calling the function in a correct way?


Thanks.

推荐答案

也许你必须在调用它之前初始化_lstValues:



Maybe you have to initialize _lstValues before call it like :

private List<string> _lstValues = new List<string>();





填写它您将使用的值并注意静态关键字,在这种情况下可能会产生副作用。

希望帮助。



fill it with the values you will use and pay attenction to static keyword , in this context could have side effect.
Hope help.


我对您的代码稍作修改,想出了这个小提琴。 https://dotnetfiddle.net/ylc2Dx [ ^ ]



代码现在,



I made a little changes to your code, and came up with this fiddle. https://dotnetfiddle.net/ylc2Dx[^]

The code was now,

// create the method, that returns List<string>
public static List<string> ABC()  {
  // initialize new list, and return that list
  List<string> list = new List<string>();
  // add three values
  list.Add("First value");
  list.Add("Second value");
  list.Add("Third value");
  // return the list
  return list;
}

// upon calling, it will return that instance of the list 
// created inside the method
List<string> newList = ABC();

// check the Type of the object, it is "System.Collections.Generic.List`1[System.String]"
Console.WriteLine(newList.ToString());





现在可以确保创建新对象,添加三个值,然后返回列表,顺便说一句,为什么要以这种方式返回一个对象,而这样就很容易写出来方式





This now ensures that your new object will be created, three values would be added, then the list would be returned, and by the way, why are you returning an object, in this way, whereas it will be easy to write it this way

List<string> list = new List<string>();
// add three (or more; or less) values to it here, using .Add() method





这个兄弟背后的具体原因是什么?



Any specific reason behind this brother?


1 down vote





如果我理解你的要求,你必须做到以下几点:

1 down vote


If I understand your request correctly, you have to do the following:
public class Section 
{ 
    public String Head
    {
        get
        {
            return SubHead.LastOrDefault();
        }
        set
        {
            SubHead.Add(value);
        }

    public List<string> SubHead { get; set; }
    public List<string> Content { get; set; }
} 



您可以这样使用它:


You use it like this:

var section = new Section();
section.Head = "Test string";



现在,测试字符串被添加到subHeads集合中,并将通过getter提供:




Now "Test string" is added to the subHeads collection and will be available through the getter:

var last = section.Head; // last will be "Test string"



希望我理解正确。


Hope I understood you correctly.


这篇关于如何调用返回列表的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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