3

次のエラーが表示されます。

配列初期化子は、変数またはフィールド初期化子でのみ使用できます。代わりに新しい式を使用してみてください。

これが私のコードです:

// Declare listbox information array
string [] tablet = new string[]{{"Microsoft Surface  Price: $1,162.99  Screen Size: 10.6 Inches  Storage Capacity: 128 GB"},

                                {"iPad 2 Price: $399.99, Screen Size: 9.7 Inches, Storage Capacity 16 GB"},
                                {"Samsung Galaxy Tab 2 Price: $329.99, Screen Size: 10.1 Inches, Storage Capacity 16 GB"},
                                {"NOOK HD Price: $199.99, Screen Size: 7 Inches, Storage Capacity 8 GB"},
                                {"IdeaTab Price: $149.99, Screen Size: 7 Inches, Storage Capacity: 8 GB"}};

//Array of product prices
int [] tabletPricesArray = new int[]{{"$1,162.99"},
                                       {"$399.99"},
                                       {"$329.99"},
                                       {"$199.99"},
                                       {"$149.99"}};

何が問題なのかよくわかりません。私はC#に比較的慣れていません。追加情報が必要な場合はお知らせください。

4

2 に答える 2

5

いくつかの問題:

問題 1:

intここでは、文字列を提供しながら型の配列を作成しています。

  int [] tabletPricesArray = new int[]{"$1,162.99",
                                         "$399.99",
                                         "$329.99",
                                         "$199.99",
                                         "$149.99"};

問題 2:

型の配列はint、価格などの浮動小数点値を保持しません。代わりに、、、または ($ の代わりに) を使用floatdoubleますdecimal

    decimal[] tabletPricesArray = new decimal[]{1162.99M,
                                                 399.99M,
                                                 329.99M,
                                                 199.99M,
                                                 149.99M};

アイテムを文字列として表示するためだけに使用したい場合tabletPricesArray(計算なし)、ここでも文字列配列を使用できます。

問題 3:

{ }各配列要素には必要ありません。

于 2013-03-18T03:20:57.927 に答える
4

以下が期待されるものであることを願っています。コードを修正しました。

       // Declare listbox information array
       string[] tablet = new string[]{"Microsoft Surface  Price: $1,162.99  Screen Size: 10.6 Inches  Storage Capacity: 128 GB",
                                      "iPad 2 Price: $399.99, Screen Size: 9.7 Inches, Storage Capacity 16 GB",
                                      "Samsung Galaxy Tab 2 Price: $329.99, Screen Size: 10.1 Inches, Storage Capacity 16 GB",
                                      "NOOK HD Price: $199.99, Screen Size: 7 Inches, Storage Capacity 8 GB",
                                      "IdeaTab Price: $149.99, Screen Size: 7 Inches, Storage Capacity: 8 GB"};

       // Array of product prices
       string[] tabletPricesArray = new string[]{"$1,162.99",
                                                   "$399.99",
                                                   "$329.99",
                                                   "$199.99",
                                                   "$149.99"};
于 2013-03-18T04:46:20.177 に答える