2013-07-16 3 views
0

나는 프로그래밍 방식으로 이벤트로 함수를 호출하려고합니다.이벤트로 문자열

문자열을 일반적인 이벤트로 변환하는 방법은 무엇입니까? 내 문제는 실제로 어떻게 알지 못합니다. 어떻게해야합니까?

str을 이벤트로 변환하는 방법?

str = "test1"; 

// UserControlsBackgroundEventArgs = EventArgs 
EventArgs arg = (EventArgs)str; --> ? 
UserControlsBackgroundOutput(str); 



//function 
private string CLICKNAME = "test0"; 
private void UserControlsBackgroundOutput(EventArgs e) 
{  
    if (CLICKNAME == e.output) 
     return; 

    if (e.output == "test1"){} 
} 

오류 해결 : 나는

UserControlsBackgroundEventArgs arg = new UserControlsBackgroundEventArgs(CLICKNAME); 

대신 이벤트 클래스는 문자열을 받아들이는 생성자를 가질 필요가

UserControlsBackgroundEventArgs arg = new (UserControlsBackgroundEventArgs)(CLICKNAME); 
+2

을 우리는 당신을 돕기 위해 열심히하게 UserControlsBackgroundEventArgs'이 무엇인지'아무 생각,이 없다 ... 질문 지금은 매우 불분명하다. http://tinyurl.com/so-list를 읽으십시오. (문자열을 * 이벤트 *로 변환하려고 시도조차하고 있지 않습니다 - 당신이 그것을'UserControlsBackgroundEventArgs' 클래스의 인스턴스로 변환하려고합니다. isn ' t 이벤트 ... –

+0

이제는 'UserControlsBackgroundEventArgs'가 'str'의 값을 보유 할 속성을 가져야한다는 것입니다. 질문에 더 많은 컨텍스트를 추가 할 수 있습니까? 실제 사용이란 무엇입니까? – Leri

+0

'UserControlsBackgroundEventArgs = EventArgs'는 무엇을 의미합니까? 동일한 클래스입니까? – pascalhein

답변

0

, 잘하면 당신이 유용 할 것입니다 :

public class UserControlsBackgroundEventArgs 
{ 
    public string output; 

    public UserControlsBackgroundEventArgs(string up) 
    { 
    output = up; 
    } 
} 

public delegate void UserControlsBackgroundOutputHandle(UserControlsBackgroundEventArgs e); 

public class testEvent 
{ 
    public event UserControlsBackgroundOutputHandle UserControlsBackgroundOutput; 

    public void DoSomeThings() 
    { 
    // do some things 

    if (UserControlsBackgroundOutput != null) 
    { 
     string str = "test1"; 

     UserControlsBackgroundEventArgs arg = new UserControlsBackgroundEventArgs(str); 
     UserControlsBackgroundOutput(arg); // you've done that with str, whitch makes me 
              // you don't know what the event param is 
    } 
    } 
} 

public class test 
{ 
    private testEvent myTest; 
    private const string CLICKNAME = "whatever"; // i don't know what you want here 

    public test() 
    { 
    myTest = new testEvent(); 
    myTest.UserControlsBackgroundOutput += UserControlsBackgroundOutput; 
    } 

    void UserControlsBackgroundOutput(UserControlsBackgroundEventArgs e) 
    { 
    if (CLICKNAME == e.output) 
     return; 

    if (e.output == "test1") 
    { 
    } 
    } 
} 
0

의해야했다. 그런 다음 문자열을 사용하여 새 이벤트 인스턴스를 만들 수 있습니다. 문자열을 이벤트 클래스의 인스턴스로 "변환"할 수 없습니다. 이벤트 클래스가 라이브러리 또는 sth에서 온 것으로 문자열 생성자가없는 경우 하위 클래스로 만들고 문자열 생성자를 구현하고 출력 속성을 무시할 수 있습니다.

0

UserControlsBackgroundEventArgs 구현시 암시 적/명시 적 캐스트를 제공 할 수 있습니다.

그러나, 요이치 Budniak의 대답은 더 implicit keyword documentation

에서 살펴 보자. 당신이 변환의이 종류가 가능하려면

0

, 당신은 사용할 필요가 explicit operator :

public static explicit operator UserControlsBackgroundEventArgs(string s) 
{ 
    var args = new UserControlsBackgroundEventArgs(); 
    args.output = s; 
    return args; 
} 

코드를 변경할 수 없기 때문에이 아니라 EventArgs으로, 새로운 클래스 만 가능 그 계급의 내가 당신에게 코드를 모방 코드를 작성했습니다