2012-04-04 2 views
0

File1에서는 3 개의 문자열이있는 클래스를 만들었습니다. 나는 공공 arraylist와 다른 수업을 만들었습니다. 이 arraylist가 동적 이길 원하고 그 안에 들어있는 객체가 3 개의 문자열을 가진 클래스입니다. 파일의 클래스 멤버에 액세스 할 수 있지만 별도의 파일에는 액세스 할 수 없습니다.arraylist 공개는 어떻게합니까?

파일 1

public class SensorCollection 
    { 
     public string ipAddress; 
     public string portNumber; 
     public string physicalLocation; 

     public DetectorCollection(string ipAddr, string portNum, string loc) 
     { 
      this.ipAddress = ipAddr; 
      this.portNumber = portNum; 
      this.physicalLocation = loc; 

     } 
    } 
    public class SensorCollectionArray 
    { 
     public System.Collections.ArrayList SensorArrayList; 
    } 

... 
System.Collections.ArrayList DetectorArrayList = new System.Collections.ArrayList(); 
... 
    DetectorArrayList.Add(new DetectorCollection(ipAddress, portNum, str)); 

그래서 나는 클래스의 배열을 채울 수 있지만 별도의 파일에 액세스 할 수 없습니다. 파일 2

AdvancedSettingsForm.SensorCollectionArray mainDetectorCollectionArray; 
    System.Collections.ArrayList arrList; 
+3

코드를 컴파일 할 수 없습니다. 당신의 생성자는'DetectorCollection'이지만 당신의 클래스는'SensorCollection'입니다. –

+0

파일이 동일한 네임 스페이스입니까? –

+2

당신은 .net 1.x에 있습니까? 아니면 왜'List '대신'ArrayList'를 사용하고 있습니까? – CodesInChaos

답변

2

이 같은 SensorCollectionArray을 만드는 경우 :

mySCA.SensorArrayList.Add(mySensorCollection); 

:

SensorCollectionArray mySCA = new SensorCollectionArray(); 

그런 다음 당신은 (예를 들어, 항목을 추가하는) 이런 ArrayList에의 액세스 할 수 있습니다 그러나 게시 한 코드에는 SensorCollectionArray에 대한 생성자가 포함되어 있지 않으므로 SensorArrayList는 인스턴스화 후에 null이됩니다. 따라서 별도로 인스턴스화 된 ArrayList로 설정하거나 SensorCollectionArray 클래스에서 ArrayList를 만들 수 있습니다.

최종 참고 사항 : 당신은 당신이 시도 뭐하는

+2

제네릭 사용에 대해 반드시 알아봐야 할 것이라고 말하고 싶습니다. .NET 2 이상을 사용하는 경우에는 'ArrayList'를 사용해야 할 강력한 이유는 없습니다. –

0

완전히 확실하지 강력한 형식의 컬렉션을 만들려면 클래스 (T의) 일반 목록으로 볼 수도 있습니다,하지만 난 그게 같은 뭔가 가정 아래에. 아마도 수집품에 저장하기 전에 어떤 종류의 규칙을 적용하기를 원할 것입니다.

"이것은 좋은 센서입니까? 그렇습니다. 컬렉션에 추가하십시오!"

그렇지 않으면, 당신은 단지

List<Sensor> mySensors; 

를 사용할 수있는 정말 본질적으로 같은 일을하는거야 클래스를 사용하지 마십시오. 그 외에, 언급 된 것처럼 실제로 ArrayList를 사용할 이유가 없습니다. Marc가 지적한 것처럼 ArrayList를 사용하는 가장 큰 이유는 다음과 같습니다. you're using .NET 1.1; 그렇지 않다면, Generic List 콜렉션과 당신을 위해하는 모든 위대한 일들을 사용해야한다.

//Sensor.cs 
public class Sensor 
{ 
    public string Ip{ get; set; } 
    public string Port{ get; set; } 
    public string PhysicalLocation{ get; set } 

    public Sensor(string ipAddr, string portNum, string loc) 
    { 
     Ip= ipAddr; 
     Port= portNum; 
     PhysicalLocation= loc; 
    } 
} 

//SensorCollection.cs 
public class SensorCollection 
{ 
    private List<Sensor> sensors; 

    public Sensor this[int i] 
    { 
     get { return this.sensors[i]; } 
     set { this.sensors[i] = value; } 
    } 

    public IEnumerable<Sensor> Sensors 
    { 
     get{ return this.sensors; } 
    } 

    public SensorCollection() 
    { 
     sensors = new List<Sensor>(); 
    } 

    public SensorCollection(string ip, string port, string location) : this() 
    { 
     this.sensors.Add(new Sensor(ip, port, location)); 
    } 

    public SensorCollection(Sensor sensor) : this() 
    { 
     this.sensors.Add(sensor); 
    } 

    public void AddSensor(Sensor sensor) 
    { 
     //Determine whether or not to add it 
     this.sensors.Add(sensor); 
    } 

    public void RemoveSensor(Sensor sensor) 
    { 
     if (sensors.Contains(sensor)) 
      sensors.Remove(sensor); 
    } 
} 

편집

어떻게 클래스의 내 동적으로 생성 목록의 각 센서의 IPADDRESS 액세스합니까?

var mySensors = new SensorCollection(); 
mySensors.AddSensor(new Sensor("1.1.1.1", "123", "Home")); 
mySensors.AddSensor(new Sensor("9.9.9.9", "123", "Work")); 

foreach(Sensor s in mySensors.Sensors) 
    Console.WriteLine(s.Ip); 
내가

그들은 당신이 "사용"을 포함하는 것과 같은 공간에있어, 또는 확인 다른 파일에서 클래스의 멤버에 액세스 할 수없는 것

문 여기에는 생성 한 클래스의 네임 스페이스가 포함됩니다.

+0

답변 해 주셔서 감사합니다. 다양한 센서가 있습니다. 동적으로 각 센서의 하위 탭을 만듭니다. 세 문자열 구성원 (ipaddr, portnum, loc)을 보유 할 클래스를 갖고 싶습니다. 이러한 멤버는 .ini 파일을 통해 채워집니다. 회원은 다른 파일에서 액세스해야하며 그 곳에서 끊깁니다. 동적으로 생성 된 클래스 목록에서 각 센서의 ipaddress에 어떻게 액세스합니까? – user1313577

+0

내가 이전 게시물을 명확하게하겠습니다. 센서 클래스의 동적 배열이 필요합니다. 첫 번째 파일에서는 다음을 사용하여 각 멤버에 액세스 할 수있었습니다. (SensorArrayList [0] as SensorDataCollection) .ipAddress하지만 다른 파일의 클래스 멤버에 액세스 할 수 없습니다. SensorArrayList [i] – user1313577

+0

내 대답을 약간 편집했습니다. 도움이되는지 확인하십시오. 물론, IList를 구현 한 사용자 정의 컬렉션을 만들거나 List 을 단순히 사용하고 사용자 정의 컬렉션 클래스를 전혀 사용하지 않는 등 특정 목표를 수행 할 수있는 여러 가지 방법이 있으므로 이러한 대안을 고려할 수 있습니다. – Jesse