472,989 Members | 3,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 4258
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
6
by: Mark Anderson | last post by:
I have code to develop result page links (like a search engine) for some results being passed from a database where I've no server-sdide acces - thus JS. The code is below and works fine except...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
6
by: Max | last post by:
Last time I tried to explain this on another forum it didn't go too well, so I'll try my best and if you know what I'm talking about then please tell me how to do this. I have a class, inside I...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
13
by: Deano | last post by:
Apparently you can only do this with one value i.e Call MyAssetLocationZoom(Me!txtLocation, "Amend data") This runs; Public Sub MyAssetLocationZoom(ctl As Control, formName As String) On...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
2
by: mark.e.nelson | last post by:
Hi, I am trying to pass a pointer to a struct to a function that uses the data in the struct, and also happens to use ncurses. I always get a segmentation violation when the program exits. I...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.