Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global classes 2

Status
Not open for further replies.

Nademic

Programmer
Mar 25, 2008
18
0
0
CA
Hi,

I am creating a program that has two forms. The first form lets you select the employee you want to make changes to and passes the employees record number to another form. The other form looks up that record number and displays the information for that employee.

I can do this easily with a module but would like to use a class instead(since they are all the rage nowadays :)). The only thing is that I can not access the class outside of the first form. I can create a new instance on the 2nd form but that will erase the value set by the first form. Any ideas about how to make this class global to all forms? My class code is below. Thanks in advance.

============================================================

Code:
Class RecordNumberBuffer
    Private m_recordNumber As Integer

    Public Property RecordNumber() As Integer

        Get
            Return m_recordNumber
        End Get
        Set(ByVal Value As Integer)
            m_recordNumber = Value
        End Set

    End Property

End Class
 
One way would be to pass the class as a parameter in the constructor to the second form.


as in:

[tt]
Public Class Form2
...
...

Private MyRecord As MyClass
Public Sub New (TheRecord As MyClass)

InitializeComponent() 'this will be entered automatically for you

MyRecord = TheRecord

End Sub
...
...
...
End Class
[/tt]


Then you will be able to use the class as MyRecord throughout Form2


Hope this helps.

[vampire][bat]
 
There are several wways to go about it, some good others really bad.

Lets start with the bad.
1) You could make you class shared. This way you don't have to instantiate it anymore. Then it looks and acts like a module.

2) you could make it a singleton (plenty of examples on google)

3) You could make a module and hold an instance there

4) You could make a public variable on form1 and call it from form2

5) ... the list goes on and on and on and on.

And now for some good ways of doing it.

1) You create a third class, let's call it controller. The third class holds an instance of your class. the controller is a singleton and both forms can get to it. The controller has a property to access your class.

2) You create a third class, let's call it controller. The third class holds an instance of your class. the forms have a constructor that has a parameter so that you can pass them the controller. The forms get opened by a fourth class that holds an instance of your controller (let's call it navigator. The controller has a property to access your class.

3) You create a third class, let's call it controller. The third class holds an instance of your class. the forms have a constructor that has a parameter so that you can pass them the controller. You use IoC/DI (like structureMap, castle windsor, spring.net,...) and you inject the controller in the forms. The controller has a property to access your class.

I prefer number 3 of the good options.





Christiaan Baes
Belgium

My Blog
 
BTW. The designer doesn't like it when you create a constructor like earthandfire did. You will still need to have this.

Public Sub New ()

InitializeComponent()

End Sub

Christiaan Baes
Belgium

My Blog
 
chrissie, I've never had any problems.

As soon as you type:

[tt]Public Sub New[/tt] and press enter, the designer adds the InitializeComponent() call and a couple of comments, then you just add the parameter in brackets after New.


Hope this helps.



[vampire][bat]
 
I did.

And I learned to keep the default constructor as clean as possible.

Anyway, DI really helps here.



Christiaan Baes
Belgium

My Blog
 
chrissie, I wasn't disagreeing with you - I daren't, you are The Chrissie [wink].

MVC requires a lot of work and needs to be carefully planned in advance. If a lot of development has already been done on a project, then my feeling is to continue as you are and regard MVC as the way forward when rewriting in the future and for future projects.

Additionally, in a small, "cheap and cheerful" project, MVC is probably overkill.

Personally, I look at each project individually and attempt to determine the best way to proceed for that particular project - of course, as I've now escaped from the land of the J word, I have a lot more freedom!!

[vampire][bat]
 
Nademic, if your project is at a point where you can realistically implement chrissie's suggestions, then I would recommend that that would way to go.


Hope this helps.

[vampire][bat]
 
Yeah you are right. But A bit of refactoring from time to time can save you work in the end.

And little projects always tend to grow up into big mean applications that cost lots of money. (And they used to be so sweet).

Christiaan Baes
Belgium

My Blog
 
Wow you guys are way ahead of me :). You really know your stuff. Going to look into MVC. This program is going to be one that overgoes alot of changes and I have read that MVC is a good approach for an app of this type. I have just started this project so might as well take a little exra time in the beginning to save time in the long run. Thanks for the advice and take care :).
 
If you have just started the project, and, as I said earlier, unless it is a very small "cheap and cheerful" project then a proper structured approach is definitely the way to go.


Be warned, getting started in something like MVC is hard work - but it does pay dividends in the end. Subsequent upgrades, bug-fixing and general maintenance are all far easier and more intuitive.


Best of luck, and have fun.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top