将列表转换为字符串数组 [英] Convert List to String array

查看:78
本文介绍了将列表转换为字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有这些物体

Hi,

I have these objects

public class OutputField
   {
       public string[] inputFieldValue;
   }

   public class OutputGroups
   {
       public OutputField input1 = new OutputField();
       public OutputField input2= new OutputField();
      
   }



和带有2个复选框的按钮.



and a button with 2 checkboxes.

private void btnSelect_Click(object sender, EventArgs e)
        {
            try
            {
                List<OutputGroups> _OutputToList = new List<OutputGroups>();

                OutputGroups _1Output = new OutputGroups();

                fileName = parameters.directory;
                sheetNumber = cboSelectWorksheet.SelectedIndex + 1;
                string[] strInput1 = { "Input Address", "Input City", "Input State", "Input Zip" };
                string[] strInput2= { "Indicator", "Return" };
               
              
                if (checkbox1.Checked)
                {                 
                    _1Output.input.inputFieldValue = strInput;
                    _OutputToList.Add(_1Output);                   
                }
                if (checkbox2.Checked)
                {
                    _1Output.LACSLink.inputFieldValue = strLACSLink;
                    _OutputToList.Add(_1Output);   
                }

                string[] allOutput = { };
                getData.AppendInputColumns(fileName, sheetNumber, allOutput);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }       
    }



当我选中checkbox1时,来自strInput1的所有字符串值都将添加到_OutputToList.Add(_1Output).如果我选中2个框,则来自strInput1和strInput2的字符串值将添加到


我该如何放置List< outputgroups>中的所有字符串? _OutputToList =新列表< outputgroups>()
到此字符串数组
string [] allOutput = {??????????};

谢谢,



When i check the checkbox1, all string value from strInput1 will be added to _OutputToList.Add(_1Output). If I check 2 boxes, string values from strInput1 and strInput2 will be added to


How Can i put the all string from List<outputgroups> _OutputToList = new List<outputgroups>()
to this string array
string[] allOutput ={??????????};

thanks,

推荐答案

创建System.Collections.Generic.List<string></string>的实例,将所有字符串一一添加,然后将string[]作为System.Collections.Generic.List<string>.ToArray()</string>返回. >
或者,如果预先知道字符串数组的长度(假设它是int count = _OutputToList.Count),则将其全部直接输出到字符串数组:
Create an instance of System.Collections.Generic.List<string></string>, add all strings one by one to it, and then return string[] as System.Collections.Generic.List<string>.ToArray()</string>.

Alternatively, if the length of the string array is known in advance (let''s say, it is int count = _OutputToList.Count), output it all directly to the string array:
string[] stringArray = new string[count];
for (int index = 0; index < count; ++index)
   // copy elements as requred ...



保持简单,我建议. ;-)

—SA



Keep it simple, I would advise. ;-)

—SA


这篇关于将列表转换为字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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