Re: Generische Klasse und CType



"Tomasz Poplawski" <tpoplawski@xxxxxxxxxxxx> schrieb
Hallo NG!

In meiner Anwendung (Windows.Forms) muss ich je nach Bedingung
bestimmte Controls ausblenden.

Ich habe es mir so vorgestellt:

Friend Class FormForHandling(Of T)
Private _frm As T

Public Sub New(ByRef frm As Form)
Me._frm = CType(frm, T)
End Sub

Friend ReadOnly Property frm() As T
Get
Return Me._frm
End Get
End Property
End Class

Damit erhalte ich Zugriff auf alle Controls und kann diese dann
Ausblenden.


Aber:

An dieser Stelle Me._frm = CType(frm, T)
erhalte ich eine Fehlermeldung:

"Der Wert vom Typ "System.Windows.Forms.Form" kann nicht zu "T"
konvertiert werden."

Warum?

Wenn du sowieso nur den Typ Form zulässt, warum deklarierst du _frm
nicht als Form?

CType kann so nicht funktionieren, da es für T keine Einschränkungen bzgl des tatsächlichen Typs gibt. Es könnte also auch "_frm As Stream" sein. Somit wäre CType(frm, Stream) nicht zulässig, da Form nicht von Stream erbt.

Wenn du eine Einschränkung hinzufügst "(Of T As {Form})" dann funktioniert auch "CType(frm, T)", da gewährleistet ist, dass der Zieltyp dem Quelltyp entspricht. Allerdings ist dann die Verwendung von Generics ziemlich sinnfrei.


Armin

.



Relevant Pages

  • Generische Klasse und CType
    ... Friend ReadOnly Property frm() As T ... Damit erhalte ich Zugriff auf alle Controls und kann diese dann Ausblenden. ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: Cross thread question .....
    ... I have an app that reads data params from a stream and updates ... the main thread that created the controls. ... Your stream thread just reads results and updates the "neutral zone" ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Cross thread question .....
    ... What if you create a delegate and pass this delegate as a parameter to your ... I have an app that reads data params from a stream and updates ... The stream reader is on a different thread than ... the main thread that created the controls. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Delphi type events in C#
    ... each of the controls would be notified? ... handlers are executed synchronously (though really it's up to the ... probably list) of subscribers and the logic to control them - yes? ... you create the stream and then attach it to the reader/writer ...
    (microsoft.public.dotnet.languages.csharp)
  • Cross thread question .....
    ... I have an app that reads data params from a stream and updates ... The stream reader is on a different thread than ... the main thread that created the controls. ...
    (microsoft.public.dotnet.languages.csharp)