2014-04-19 3 views
0

개체에 항상 변경되는 이름을 지정하고 싶습니다. 나는 압정을 놓는 foreach가있다. foreach를 통해 반복 될 때마다 내 목록의 내용을 기반으로 새 이름이 필요합니다 (목록을 반복합니다). test.testname 문자열이기 때문에, 나는이 작업을 수행 할 수없는 오전변수 문자열의 개체 이름을 지정하십시오.

Test test = new Test(); 
      foreach (var test in Test.allTests) 
      { 
       Pushpin test.testname = new Pushpin(); 
      } 

.. : 나는 이런 식으로 뭔가를하고 싶은

Test test = new Test(); 
     foreach (var test in Test.allTests) 
     { 
      Pushpin pushpin = new Pushpin(); 
     } 

:

예를 들어,이 내 코드입니다

+0

'Dictionary '이 여기에 부적합한 특별한 이유는 무엇입니까? – ClickRick

+1

"이와 비슷한 것"으로 무엇을 달성하고 싶은지 설명 할 수 있습니까? 어떤 타입이'test'이고'testname'을 어떻게 설정 하시겠습니까? – CodeCaster

+2

달성하려는 목표는 무엇입니까? –

답변

1

나는 여기에서 내 목을 붙잡고 실제로 원하는 것에 대해 야생의 가정을 할 것입니다.

class Pushpin 
{ 
    public Pushpin() { } 
    // More Pushpin-related members and methods here 
} 
class Test 
{ 
    public Test(string name) { Name = name; } 
    public string Name { get; set; } 
    // More Test-related members and methods here 
} 
class SO23174064 
{ 
    Dictionary<string, Pushpin> pushpins = new Dictionary<string, Pushpin>(); 
    public Dictionary<string, Pushpin> CreatePushpins(IEnumerable<Test> tests) 
    { 
     foreach (Test test in tests) 
      pushpins[test.Name] = new Pushpin(); 
     return pushpins; 
    } 
} 

그런 다음 메인 프로그램에서이 같은 것을 사용할 수 있습니다

Test[] tests = new Test[] { new Test("x"), new Test("y") }; 
    Dictionary<string, Pushpin> pins = new SO23174064().CreatePushpins(tests); 
    // use pins["x"] etc here 

내가 닫나요?

관련 문제