473,379 Members | 1,355 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,379 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 4285
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?

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.