Basic questions on Classes - before I develop bad habits

P

Paul Bromley

I have written a similar enquiry to this newsgroup, but had no responses -
hence I will rephrase it with the hope that someone will answer.

I am new to using Classes, but trying hard to get the basics at the present
time, and seem to be winning.

Presently I am developing a Usercontrol for use in my application. To keep
it simple, let me give as an example - I have 3 command buttons in the User
Control. Each of these will have separate code in the click vent. However, I
wish to trigger a common event off in my external application once I press
one of these command buttons - i.e. I need to be able to create a new
instance of an existing class in my external application and send this
parameters. It may be a simple thing to do, but I am stuck - can soemone
send me a few clues as to how to achive this.

A few basic things on Classes that I need to get into my head:-

Is it bad practice to create n instance of class in the load event of a
form and re-use this?? Is best practice to create a new instance as and when
I need it.

Should I be setting the instance of the calss to Nothing once I have used
it.

I now understand about setting and getting the Properties within the Class
and using these properties in my application. The question is - can you do
the same with Methods, and if so how do you have acess to them externally,
or is this what I should be avoiding within a class? I was wondering whether
this was the way to go with my qustion at the top, but creating a Public Sub
in my Class does not expose it to my program.

Sorry if all the above appears rather obvious or silly to some. My
programming is set in the past, and may appear to be childish to others - I
am therefore trying to take this in hand, and to get Classes right from the
beginning. Anyone know of any useful on plain english links on Classes on
the Web?

Thanks in anticipation.

Paul Bromley
 
S

Simon Jefferies

Hello there,
Is it bad practice to create n instance of class in the load event of a
form and re-use this?? Is best practice to create a new instance as and
when
I need it.


There is nothing wrong in creating an instance of a class in any event, it
all depends on your usage. If the lifetime of the instance is to perform a
simple task and is limited to just the time inside your event then creating
an instance "locally" is fine.
Should I be setting the instance of the calss to Nothing once I have used
it.

There is nothing wrong in setting the instance afterwards, it is a good
habit - as it clearly shows that you have finished with the instance of the
class. I don't think it guarantees that the memory is released for it
though.
I now understand about setting and getting the Properties within the Class
and using these properties in my application. The question is - can you do
the same with Methods, and if so how do you have acess to them externally,
or is this what I should be avoiding within a class?

I prefer to use Property's and set-up the instance of the class up front.
Then call a function to perform an action e.g.

Dim Pic As New MyPicture
Pic.Width = 16
Pic.Height = 16
Pic.Load("etc.")

rather than:

Dim Pic As New MyPicture
Pic.Load("etc.", 16, 16)

Methods are only one way. Yes, you do need to set them as Public to be
called externally.

Also, I try to watch when writing Sub's and try to write Functions instead
(returning a boolean to indicate successful completion).
I was wondering whether
this was the way to go with my qustion at the top, but creating a Public
Sub
in my Class does not expose it to my program.

What keyword are you using on your user control class? Is it Friend, public
or private?

Hope this helps,
Simon Jefferies
Tools Programmer, Headfirst Productions
mailto:[email protected]
-
 
P

Paul Bromley

Hi Simon,

Thanks for your help so far on this one. I am using Public on my User
Control class. I understand what you are saying so far. My problem still
remains unanswered - I have Command buttons in my suer control. Each of
these has different code in the click event. I also want each of these Click
events however to be able to Instantiate a new instance of a Class in my
external application. I can achive this if I place the Class within the User
Control, as there are references to the Class. I am unsure however how to
Intsnatiate the Class in my application however, where the class ideally
needs to be as I need my application to also access this class. Doing it the
way that I can get it to work at the moment would mean my placing the code
for my Class in the control and in my application. Hope that you understand
what I am saying here. It is driving me nuts, as I know that there must be a
more elegant solution to the Class code being in 2 places.

Many thanks if you or anyone else can help.

Best iwshes

Paul Bromley
 
P

Paul Bromley

Hi Simon,

Thanks for your help so far on this one. I am using Public on my User
Control class. I understand what you are saying so far. My problem still
remains unanswered - I have Command buttons in my suer control. Each of
these has different code in the click event. I also want each of these Click
events however to be able to Instantiate a new instance of a Class in my
external application. I can achive this if I place the Class within the User
Control, as there are references to the Class. I am unsure however how to
Intsnatiate the Class in my application however, where the class ideally
needs to be as I need my application to also access this class. Doing it the
way that I can get it to work at the moment would mean my placing the code
for my Class in the control and in my application. Hope that you understand
what I am saying here. It is driving me nuts, as I know that there must be a
more elegant solution to the Class code being in 2 places.

Many thanks if you or anyone else can help.

Best iwshes

Paul Bromley
 
S

Simon Jefferies

Hello,

From my understanding I think you need to place a "reference" in your user
control. If you right click on references in the user control project, and
select Add Reference. From the Add Reference dialog, select the Projects
tab, select the external application from the list by double-clicking it.

This will now allow your user control project to have access to your classes
in the external application without having to include it etc.

Hope this helps further.
Simon Jefferies
Tools Programmer, Headfirst Productions
mailto:[email protected]
-
 
P

Paul Bromley

Hi Simon,

Thanks for your help. The only way that I can manage to do this is by
compiling the class to a DLL, and referencing that. This works fine, but I
would have thought that there may have been a way of using the class
externally as it was.

Best wishes

Paul Bromley
 

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