Ess Kay
asked on
Cast or Directcast, passing a custom type
I have several forms who will be parent calling this user control. They all have a variable (isGood = boolean)
they also all have functions (setGood(boolean)) which sets to true or false.
I have an object clsObject
Dim clsObject As Object 'Object to hold parent form
If Me.ParentForm.Name = "frm1" Then
clsObject = CType(Me.ParentForm, frm1)
ElseIf Me.ParentForm.Name = "frm2" Then
clsObject = CType(Me.ParentForm, frm2)
End If
clsObject.setGood(true) 'This works fine
' Dim x = clsObject.isGood -Doesnt work
Dim sType As Type = clsObject.GetType
Dim tempGood
tempGood = CType(Me.ParentForm, Object).isGood 'Doesnt work - "Public member 'isGood' on type 'frm1' not found."
tempGood = CType(Me.ParentForm, stype).isGood 'Doesnt work - Doesnt understand Stype
'DirectCast(CType(Me.Paren tForm, Object), stype).isGood 'Doesnt work - Doesnt understand Stype
the only way i can get it to work is
DirectCast(CType(Me.Parent Form, Object), bla.bla.frm1).isGood
but that is not generic and the parent form of the udc WILL change
How do i get the value of clsObject.isGood ??
they also all have functions (setGood(boolean)) which sets to true or false.
I have an object clsObject
Dim clsObject As Object 'Object to hold parent form
If Me.ParentForm.Name = "frm1" Then
clsObject = CType(Me.ParentForm, frm1)
ElseIf Me.ParentForm.Name = "frm2" Then
clsObject = CType(Me.ParentForm, frm2)
End If
clsObject.setGood(true) 'This works fine
' Dim x = clsObject.isGood -Doesnt work
Dim sType As Type = clsObject.GetType
Dim tempGood
tempGood = CType(Me.ParentForm, Object).isGood 'Doesnt work - "Public member 'isGood' on type 'frm1' not found."
tempGood = CType(Me.ParentForm, stype).isGood 'Doesnt work - Doesnt understand Stype
'DirectCast(CType(Me.Paren
the only way i can get it to work is
DirectCast(CType(Me.Parent
but that is not generic and the parent form of the udc WILL change
How do i get the value of clsObject.isGood ??
ASKER
not sure i follow.
I mean, i can write a method in each form which will use this like getGood() returns boolean, but it is autogenerated, with new forms, and i would rather find a workaround
why would i be able to call functions [ clsObject.setGood(true) ] , but not varaibles [ x = clsObject.isGood ]
?
I mean, i can write a method in each form which will use this like getGood() returns boolean, but it is autogenerated, with new forms, and i would rather find a workaround
why would i be able to call functions [ clsObject.setGood(true) ] , but not varaibles [ x = clsObject.isGood ]
?
You need to define an interface that your forms implement. Then you simply work with the interface type rather than the concrete form types. For example:
Then in your forms:
Now you don't have to do any type checking...at least not the way you are doing. You simply work with a reference to the interface:
Public Interface IGood
IsGood As Boolean
End Interface
Then in your forms:
Public Class frm1
Inherits Form
Implements IGood
...
Public Property IsGood As Boolean
End Class
Public Class frm2
Inherits Form
Implements IGood
...
Public Property IsGood As Boolean
End Class
Now you don't have to do any type checking...at least not the way you are doing. You simply work with a reference to the interface:
Dim frm As IGood = TryCast(Me.ParentForm, IGood)
If frm IsNot Nothing Then
Dim x = clsObject.isGood
End If
I suspect the method from kaufmed is neater and suitable for you.
To explain a little more about mine.
Currently in the parent form you have a method - IsGood() which returns a boolean. Instead of that you have a custom object, that object has a method IsGood which returns a boolean.
In ParentForm
MyOb Settings
Now when you create your child form you just pass a reference to this custom object, call it ParentSettings.
In ChildForm
MyOb ParentSettings - you set this equal to Settings in the parent form when the parent creates this child form.
Now to see if the IsGood is true you do not need to do anything about what the parent is, instead you just call one line - ParentSettings.IsGood().
To explain a little more about mine.
Currently in the parent form you have a method - IsGood() which returns a boolean. Instead of that you have a custom object, that object has a method IsGood which returns a boolean.
In ParentForm
MyOb Settings
Now when you create your child form you just pass a reference to this custom object, call it ParentSettings.
In ChildForm
MyOb ParentSettings - you set this equal to Settings in the parent form when the parent creates this child form.
Now to see if the IsGood is true you do not need to do anything about what the parent is, instead you just call one line - ParentSettings.IsGood().
ps. It is a bit like a global variable, but it isn't global because you are containing it inside the form class as a class level variable - so you control what can access it.
ASKER
that causes a problem for me, by having to rewrite all the parents.
like i mentioned earlier, all the parentforms are generated with preset code, altering that program is not possible.
I'm looking for a solution within my udc form, without having to rewrite anything else
there was code which i lost that was able to accomplish catching the type of an object, and send it to directCast function
i was trying to remember it and came up with Dim sType As Type = clsObject.GetType, but that is also giving me issues
like i mentioned earlier, all the parentforms are generated with preset code, altering that program is not possible.
I'm looking for a solution within my udc form, without having to rewrite anything else
there was code which i lost that was able to accomplish catching the type of an object, and send it to directCast function
i was trying to remember it and came up with Dim sType As Type = clsObject.GetType, but that is also giving me issues
...by having to rewrite all the parents.Er...not really. An interface just defines a contract. It says, "the class implementing me shall have the following members." If all of your classes have the same property--namely IsGood, a Boolean property--then you are really only adding one line of code to each class: the Implements line.
Now, if the issue is that you have auto-generated classes. then typically code generators generate classes as partial classes. If yours have been created as such, then you can simply add another partial class definition to your project. This partial class definition will have the same exact name as your other class(es) [e.g. Public Partial Class frm1], and you can add your interface implementation line to the partial class definition that you created, not the one that the generator created.
e.g.
' Generated Code
Public Partial Class frm1
' generated code
...
Public Property IsGood As Boolean
End Class
' Your Definition
Public Partial Class frm1
Implements IGood
' Don't need to add anything here if the generated code already defines "IsGood"
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Obviously it's your code, and we're not the ones that will have to maintain it, but I really think reflection is the wrong approach here. But if it works, then so be it.
>>that causes a problem for me, by having to rewrite all the parents.
>>like i mentioned earlier, all the parentforms are generated with preset code
Maybe I misunderstand but don't you have to add this IsGood functionality to the parent forms anyway.
ps. If you have something that is generating the parent forms on a base form with this IsGood (so only one copy of that code) already in place - then you just cast to that common base class, not the specific parent class.
>>like i mentioned earlier, all the parentforms are generated with preset code
Maybe I misunderstand but don't you have to add this IsGood functionality to the parent forms anyway.
ps. If you have something that is generating the parent forms on a base form with this IsGood (so only one copy of that code) already in place - then you just cast to that common base class, not the specific parent class.
ASKER
It was a combination of that, and setting the value to PUBLIC instead of friend
ASKER
thanks for playing
You could have a custom object that is a member variable of the parent forms and pass a reference to this object into the child form as part of the creation. Then store anything you want common access to into this custom class object, rather than the forms.
(With classical windows programming you would just pass a message, simple and object type independent - but not so simple AFAIK in .net forms).