2014-10-28 2 views
2

Windows 양식 응용 프로그램의 경우 C#에서 ArrayList을 초기화했습니다. 내가합니다 (ArrayList 많은 개체가 가정)을 ArrayList에서 각 개체의 FieldName을 검사 할 때 이제ArrayList에있는 개체의 속성 값을 가져옵니다.

ArrayList FormFields = new ArrayList(); 

CDatabaseField Db = new CDatabaseField(); 
Db.FieldName = FieldName; //FieldName is the input value fetched from the Windows Form 
Db.PageNo = PageNo; //PageNo, Description, ButtonCommand are also fetched like FieldName 
Db.Description = Description; 
Db.ButtonCommand = ButtonCommand; 
FormFields.Add(Db); 

: 나는 같은 ArrayList에있는 각 개체의 몇 가지 속성을 가진 새로운 개체를 추가하고 . 어떻게 할 수 있습니까 ??

for(int i=0; i<FormFields.Count; i++) 
{ 
    FieldName = FormFields[i].FieldName; 
} 

를하지만이 (IDE에서) 오류를 생성한다 :

나는 시도했다. 나는 C# 프로그래밍에 익숙하지 않아 누군가 이걸 도와 줄 수 있니?

Error: Error 21 'object' does not contain a definition for 'FieldName' and no extension method 'FieldName' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

+1

배열 목록 대신 'CDatabaseField' 목록을 사용할 수 있습니까? –

+0

'Recipe'는'OP's'의 가장 좋은 방법은'Selman22'이 지적한대로 실제로 CDatabaseField jsut리스트를 만드는 것입니다.'List ' – MethodMan

+0

Daniel, 실제로 저는 오래된 소프트웨어로 작업 중이며 이름이 arraylist입니다 FormFields는 CDatabaseField 목록을 저장하며 소프트웨어의 수천 곳에서 사용되고 있습니다. arraylist를 목록으로 바꾸려면 나는 많은 곳에서 그것을 변경할 필요가있다. 이것은 현재 실현 가능하지 않다. arraylist에 보관 된 객체에서 특정 fieldname을 가져 오는 방법을 제안 할 수 있습니까 ?? –

답변

2

마지막으로 대답을 알아 냈습니다. this에 따라

for (int i = 0; i < FormFields.Count; i++) 
{ 
    CDatabaseField Db = (CDatabaseField)FormFields[i]; 
    Label1.Text = Db.FieldName; //FieldName is the required property to fetch 
} 
4

ArrayList 개체가 있습니다. 그것은 일반적인 것이 아니고 타입 안전합니다. 그래서 여러분은 여러분의 객체가 속성에 접근 할 수 있도록 캐스팅해야합니다. 대신 List<T>과 같은 일반적인 컬렉션을 사용해보십시오.

var FormFields = new List<CDatabaseField>(); 
CDatabaseField Db = new CDatabaseField(); 
... 
FormFields.Add(Db); 

그럼 당신은 지금 컴파일러가 요소의 유형을 알고 당신이 형태 보증 방식으로 유형의 멤버에 액세스 할 수 있기 때문에 모든 속성이 표시됩니다 것을 볼 수 있습니다.

+0

지금이 방법을 시도했지만 'var'는 Visual Basic에서 오류를 생성합니다. Error 'var'형식 또는 네임 스페이스 이름을 찾을 수 없습니다 (사용 지시문이나 어셈블리 참조가 누락 되었습니까?) –

+0

Selman22 실제로 저는 오래된 소프트웨어를 연구하고 있으며 소프트웨어의 수천 곳에서 사용되고있는 CDatabaseField 목록을 저장하기 위해 FormFields라는 arraylist를 구현합니다.arraylist를 목록으로 바꾸려면 나는 많은 곳에서 그것을 변경할 필요가있다. 이것은 현재 실현 가능하지 않다. arraylist에 보관 된 객체에서 특정 fieldname을 가져 오는 방법을 제안 해 주시겠습니까 ?? –

1

가 이미 지적과 : 나는 ArrayList의에 저장된 각 개체의 개체를 캐스팅하려고 마지막으로 각 개체의 필수 항목을 가져올 수

The Item returns an Object , so you may need to cast the returned value to the original type in order to manipulate it. It is important to note that ArrayList is not a strongly-typed collection. For a strongly-typed alternative, see List<T> .

을 그러나로 다른 옵션으로는 for 대신 foreach 루프를 사용할 수 있습니다.

foreach (CDatabaseField item in FormFields) 
{ 
    FieldName = item.FieldName; 
} 

foreachdocumentation 및 C# 6 구문에 따르면, 위의 코드는 동일합니다 : 그것은 CDatabaseFieldArrayListcast 요소에 foreach 실행을 시도 할 때와 요소가 CDatabaseField로 전환하지 않은 경우 당신은이 InvalidCastException 얻을 것이다 까지 :

var enumerator = FormFields.GetEnumerator(); 
try 
{ 
    while (enumerator.MoveNext()) 
    { 
     CDatabaseField item = (CDatabaseField)enumerator.Current; 
    } 
} 
finally 
{ 
    var disposable = enumerator as IDisposable; 
    disposable?.Dispose(); 
} 
관련 문제