classes to perform functions for many forms

G

Guest

I have alot of event procedures, variables and methods I do in many forms. I thought I'd try to call class functions to do them. I started with an extremely simple example. A start form has two buttons. One opens form1 the other form2. Click a button and it sets an integer in the class and opens the form. In each form 3 public integers are defined. Two have values. In the form1 load event I instantiate class1 and call the add procedure c = cls1.ad

In class1 the add function checks the integer. If 1 it instantiates form1, if 2 form2. Then it attempts to add frm.a + frm.b

Unsurprisingly, the project can't build, telling me that a and b are not members of class1

If I have to pass a and b, or set them as public integers in class1, then how is it possible to build classes that can do complicated things for many forms? There must be a way, because the dataform wizard does something like it

The code is below. Any help would be appreciated

polynomial5


Public Class StartFor
Inherits System.Windows.Forms.For
Dim cls1 As New Class

#Region " Windows Form Designer generated code

Private Sub btnForm1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForm1.Clic
cls1.iForm =
Dim frm As New Form
frm.Show(
End Su

Private Sub btnForm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForm2.Clic
cls1.iForm =
Dim frm As New Form
frm.Show(
End Su
End Clas

Public Class Form
Inherits System.Windows.Forms.For
Public a As Integer =
Public b As Integer =
Public c As Intege

#Region " Windows Form Designer generated code

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loa
Dim cls1 As New Class
c = cls1.Ad
MsgBox(c.ToString
End Su
End Clas

form 2 is almost identica

Public Class Class
Public iForm As Intege

Public Function Add() As Intege
Dim frm As For

Select Case iFor
Case
frm = New Form

Case
frm = New Form

Case Els
MsgBox("No such form"

End Selec

Add = frm.a + frm.

End Functio

End Class
 
T

Thomas

Dim frm As Form - this gives you a reference to a base form.
Your instaces - Form1 and Form2 are decendants of the base form, but adds
new properties and methods.
When you use the base class - Form - to work with them, you only have
access to the base properties and methods, not the one you've added.

Here's a quick example of what you are looking for:

Public Class Form1
....

Public Class Form2
....


Dim f1 as Form1 <---- must declare them using correct class - or typecast
them from a Form to a Form1 (or Form2) when needed
Dim f2 as Form2
Dim x = f1.a + f2.b


Other alternative is to create a Interface and have each form implement that
interface:

Dim f1 as IMyForm
Dim f2 as IMyForm

Dim x = f1.a + f2.b




polynomial5d said:
I have alot of event procedures, variables and methods I do in many forms.
I thought I'd try to call class functions to do them. I started with an
extremely simple example. A start form has two buttons. One opens form1
the other form2. Click a button and it sets an integer in the class and
opens the form. In each form 3 public integers are defined. Two have
values. In the form1 load event I instantiate class1 and call the add
procedure c = cls1.add
In class1 the add function checks the integer. If 1 it instantiates
form1, if 2 form2. Then it attempts to add frm.a + frm.b.
Unsurprisingly, the project can't build, telling me that a and b are not members of class1.

If I have to pass a and b, or set them as public integers in class1, then
how is it possible to build classes that can do complicated things for many
forms? There must be a way, because the dataform wizard does something like
it.
The code is below. Any help would be appreciated.

polynomial5d



Public Class StartForm
Inherits System.Windows.Forms.Form
Dim cls1 As New Class1

#Region " Windows Form Designer generated code "


Private Sub btnForm1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnForm1.Click
cls1.iForm = 1
Dim frm As New Form1
frm.Show()
End Sub

Private Sub btnForm2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnForm2.Click
cls1.iForm = 2
Dim frm As New Form2
frm.Show()
End Sub
End Class

Public Class Form1
Inherits System.Windows.Forms.Form
Public a As Integer = 5
Public b As Integer = 6
Public c As Integer

#Region " Windows Form Designer generated code "

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
 
G

Guest

Thanks muchly. That answers my question. And while getting a class to do work for many forms remains much more difficult than in vb6, at least it's doable

polynomial5d
 
S

Steve Moores

Hi there,

You can also define class members as Shared, that way you don't have to
instantiate an instance of the class to use the public members you've set
up.

It's not a good practice, but it sort of mimics the direct
ClassX.FunctionName syntax you're use to in VB6.

polynomial5d said:
Thanks muchly. That answers my question. And while getting a class to do
work for many forms remains much more difficult than in vb6, at least it's
doable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top