2016-03-26 8 views
2

Ich schrieb den folgenden Code als eine schnelle und schmutzige POC, und alles funktioniert, bis ich versuche, auf die Objekte in dem Array, das ich erstellt habe, zuzugreifen.Objekte in Arrays speichern und darauf zugreifen

Wie Sie im Code unten sehen können wir das Objekt in dem Feld mit der folgenden Codezeile zuzugreifen versuchen:

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

Der gesamte Code unten:

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(); 

     } 
    } 
} 

Die intellisense liest die .AccountId nicht ein und hebt sie mit dem folgenden Fehler hervor.

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

Was ist das Problem/Fehler? – Rob

+0

und Sie Problem ist? was fragst du? –

+0

Ihre 'Console.WriteLine' Methoden enthalten' {0] '. Ordne deine Hosenträger zusammen. –

Antwort

1

Korrigieren Sie Ihre Prozedur Main() mit AWSAccount[] array1 = new AWSAccount[2] wie unten dargestellt:

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(); 
} 

Hinweis die Syntaxfehler in ihrer Original-Beitrag: "AWS Account Id = {0]"

Hope this helfen können.

+0

Ich habe die Syntax korrigiert und die anderen Änderungen vorgenommen, aber jetzt beschweren sie sich über die Formatierung "array1 [0] .AccountId". FormatException wurde nicht behandelt. {"Die Eingabezeichenfolge hatte kein richtiges Format."} ... Ich habe versucht, ToString() und kein bueno hinzuzufügen. –

+1

Kopieren Sie einfach das gesamte Code-Snippet aus meiner Antwort. es funktioniert gut (getestet in der realen app). Mit freundlichen Grüßen, –

+1

Nizza! Das hat funktioniert! Ich werde unvergleichlich gegen das, was du hattest und was ich hatte, verwenden und herausfinden, wo ich versagt habe! Vielen Dank für die Hilfe! –

1

Ändern Sie Ihr Objekt [] zu AWSAccount [], die Sie aussortieren sollten.

+0

Das scheint mich näher zu bringen, aber jetzt beschweren sie sich über die Formatierung "array1 [0] .AccountId". FormatException wurde nicht behandelt. {"Die Eingabezeichenfolge hatte kein richtiges Format."} ... Ich habe versucht, ToString() und kein bueno hinzuzufügen. –

Verwandte Themen