2014-06-24 2 views
0

ListView 개체를 배열에 추가하여 for 루프를 사용하여 신속하게 해결할 수 있습니까?배열에 ListView 개체 추가

public static ListView tagListView; 
    public static ListView commonListView; 
    public static ListView recentListView; 
    public static ListView[] listviews = { tagListView, commonListView, recentListView }; 

이 코드는 listviews 배열 항목이 null이됩니다. 나는 동일한 결과로 이것에 대한 몇 가지 변형을 시도했다. 가능한가요? 이 세 가지 객체에 대한 포인터 배열을 만들어야하는 것 같습니다.

ListViews는 대부분 매우 다르며 이름을 사용하면 배열에 세 개의 항목을 포함하는 것보다 훨씬 읽기 쉽기 때문에이 작업을 수행하려고합니다.하지만 동일한 작업을 수행해야 할 때가 있습니다. 세 명 모두에게.

+4

배열에 추가 할 세 가지 변수를 인스턴스화하지 않았습니다. 그것이 그들이 'null'인 이유입니다. – Enigmativity

답변

1

거의 다 알았습니다. ListView를 인스턴스화하면됩니다. 먼저 세 가지 목록보기를 인스턴스화 할 수있는 경우

public static ListView[] listviews = { null, null, null}; 

:

public static ListView tagListView = null; 
public static ListView commonListView = null; 
public static ListView recentListView = null; 

그래서 배열 할당은 정말이 일을한다 : 그것은 약자로

public static ListView tagListView = new ListView(); 
    public static ListView commonListView = new ListView(); 
    public static ListView recentListView = new ListView(); 
    public static ListView[] listviews = { tagListView, commonListView, recentListView }; 
+0

죄송 합니다만,이 세 개의 ListView가 내 코드의 다른 부분에서 내 양식의 실제 ListView에 지정되었다는 것을 언급하지 않았습니다. – Stoopkid

+0

당신이 다루고있는 문제의 코드를 보여줄 수 있습니까? –

0

코드는, 효과적으로이다 그러면 그것은 최선의 접근 방식이 될 것입니다.

그러나이를 수행 할 수없고 나중에 코드에서 인스턴스화해야하는 경우 다른 접근 방법이 있습니다.

당신은이 작업을 수행 할 수 있습니다 :

public static IEnumerable<ListView> listviews = (new Func<ListView>[] 
{ 
    () => tagListView, 
    () => commonListView, 
    () => recentListView, 
}).Select(x => x()).Where(x => x != null); 

지금 당신은 당신의 인스턴스 목록보기의 열거가 당시 당신은 열거 반복.