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
Was ist das Problem/Fehler? – Rob
und Sie Problem ist? was fragst du? –
Ihre 'Console.WriteLine' Methoden enthalten' {0] '. Ordne deine Hosenträger zusammen. –