一个表单中的多个控件数组

时间:2017-08-26 23:11:42

标签: arrays vb.net

我有一个名为lblTables的数组,其中包含21个我的表单。这完全没问题。它的代码如下:

Public lblTables() As Label
Public Sub New()
    InitializeComponent()
    lblTables = New Label() {Label2, table1, table2, table3, table4, table5, table6, table7, table8, table9, table10,
    table11, table12, table13, table14, table15, table16, table17, table18, table19, table20}
End Sub

我需要创建另一个名为lblStartTimes的数组,它将包含另外21个标签。所以我继续如下:

Private lblStartTimes() As Label
Public Sub New()
    InitializeComponent()
    lblStartTimes = New Label() {startTime1, startTime2, startTime3, startTime4, startTime5, startTime6,
    startTime7, startTime8, startTime9, startTime10, startTime11, startTime12, startTime13, startTime14,
    startTime15, startTime16, startTime17, startTime18, startTime19, startTime20}
End Sub

但是当我试图这样做时,我收到一条错误Public Sub New() has multiple definitions with identical signatures。因此,在进行研究后,我发现最简单的方法如下:

Public lblStartTimes() As Label = {Label3, startTime1, startTime2, startTime3, startTime4, startTime5, startTime6,
    startTime7, startTime8, startTime9, startTime10, startTime11, startTime12, startTime13, startTime14,
    startTime15, startTime16, startTime17, startTime18, startTime19, startTime20}

但是当我使用这种方法时,我不断收到System.NullReferenceException错误。我需要声明lblStartTimes与我声明lblTables数组相同的OR方式,而不会出现多个定义错误。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

与评论中已经建议的一样,允许在构造函数中写入多行。只需将第二个数组初始化代码移动到第一个构造函数:

// Your array
$inputArray = array(
    "msgName"=>
    array(0=> "John",
        1=> "Mike",
        2=> "Roger",
        3=> "Andrew",
        ),
    "msgContent"=>
    array(
        0=> "Something that john said goes here",
        1=> "Something that mike said goes here",
        2=> "Something that roger said goes here",
        3=> "Something that andrew said goes here"
    ),
    "Leader"=> "John"
);

// Create the file
$output = fopen("example.js","w");
// a foreach to loop through
foreach($inputArray["msgName"] as $index=>$name)
{
    // We start adding the content
    // You may notice I do not concatinate strings. That is because it uses a lot of memory!
    fwrite($output,"},
'");
    fwrite($output, $index);
    fwrite($output,"': {
    'text': \"<p><div class=\\\"");
    // The if else for the leader
    if ($inputArray["Leader"] == $name)
    {
        fwrite($output,"user gang p1");
    }
    else
    {
        fwrite($output,"person gang p2");
    }

    fwrite($output, "\\\"><strong>");
    fwrite($output, $name);
    fwrite($output, "</strong><br>\\n");
    fwrite($output, $inputArray["msgContent"][$index]);
    fwrite($output, "</div>\\n<br></p>\",
    'passages': {
    },
");
}
// Be sure to close the file
fclose($output);