473,386 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to cast a Form

I have two forms. Each contains a property, say Prop.

I do
Public FormBeingUsed as Form
..
..
..
Then I do
FormBeingUsed= Form1
or maybe
FormBeingUsed=Form2
..
..
..
then I want to do
FormBeingUsed.Prop=123

Of course, because Form does not have a property Prop the compiler complains

Is there a good way to do this?
Thanks
Nov 21 '05 #1
13 1492
Just Me,
I have two forms. Each contains a property, say Prop.
Is there a good way to do this?
Polymorphism, one of the tenants of OO, is a good way to do this. You can
use either Class Inheritance or Interface Implementation to achieve this
Polymorphism.

' Class Inheritance

Public Class BaseForm
Inherits System.Windows.Forms.Form

Public Property Prop As Integer
...
End Class
Public Class Form1
Inherits BaseForm
...
End Class

Public Class Form2
Inherits BaseForm
...
End Class

Public FormBeingUsed as BaseForm

FormBeingUsed= Form1
FormBeingUsed.Prop=123

FormBeingUsed=Form2
FormBeingUsed.Prop=123

' Interface Implementation

Public Interface IProp

Public Property Prop As Integer
...
End Interface
Public Class Form1
Inherits System.Windows.Forms.Form
Implements IProp

Public Property Prop As Integer Implements IProp.Prop
...
End Class

Public Class Form2
Inherits System.Windows.Forms.Form
Implements IProp

Public Property Prop As Integer Implements IProp.Prop
...
End Class

Public FormBeingUsed as IProp

FormBeingUsed= Form1
FormBeingUsed.Prop=123

FormBeingUsed=Form2
FormBeingUsed.Prop=123
You could define Prop to be Overridable in BaseForm or define an
OnPropChanged method that the BaseForm.Prop.Set method calls when the value
changes if you need derived forms to know about the value of Prop
changing...

I would favor Class Inheritance over Interface Implementation as normally
Class Inheritance will produce less code.

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:u1**************@TK2MSFTNGP09.phx.gbl...I have two forms. Each contains a property, say Prop.

I do
Public FormBeingUsed as Form
.
.
.
Then I do
FormBeingUsed= Form1
or maybe
FormBeingUsed=Form2
.
.
.
then I want to do
FormBeingUsed.Prop=123

Of course, because Form does not have a property Prop the compiler
complains

Is there a good way to do this?
Thanks

Nov 21 '05 #2
" Just Me" <gr****@a-znet.com> schrieb:
I have two forms. Each contains a property, say Prop.

I do
Public FormBeingUsed as Form
.
.
.
Then I do
FormBeingUsed= Form1
or maybe
FormBeingUsed=Form2
.
.
.
then I want to do
FormBeingUsed.Prop=123

Of course, because Form does not have a property Prop the compiler
complains

Is there a good way to do this?


\\\
Public Interface IUsable
Property Used As Boolean
End Interface

Public Class Form1
Inherits Form
Implements IUsable

Public Property Used() As Boolean Implements IUsable.Used
...
End Property
...
End Class

Public Class Form2
...
End Class
..
..
..
Dim f As IUsable
If...Then
f = New Form1()
Else
f = New Form2()
End If
f.Used = True
///

Another approach would use a common base class that adds a 'Used' property
to the form class and from which 'Form1' and 'Form2' inherit.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
Just Me,
I have two forms. Each contains a property, say Prop.
Is there a good way to do this?
Polymorphism, one of the tenants of OO, is a good way to do this. You can
use either Class Inheritance or Interface Implementation to achieve this
Polymorphism.

' Class Inheritance

Public Class BaseForm
Inherits System.Windows.Forms.Form

Public Property Prop As Integer
...
End Class
Public Class Form1
Inherits BaseForm
...
End Class

Public Class Form2
Inherits BaseForm
...
End Class

Public FormBeingUsed as BaseForm

FormBeingUsed= Form1
FormBeingUsed.Prop=123

FormBeingUsed=Form2
FormBeingUsed.Prop=123

' Interface Implementation

Public Interface IProp

Public Property Prop As Integer
...
End Interface
Public Class Form1
Inherits System.Windows.Forms.Form
Implements IProp

Public Property Prop As Integer Implements IProp.Prop
...
End Class

Public Class Form2
Inherits System.Windows.Forms.Form
Implements IProp

Public Property Prop As Integer Implements IProp.Prop
...
End Class

Public FormBeingUsed as IProp

FormBeingUsed= Form1
FormBeingUsed.Prop=123

FormBeingUsed=Form2
FormBeingUsed.Prop=123
You could define Prop to be Overridable in BaseForm or define an
OnPropChanged method that the BaseForm.Prop.Set method calls when the value
changes if you need derived forms to know about the value of Prop
changing...

I would favor Class Inheritance over Interface Implementation as normally
Class Inheritance will produce less code.

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:u1**************@TK2MSFTNGP09.phx.gbl...I have two forms. Each contains a property, say Prop.

I do
Public FormBeingUsed as Form
.
.
.
Then I do
FormBeingUsed= Form1
or maybe
FormBeingUsed=Form2
.
.
.
then I want to do
FormBeingUsed.Prop=123

Of course, because Form does not have a property Prop the compiler
complains

Is there a good way to do this?
Thanks

Nov 21 '05 #4
Every once in a while I ask a question that results in especially
interesting answers.

Answers that not only solve but have some academically interesting value.

I think these answers both helped and stimulated.

Thanks

" Just Me" <gr****@a-znet.com> wrote in message
news:u1**************@TK2MSFTNGP09.phx.gbl...
I have two forms. Each contains a property, say Prop.

I do
Public FormBeingUsed as Form
.
.
.
Then I do
FormBeingUsed= Form1
or maybe
FormBeingUsed=Form2
.
.
.
then I want to do
FormBeingUsed.Prop=123

Of course, because Form does not have a property Prop the compiler
complains

Is there a good way to do this?
Thanks

Nov 21 '05 #5
Every once in a while I ask a question that results in especially
interesting answers.

Answers that not only solve but have some academically interesting value.

I think these answers both helped and stimulated.

Thanks

" Just Me" <gr****@a-znet.com> wrote in message
news:u1**************@TK2MSFTNGP09.phx.gbl...
I have two forms. Each contains a property, say Prop.

I do
Public FormBeingUsed as Form
.
.
.
Then I do
FormBeingUsed= Form1
or maybe
FormBeingUsed=Form2
.
.
.
then I want to do
FormBeingUsed.Prop=123

Of course, because Form does not have a property Prop the compiler
complains

Is there a good way to do this?
Thanks

Nov 21 '05 #6
What's going on below. It looks like late binding is used for sender.Index
since Object does not have an index property.

Private Sub HandleTool_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim Index As Integer = sender.Index

If so, why isn't that a solution to the problem I posted below?
That is, using: Public FormBeingUsed as Object

Thanks again
" Just Me" gr****@a-znet.com> wrote in message
I have two forms. Each contains a property, say Prop.

I do
Public FormBeingUsed as Form
.
.
.
Then I do
FormBeingUsed= Form1
or maybe
FormBeingUsed=Form2
.
.
.
then I want to do
FormBeingUsed.Prop=123

Of course, because Form does not have a property Prop the compiler
complains

Is there a good way to do this?
Thanks

Nov 21 '05 #7
What's going on below. It looks like late binding is used for sender.Index
since Object does not have an index property.

Private Sub HandleTool_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim Index As Integer = sender.Index

If so, why isn't that a solution to the problem I posted below?
That is, using: Public FormBeingUsed as Object

Thanks again
" Just Me" gr****@a-znet.com> wrote in message
I have two forms. Each contains a property, say Prop.

I do
Public FormBeingUsed as Form
.
.
.
Then I do
FormBeingUsed= Form1
or maybe
FormBeingUsed=Form2
.
.
.
then I want to do
FormBeingUsed.Prop=123

Of course, because Form does not have a property Prop the compiler
complains

Is there a good way to do this?
Thanks

Nov 21 '05 #8
Just Me,
It looks like late binding to me. Do you have Option Strict On at the top of
the source file?

I use Option Strict On at the top of every source file as I prefer compile
time errors over hard to track down run time errors.

Where I need to use late binding (such as COM interop with CDO), I isolate
the late binding to a small isolated module in the far corner of my app...
If so, why isn't that a solution to the problem I posted below?
That is, using: Public FormBeingUsed as Object Declaring a variable as Object does not enable late binding, you need to use
Option Strict Off to enable late binding, when you use late binding you run
the risk (a potentially high risk) have having hard to track down run time
errors, instead of obvious compile time errors. Not to mention that late
binding is generally a couple of magnitudes slower then early binding...

Unfortunately Option Strict Off is the default under project properties. I
normally include Option Strict On (or Off) in each source file, so its
obvious to other developers (who may link to the file) that the file does or
does not intend on using late binding...

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:uv**************@tk2msftngp13.phx.gbl... What's going on below. It looks like late binding is used for sender.Index
since Object does not have an index property.

Private Sub HandleTool_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim Index As Integer = sender.Index

If so, why isn't that a solution to the problem I posted below?
That is, using: Public FormBeingUsed as Object

Thanks again
" Just Me" gr****@a-znet.com> wrote in message
I have two forms. Each contains a property, say Prop.

I do
Public FormBeingUsed as Form
.
.
.
Then I do
FormBeingUsed= Form1
or maybe
FormBeingUsed=Form2
.
.
.
then I want to do
FormBeingUsed.Prop=123

Of course, because Form does not have a property Prop the compiler
complains

Is there a good way to do this?
Thanks


Nov 21 '05 #9
Just Me,
It looks like late binding to me. Do you have Option Strict On at the top of
the source file?

I use Option Strict On at the top of every source file as I prefer compile
time errors over hard to track down run time errors.

Where I need to use late binding (such as COM interop with CDO), I isolate
the late binding to a small isolated module in the far corner of my app...
If so, why isn't that a solution to the problem I posted below?
That is, using: Public FormBeingUsed as Object Declaring a variable as Object does not enable late binding, you need to use
Option Strict Off to enable late binding, when you use late binding you run
the risk (a potentially high risk) have having hard to track down run time
errors, instead of obvious compile time errors. Not to mention that late
binding is generally a couple of magnitudes slower then early binding...

Unfortunately Option Strict Off is the default under project properties. I
normally include Option Strict On (or Off) in each source file, so its
obvious to other developers (who may link to the file) that the file does or
does not intend on using late binding...

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:uv**************@tk2msftngp13.phx.gbl... What's going on below. It looks like late binding is used for sender.Index
since Object does not have an index property.

Private Sub HandleTool_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim Index As Integer = sender.Index

If so, why isn't that a solution to the problem I posted below?
That is, using: Public FormBeingUsed as Object

Thanks again
" Just Me" gr****@a-znet.com> wrote in message
I have two forms. Each contains a property, say Prop.

I do
Public FormBeingUsed as Form
.
.
.
Then I do
FormBeingUsed= Form1
or maybe
FormBeingUsed=Form2
.
.
.
then I want to do
FormBeingUsed.Prop=123

Of course, because Form does not have a property Prop the compiler
complains

Is there a good way to do this?
Thanks


Nov 21 '05 #10
I do have Option Strict Off.
I didn't know late binding was possible.

Thanks for the info

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OD**************@tk2msftngp13.phx.gbl...
Just Me,
It looks like late binding to me. Do you have Option Strict On at the top
of the source file?

I use Option Strict On at the top of every source file as I prefer compile
time errors over hard to track down run time errors.

Where I need to use late binding (such as COM interop with CDO), I isolate
the late binding to a small isolated module in the far corner of my app...
If so, why isn't that a solution to the problem I posted below?
That is, using: Public FormBeingUsed as Object

Declaring a variable as Object does not enable late binding, you need to
use Option Strict Off to enable late binding, when you use late binding
you run the risk (a potentially high risk) have having hard to track down
run time errors, instead of obvious compile time errors. Not to mention
that late binding is generally a couple of magnitudes slower then early
binding...

Unfortunately Option Strict Off is the default under project properties. I
normally include Option Strict On (or Off) in each source file, so its
obvious to other developers (who may link to the file) that the file does
or does not intend on using late binding...

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:uv**************@tk2msftngp13.phx.gbl...
What's going on below. It looks like late binding is used for
sender.Index since Object does not have an index property.

Private Sub HandleTool_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim Index As Integer = sender.Index

If so, why isn't that a solution to the problem I posted below?
That is, using: Public FormBeingUsed as Object

Thanks again
" Just Me" gr****@a-znet.com> wrote in message
I have two forms. Each contains a property, say Prop.

I do
Public FormBeingUsed as Form
.
.
.
Then I do
FormBeingUsed= Form1
or maybe
FormBeingUsed=Form2
.
.
.
then I want to do
FormBeingUsed.Prop=123

Of course, because Form does not have a property Prop the compiler
complains

Is there a good way to do this?
Thanks



Nov 21 '05 #11
Just Me,
Late binding is possible, but it is not recommended.

I only use it in very select cases, which normally involves COM interop to
COM objects that are intended for VBScript (all parameters & properties are
type Object). Even then I try to isolate the late binding or encapsulate it
in classes where I have a heavy dose of DirectCasts (casting the property or
return value to its actual type).

Remember that late binding is inherently slower (as the method or property)
needs to be looked up at runtime rather compile time. These look ups can
fail, or an unexpected implicit conversion may occur which can create hard
to track down run time errors...

So in your case I strongly recommend using what Herfried & I initially
suggested!

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I do have Option Strict Off.
I didn't know late binding was possible.

Thanks for the info

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OD**************@tk2msftngp13.phx.gbl...
Just Me,
It looks like late binding to me. Do you have Option Strict On at the top
of the source file?

I use Option Strict On at the top of every source file as I prefer
compile time errors over hard to track down run time errors.

Where I need to use late binding (such as COM interop with CDO), I
isolate the late binding to a small isolated module in the far corner of
my app...
If so, why isn't that a solution to the problem I posted below?
That is, using: Public FormBeingUsed as Object

Declaring a variable as Object does not enable late binding, you need to
use Option Strict Off to enable late binding, when you use late binding
you run the risk (a potentially high risk) have having hard to track down
run time errors, instead of obvious compile time errors. Not to mention
that late binding is generally a couple of magnitudes slower then early
binding...

Unfortunately Option Strict Off is the default under project properties.
I normally include Option Strict On (or Off) in each source file, so its
obvious to other developers (who may link to the file) that the file does
or does not intend on using late binding...

Hope this helps
Jay

" Just Me" <gr****@a-znet.com> wrote in message
news:uv**************@tk2msftngp13.phx.gbl...
What's going on below. It looks like late binding is used for
sender.Index since Object does not have an index property.

Private Sub HandleTool_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim Index As Integer = sender.Index

If so, why isn't that a solution to the problem I posted below?
That is, using: Public FormBeingUsed as Object

Thanks again
" Just Me" gr****@a-znet.com> wrote in message
I have two forms. Each contains a property, say Prop.

I do
Public FormBeingUsed as Form
.
.
.
Then I do
FormBeingUsed= Form1
or maybe
FormBeingUsed=Form2
.
.
.
then I want to do
FormBeingUsed.Prop=123

Of course, because Form does not have a property Prop the compiler
complains

Is there a good way to do this?
Thanks



Nov 21 '05 #12
Just Me,

I miss something in the answers

FormBeingUsed= new Form2
if typeof formbeingused Is Form2 then
Directcast(FormBeingUsed,Form2).Prop=123
end if

When this was already told, than sorry

I hope this helps anyway something?

Cor

Nov 21 '05 #13
I already received good answers.
Thanks anyway!


"Cor Ligthert" <no************@planet.nl> wrote in message
news:uM**************@tk2msftngp13.phx.gbl...
Just Me,

I miss something in the answers

FormBeingUsed= new Form2
if typeof formbeingused Is Form2 then
Directcast(FormBeingUsed,Form2).Prop=123
end if

When this was already told, than sorry

I hope this helps anyway something?

Cor

Nov 21 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Brent | last post by:
I maintain a Sql Server database application whose forms are compiled in a .dll and kept in a remote web folder. The main .exe looks to the web folder to see if this .dll exists. If so, then it...
0
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception:...
3
by: Mike | last post by:
I am using MS-Access as a front end for my MS-SQL DB. I have a sql view that uses the following: SELECT TOP 100 PERCENT RECID, PATNUMBER AS , SVCCODE AS , QTY, PROF_CHRGS AS , AMOUNT,...
3
by: Chris | last post by:
Hi, I'm trying to append text from another class to a generic richTextBox that I've added to a Windows form. I can't seem to figure out how to expose the richTextBox to append text to it. ...
2
by: Ronny Van Assche | last post by:
Hi, I am working with an Mdiform and several Childforms who are loaded dynamically with reflection. Every time a child is getting activate i must reinitialize the menu. (i think) This is...
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
2
by: harvie wang | last post by:
Hi, I want to implement a common Form with special interface, such as MovePoint(double,double). I create a interface first: namespace ABC.Test { public Interface IMyWindowInterface { void...
1
by: ramhog | last post by:
I have a custom control created in C#. The control is being consumed in a COM exe written in VB 6. At runtime when a user moves the mouse over the control I want to get the parent form for the...
2
by: Mandrah | last post by:
I'm having problems casting input information from an XHTML form as a different type in a CGI script using Python. For just a simple example I'd use the XHTML code: <?xml version = "1.0" encoding =...
2
by: thierry savard | last post by:
I want the 2 compute column to show only 4 decimal, but I cant find a way to affect the return value of 'em. I wrote down this command "compute avg(x)" because I need the average to be in a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.