468,503 Members | 2,147 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,503 developers. It's quick & easy.

Passing a form object in a function

Hi,
If I want to pass a form (forms I have created) to a
function what would I set the declaration as

for example

Dim p as existingForm
OpenForm (p, existingForm)
Function (P as windows.forms.form, thisForm as *******)
p=NEW thisForm
.....

What do I put in *******?
Nov 20 '05 #1
10 3785
as ExistingForm. If I'm reading this right, you don't even need to pass the
P.
"Sebastian Santacroce" <se******@ilogic.com> wrote in message
news:82****************************@phx.gbl...
Hi,
If I want to pass a form (forms I have created) to a
function what would I set the declaration as

for example

Dim p as existingForm
OpenForm (p, existingForm)
Function (P as windows.forms.form, thisForm as *******)
p=NEW thisForm
....

What do I put in *******?

Nov 20 '05 #2
Dim p as existingForm

OpenForm(p)

Function(ByRef theForm as existingForm)
End Function
"Sebastian Santacroce" <se******@ilogic.com> wrote in message
news:82****************************@phx.gbl...
Hi,
If I want to pass a form (forms I have created) to a
function what would I set the declaration as

for example

Dim p as existingForm
OpenForm (p, existingForm)
Function (P as windows.forms.form, thisForm as *******)
p=NEW thisForm
....

What do I put in *******?

Nov 20 '05 #3
Sebastian Santacroce wrote:
Hi,
If I want to pass a form (forms I have created) to a
function what would I set the declaration as

for example

Dim p as existingForm
OpenForm (p, existingForm)
Function (P as windows.forms.form, thisForm as *******)
p=NEW thisForm
....

What do I put in *******?


If I read this right, you want to have a function that create an instance of
a type, without knowing a priori exactly what that type will be. Because
..Net doesn't have generics (yet), the only way to do this is using
reflection. Note that the example below will be slower than just using 'p =
new existingForm'.

First, I'd think the design of your program through to decide if you
*really* need this. But if you must do it, this is how:

Imports System.Reflection

Public Sub OpenForm(ByVal p As Windows.Forms.Form, ByVal thisForm As Type)
Dim ci As ConstructorInfo = thisForm.GetConstructor(Type.EmptyTypes)
p = ci.Invoke(Nothing)
' do something with p.
End Sub

Dim p As existingForm
OpenForm(p, Type.GetType("YourNamespace.existingForm"))

Again, only do this if you have absolutely no alternative. It is ugly code,
hell to maintain, not very performant, and not type-safe.

--
Sven Groot

http://unforgiven.bloghorn.com

Nov 20 '05 #4
* "Sebastian Santacroce" <se******@ilogic.com> scripsit:
If I want to pass a form (forms I have created) to a
function what would I set the declaration as

for example

Dim p as existingForm
OpenForm (p, existingForm)
Function (P as windows.forms.form, thisForm as *******)
p=NEW thisForm
....

What do I put in *******?


I am not sure what you want to do, but maybe you are looking for this:

\\\
Private Function CreateClassByName( _
ByVal PartialAssemblyName As String, _
ByVal QualifiedClassName As String _
) As Object
Return _
Activator.CreateInstance( _
[Assembly].LoadWithPartialName( _
PartialAssemblyName _
).GetType(QualifiedClassName) _
)
End Function
///

Usage:

\\\
Dim c As Control = _
DirectCast( _
CreateClassByName( _
"System.Windows.Forms", _
"System.Windows.Forms.Button" _
), _
Control _
)
With c
.Location = New Point(10, 10)
.Size = New Size(80, 26)
.Text = "Hello World"
End With
Me.Controls.Add(c)
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5
This is exactly what I was looking for but its not
working.
The
Type.GetType("YourNamespace.existingForm")

is returning "Nothing".
What would I put under "YourNamespace"?
would one always exist.
This solution has only one project and I haven't created
any namespaces but the form is in a solution folder.
Is there something I'm missing?

Thanks.
-----Original Message-----
Sebastian Santacroce wrote:
Hi,
If I want to pass a form (forms I have created) to a
function what would I set the declaration as

for example

Dim p as existingForm
OpenForm (p, existingForm)
Function (P as windows.forms.form, thisForm as *******)
p=NEW thisForm
....

What do I put in *******?
If I read this right, you want to have a function that

create an instance ofa type, without knowing a priori exactly what that type will be. Because..Net doesn't have generics (yet), the only way to do this is usingreflection. Note that the example below will be slower than just using 'p =new existingForm'.

First, I'd think the design of your program through to decide if you*really* need this. But if you must do it, this is how:

Imports System.Reflection

Public Sub OpenForm(ByVal p As Windows.Forms.Form, ByVal thisForm As Type) Dim ci As ConstructorInfo = thisForm.GetConstructor (Type.EmptyTypes) p = ci.Invoke(Nothing)
' do something with p.
End Sub

Dim p As existingForm
OpenForm(p, Type.GetType("YourNamespace.existingForm"))

Again, only do this if you have absolutely no alternative. It is ugly code,hell to maintain, not very performant, and not type-safe.

--
Sven Groot

http://unforgiven.bloghorn.com

.

Nov 20 '05 #6
"Sebastian Santacroce" <se*******@ilogic.com> schrieb
This is exactly what I was looking for but its not
working.
The
Type.GetType("YourNamespace.existingForm")

is returning "Nothing".
What would I put under "YourNamespace"?
would one always exist.
This solution has only one project and I haven't created
any namespaces but the form is in a solution folder.
Is there something I'm missing?


Open the class view or the object browser.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #7
Sebastian Santacroce wrote:
This is exactly what I was looking for but its not
working.
The
Type.GetType("YourNamespace.existingForm")

is returning "Nothing".
What would I put under "YourNamespace"?
would one always exist.


The default root namespace for a VB.NET project is always the project name,
unless specified otherwise in the project properties.

--
Sven Groot

http://unforgiven.bloghorn.com

Nov 20 '05 #8
Sven Groot wrote:
Public Sub OpenForm(ByVal p As Windows.Forms.Form, ByVal thisForm As
Type)


Upon re-reading, it occurred to me you probably want the form instance to be
accessible through the passed first parameter once OpenForm has returned. In
which case you should put:
Public Sub OpenForm(ByRef p As Windows.Forms.Form, ByVal thisForm As Type)

Note the ByRef instead of ByVal for the first parameter.

--
Sven Groot

http://unforgiven.bloghorn.com

Nov 20 '05 #9
So that worked great now.
I need to check if the form has been closed ie, the
variable has been disposed so I can recreate the form.

How would I check if the variable is disposed?
-----Original Message-----
Sebastian Santacroce wrote:
This is exactly what I was looking for but its not
working.
The
Type.GetType("YourNamespace.existingForm")

is returning "Nothing".
What would I put under "YourNamespace"?
would one always exist.
The default root namespace for a VB.NET project is always

the project name,unless specified otherwise in the project properties.

--
Sven Groot

http://unforgiven.bloghorn.com

.

Nov 20 '05 #10
Scratch that last dispose question. Figured it out. I used
Canfocus property of form to detect if its set to dispose.
I basically wanted to not have the same form open twice
which works fine now for forms without contructor
parameters.

However, regarding the function you gave I've added an
optional parameter to give to the form contructor an array
of types but it always gives the error: "Object reference
not set to an instance of an object".Heres the function:

Public Sub ShowForm(ByRef objForm As Windows.Forms.Form,
ByVal objFormInstance As Type, Optional ByVal Params As
Object() = Nothing) '

If IsNothing(objForm) OrElse Not objForm.CanFocus
Then
Dim ci As ConstructorInfo =
objFormInstance.GetConstructor(Type.EmptyTypes)
objForm = ci.Invoke(Params)
objForm.Show()
Else
objForm.Activate()
End If

End Sub

This particular form's contructor takes one parameter of
type string and I want to set it to "go"
The Call to the above function is:
Dim formVariable as FormName
Dim ParamArr(0) As Object
ParamArr(0) = "go"

Call ShowForm(formVariable, Type.GetType
("Namespace.FormName"), ParamArr)

Whats causeing that Error? Am I passing the array
incorrectly? or my declaring it wrong?
Thanks,
-----Original Message-----
Sebastian Santacroce wrote:
This is exactly what I was looking for but its not
working.
The
Type.GetType("YourNamespace.existingForm")

is returning "Nothing".
What would I put under "YourNamespace"?
would one always exist.
The default root namespace for a VB.NET project is always

the project name,unless specified otherwise in the project properties.

--
Sven Groot

http://unforgiven.bloghorn.com

.

Nov 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by domeceo | last post: by
6 posts views Thread by Mark Anderson | last post: by
12 posts views Thread by Andrew Bullock | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.