2016-03-26 27 views
2

次のコードをすばやく汚れたPOCとして作成しました。作成した配列のオブジェクトにアクセスしようとするまではすべて動作します。オブジェクトを配列に格納してからアクセスする

私は次のコード行を配列内のオブジェクトにアクセスしようとしています下のあなたは、コードで見ることができるように:

Console.WriteLine("AWS Account Id = {0]", array1[1].AccountId); 

全体のコードは以下の通りです:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
    public class AWSAccount 
    { 
     public string AccountId { get; set; } 
     public string[] Instances { get; set; } 
     public AWSAccount(string accountId, string[] instances) 
     { 
      AccountId = accountId; 
      Instances = instances; 
     } 
    } 

    class Program 
    { 
     static void Main() 
     { 
      string[] instances1 = {"i-xxxxx01", "i-xxxxx02"}; 
      AWSAccount account1 = new AWSAccount("53853288254", instances1); 
      Console.WriteLine("AWS Account Id = {0} Instances = {1} {2}", account1.AccountId, account1.Instances[0], account1.Instances[1]); 

      string[] instances2 = { "i-zzzzz01", "i-zzzzz02" }; 
      AWSAccount account2 = new AWSAccount("74378834238", instances2); 
      Console.WriteLine("AWS Account Id = {0} Instances = {1} {2}", account2.AccountId, account2.Instances[0], account2.Instances[1]); 


      object[] array1 = new object[2]; 
      array1[0] = account1; 
      array1[1] = account2; 

      Console.WriteLine("AWS Account Id = {0}", array1[0].AccountId); 
      Console.WriteLine("AWS Account Id = {0}", array1[1].AccountId); 

      // Keep the console open in debug mode. 
      Console.WriteLine("Press any key to exit."); 
      Console.ReadKey(); 

     } 
    } 
} 

intellisenseは.AccountIdを取得せず、以下のエラーで強調表示します。

Error 1 'object' does not contain a definition for 'AccountId' and no extension method 'AccountId' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) c:\Users\jploof\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 37 65 ConsoleApplication1 
+0

問題/エラーは何ですか? – Rob

+0

あなたは問題ですか?何を聞いていますか? –

+0

'Console.WriteLine'メソッドには' {0} 'が含まれています。あなたの中括弧を合わせます。 –

答えて

1

以下に示すようにAWSAccount[] array1 = new AWSAccount[2]を使用してのMain()プロシージャを修正:

static void Main(string[] args) 
{ 

    string[] instances1 = { "i-xxxxx01", "i-xxxxx02" }; 
    AWSAccount account1 = new AWSAccount("53853288254", instances1); 
    Console.WriteLine("AWS Account Id = {0} Instances = {1} {2}", account1.AccountId, account1.Instances[0], account1.Instances[1]); 

    string[] instances2 = { "i-zzzzz01", "i-zzzzz02" }; 
    AWSAccount account2 = new AWSAccount("74378834238", instances2); 

    Console.WriteLine("AWS Account Id = {0} Instances = {1} {2}", account2.AccountId, account2.Instances[0], account2.Instances[1]); 

    AWSAccount[] array1 = new AWSAccount[2]; 
    array1[0] = account1; 
    array1[1] = account2; 

    Console.WriteLine("AWS Account Id = {0}", array1[0].AccountId); 
    Console.WriteLine("AWS Account Id = {0}", array1[1].AccountId); 

    // Keep the console open in debug mode. 
    Console.WriteLine("Press any key to exit."); 
    Console.ReadKey(); 
} 

あなたのオリジナルのポストでの構文エラー:これは役立つかもしれ"AWS Account Id = {0]"

希望を。

+0

構文を修正してその他の変更を加えましたが、現在は "array1 [0] .AccountId"の書式について不平を言っています。FormatExceptionは処理されませんでした。 {"入力文字列が正しい形式ではありませんでした。"} ...私はToString()とbuenoを追加しようとしました。 –

+1

ちょうどコピー - 私の答えからコードスニペット全体を貼り付けます。それはうまく動作します(実際のアプリでテストされています)。よろしく、 –

+1

ニース!それはうまくいった!私はあなたが持っていたものと私が持っていたものとの比較を超えて使用しなければならないでしょう。助けてくれてありがとう! –

1

あなたのオブジェクト[]をあなたを並べ替えるべきAWSAccount []に変更してください。

+0

これは私を近づけているようですが、今は "array1 [0] .AccountId"の書式設定についてcomplaingしています。FormatExceptionは処理されませんでした。 {"入力文字列が正しい形式ではありませんでした。"} ...私はToString()とbuenoを追加しようとしました。 –

関連する問題