Get type

Carlos++

Active member
Joined
Sep 14, 2007
Messages
26
Location
As. Paraguay
Programming Experience
5-10
how can i do to get a class in run time to make set sentences in another class?
as a example:

i've got a form named as form1.
this form has controls, codes, etc. inside...
it's very easy to asign any kind of type to a variable

dim a as form1 ...

but what i need is to make that instuction when the program is running

other example


VB.NET:
class cl
     dim a as [type] '<- in this line i need the form1 class type to obtain a        'result like this: dim a as form1

end class

thanks for the help

disculpen el malisimo ingles (bad english speaker)
 
Your question doesnt make much sense.. but by declaring a variable as type Object you can assign anything to it:

Dim o as Object = New Form1


Perhaps if you tell us what problem you are trying to solve, and not the problem with your proposed solution, we can help more
 
VB.NET:
class alfa(of T)
dim a as t
end class

"using generics"

dim b as alfa (of form1)

when the alfa class became a class of form1 i need to use T variable as a instance of form... T is only a generic object...

how can i fix my problem?
 
T is the Type, not an instance. For generic classes your class code have to be written to match all types, so you have to write generic code.

What is it you are trying to do?
 
you're right... my mistake.
me exprese mal... i know that T is a type ... but i need to use t as an instanciable object.

i have 100 forms of differents types. everyone has a unique behavior.

i need to englobe those forms in a class that supports every event, property and method of the whole forms group.

like this

dim a as AllForms

a.form("myformname").oneproperty="hola mundo"
a.form("anotherformname").oneproperty="hola mundo 2"
 
You don't normally invoke instance members with generics, a generic class is more like a container that is written to handle any type, where the instances is provided at runtime, for example to maintain a strongly typed collection of these and/or compare them.

From a Type you can use reflection to create a new instance, you will then get a new instance reference of that type encapsulated as Object. Also here the code have to be specialized for each type to access instance members, which is not generic (and not commonly used for programming)

Another way to handle different types as one type is to use an Interface. An interface is an empty type specification that defines methods and properties. Implementing classes define the method/property content. Different instances that implement the same interface can be casted to this type to get access to the interface members.

As for what you are trying to do I still don't understand it, your description is too vague.
 
i'm sorry but i really can't find the way to explain my problem in english and google translator writes only "mierda".

can you tell more about your idea, maybe some tutorials , code or something else.
only code can speak for you spanish :D

thank you for your help...
 
VB.NET:
Class MasterForm

  private _oneProperty as String
  Public Property OneProperty
    Get
      Return _oneProperty
    End Get
    Set
      _oneProperty = value
    End Set
  End Property

...

VB.NET:
  Class FormOne
    Inherits MasterForm

...

VB.NET:
  Class FormTwo
    Inherits MasterForm

...

VB.NET:
  Class FormNinetyNine
    Inherits MasterForm

...

VB.NET:
  Class FormOneHundred
    Inherits MasterForm

...

VB.NET:
  ...

  Dictionary form(Of String, MasterForm) = New Dictionary(Of String, MasterForm)

  form.Add("myFormName", New FormOne)
  form.Add("anotherFormName", New FormTwo)
  ...
  form.Add("yetAnotherFormName", New FormNinetyNine)
  form.Add("stillAnotherFormName", New FormOneHundred)


  form("myFormName").OneProperty = "hola mundo"
  form("anotherFormName").OneProperty = "hola mundo 2"
  ...
  form("yetAnotherFormName").OneProperty = "hola mundo 98"
  form("stillAnotherFormName").OneProperty = "hola mundo 99"
  ...

Hopefully this makes more sense than my Spanish?
 
Each form is a class already, cjard suggested you could inherit from the same base class (form).
 
yes, i understand that.

look this code, is probable to find errors because of the copy/paste.
i want to change every class called Form for another unknow class.
e.g. Form -> Form1..., Form2, etc.
i use Form class only for this example.
i can't use the object type because it can't support any behavior of the Form class.

thank you johnh and cjard :)

VB.NET:
Public Class Form1
    Class Mani
        Class Formulario
            Public FormularioDetalle As Detail
            Public FormularioLista As List
            Friend _FList As New Form '** <-
            Friend _FDetail As New Form
            Sub New(ByVal D As Detail, ByVal L As List)
                FormularioDetalle = D
                FormularioLista = L
            End Sub
            Public MustInherit Class FormBase
                Protected WithEvents _F As Form
                Public Overridable Sub Mostrar()
                    If _F IsNot Nothing Then
                        _F.Show()
                    Else
                        _F = New Form
                    End If
                End Sub
                Private Sub Al_Cargar_Cliente(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _F.Load
                    MsgBox("abriendo " & _F.Text)
                End Sub

                Private Sub Al_Cerrar_Cliente(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles _F.FormClosing
                    MsgBox("cerrando")
                    '_F = New sender
                End Sub
            End Class

            Public Class Detail : Inherits FormBase
                Sub New(ByVal ClienteDetalle As Form)
                    _F = ClienteDetalle
                End Sub
            End Class

            Public Class List : Inherits FormBase
                Sub New(ByVal ClienteDetalle As Form)
                    _F = ClienteDetalle
                End Sub
            End Class
        End Class
        Private _hashtbl_Formularios As Hashtable
        Sub alfa(ByVal a As t)
            Dim al As Form
            'al = a

        End Sub
        Sub New()
            'HashTbl_FList = New Hashtable
            'HashTbl_FDetail = New Hashtable
            _hashtbl_Formularios = New Hashtable
        End Sub

        Sub AgregarFormulario(ByVal Key As String, ByVal ClienteList As Form, ByVal ClienteDetail As Form)
            Try
                If Key.Trim <> "" Then
                    _hashtbl_Formularios(Key) = New Formulario(New Formulario.Detail(ClienteDetail), New Formulario.List(ClienteList))
                Else
                    Throw New Exception("Clave no Válida")
                End If
            Catch ex As Exception
                Throw New Exception(ex.Message)
            End Try

        End Sub

        Sub i()

        End Sub

        Public ReadOnly Property GetForm(ByVal Key As String) As Formulario
            Get
                Return _hashtbl_Formularios(Key)
            End Get
        End Property
    End Class

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim a As New Mani
        a.AgregarFormulario("f1", Form2, Form3)
        a.GetForm("f1").FormularioDetalle.Mostrar()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
 
so i have to make one class for each form in my project?

What? Er. Every time you make a form its already inherited from Form:

VB.NET:
Class MyForm 
  Inherits Form

you make a new type of form every time you have something very different to show or do
 
yes, i know that, it's been 5 years since i start vb programming
TBH, being a VB programmer doesnt make you an OO programmer..

i think that my problem has no solution...
I think we are having trouble understanding what youe problem is.. I know the language barrier is an obstacle, but if you can imagine that you are explaining it to a 5 year old child, and write it very simple, then the translator might have better luck.
 
i finally discover how to fix my problem...
was a long search...
here is te code:
VB.NET:
Public Class myinfo
    Public Shared ReadOnly Property NombreEnsambladoA() As String
        Get
            Return My.Application.Info.AssemblyName & "."
        End Get
    End Property
    Public Shared ReadOnly Property NombreEnsambladoB() As String
        Get
            Return My.Application.Info.ProductName & "."
        End Get
    End Property
End Class
Public Class crdFHandler
    Class Formularios
        Private Structure AssUti
            Dim MiEnsamblado As System.Reflection.Assembly
            Dim TipoD As Type
            Dim TipoL As Type
        End Structure
        Private _AU As AssUti
        Private _FL As FList
        Private _FD As FDetail
        Public MustInherit Class MasterForm
            Protected WithEvents _F As Form
            Protected _MiTipo As Type            
            Public Overridable Sub Mostrar()
                If _F Is Nothing Or _F.IsDisposed = True Then
                    _F = Me.NuevaInstanciaDeMiTipo
                Else
                    _F.BringToFront()
                End If
                _F.Show()
            End Sub
            Private Function NuevaInstanciaDeMiTipo() As Form
                Return Activator.CreateInstance(_MiTipo)
            End Function
        End Class
        Public Class FDetail : Inherits MasterForm            
            Sub New(ByVal FormDetail As Object, ByVal MiTipo As Type)
                Me._F = DirectCast(FormDetail, Form)
                Me._MiTipo = MiTipo
            End Sub
        End Class

        Public Class FList : Inherits MasterForm
            Sub New(ByVal FormList As Object, ByVal MiTipo As Type)
                Me._F = DirectCast(FormList, Form)
                Me._MiTipo = MiTipo
            End Sub
        End Class
        Private Sub Inicializar()
            _AU.MiEnsamblado = System.Reflection.Assembly.GetExecutingAssembly
        End Sub
        Sub New(ByVal NombreClaseL As String, ByVal NombreClaseD As String)
            Me.Inicializar()
            _AU.TipoD = _AU.MiEnsamblado.GetType(NombreClaseD)
            _AU.TipoL = _AU.MiEnsamblado.GetType(NombreClaseL)
            _FL = New FList(Activator.CreateInstance(_AU.TipoL), _AU.TipoL)
            _FD = New FDetail(Activator.CreateInstance(_AU.TipoD), _AU.TipoD)
        End Sub
        Public ReadOnly Property FormularioDetalle() As FDetail
            Get
                Return _FD
            End Get
        End Property
        Public ReadOnly Property FormularioLista() As FList
            Get
                Return _FL
            End Get
        End Property
    End Class
    Private _MyAss As System.Reflection.Assembly
    Private _FClasses() As Type
    Private _htFormularios As Hashtable

    Sub New()
        Call Me.ListarClasesDelEnsamblado()
        _htFormularios = New Hashtable
    End Sub

    Private Sub ListarClasesDelEnsamblado()
        Dim Counter As Integer
        _MyAss = System.Reflection.Assembly.GetExecutingAssembly
        For Each T As Type In _MyAss.GetTypes
            ReDim Preserve _FClasses(Counter)
            _FClasses(Counter) = T
            Counter += 1
        Next
    End Sub

    Public Function AgregarFormularios(ByVal Clave As String, ByVal NombreFormList As String, ByVal NombreFormDetail As String) As Boolean
        Try
            If _htFormularios(Clave) Is Nothing Then
                If EsFormulario(NombreFormDetail) And EsFormulario(NombreFormList) Then
                    _htFormularios.Add(Clave, New Formularios(NombreFormList, NombreFormDetail))
                Else
                    Throw New Exception("Parametro Pasado No es un Formulario")
                End If
                Return True
            Else
                Throw New Exception("Ya existe la clave '" & Clave & "' en la lista")
            End If
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function

    Public ReadOnly Property GetFormularios(ByVal Clave As String) As Formularios
        Get
            Try
                If _htFormularios(Clave) Is Nothing Then
                    Throw New Exception("Clave '" & Clave & "' no existe o no apunta a ningún valor")
                Else
                    Return _htFormularios(Clave)
                End If
            Catch ex As Exception
                Throw New Exception(ex.Message)
            End Try
        End Get
    End Property

    Private Function EsFormulario(ByVal NombreClase As String) As Boolean
        Try
            Dim T As Type = _MyAss.GetType(NombreClase), oTest As Object
            oTest = Activator.CreateInstance(T)
            If TypeOf oTest Is Form Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Debug.Print(ex.Message)
            Return False
        End Try
    End Function
End Class

thanks to all of you :)
 
Back
Top