"hamil" <ha***@discussions.microsoft.com> wrote in message news:<BC**********************************@microso ft.com>...
Ken What you suggest works! Could you explain the problem in a bit more
detail. Your example is similar to what I wrote except that I tried to pass
the entire form object and you passed only the TestBox1 object.
Is there a way to pass the entire form so if I have a number of textboxes in
the form I can pass the entire form by setting one object variable?
I'm new to VB.net so excuse me if I am not saying things exactly correct.
The following should help clear things up a bit. When you design a
class, you can control the level of access for each class member.
Class members are class variables, properties, events, methods (subs
and functions), etc.
You can specify whether a class member is:
1) Public
2) Private
3) Protected
4) Friend
5) Protected Friend
This list is known as "modifiers", because they modify the member's
level of access.
For example:
Class MyClass
Private PrivateVar As Integer
Public PublicVar As Integer
Protected ProtectedVar As Integer
Friend FriendVar As Integer
End Class
Class MyOtherClass
Public Sub DoThis(mc As MyClass)
mc.PublicVar = 1 '<--- OK because it is Public
mc.PrivateVar = 1 '<-- illegal because this is private to
MyClass
mc.ProtectedVar = '<-- illegal because this is private to
MyClass and its derived classes
mc.FriendVar = 1 '<-- OK as long as MyOtherClass is in same
project/assembly as MyClass
End Sub
End Class
"Public" means that the member can be accessed from anywhere ...
inside or outside the class, and inside or outside the
project/assembly. This is the one end of the spectrum. In the example
above, MyOtherClass can access MyClass.PublicVar because it is Public.
"Private" means that the member can only be access from within the
class itself. This is the opposite end of the spectrum from Public. In
the example above, MyOtherClass cannot access MyClass.PrivateVar
because PrivateVar is private to MyClass; only code inside MyClass is
allowed to access PrivateVar.
"Protected" is the same as 'private', except that derived classes can
also access the member. Consider the following class:
Class MySimiliarClass
Inherits MyClass
Public Sub DoSomething()
Me.ProtectedVar = 1 '<--- OK because member is protected
(accessible by class and derived classes only)
Me.PublicVar = 1 '<--- OK because member is public
Me.PrivateVar = 1 '<-- illegal because member is private to
MyClass
Me.FriendVar = 1 '<-- OK as long as MySimiliarClass is in
same project/assembly as MyClass
End Sub
End Class
"Friend" is much like 'public' in that any code within the same
project/assembly can access Friend members. However, code outside the
project/assembly cannot access these members.
"Protected Friend" combines 'Protected' and 'Friend', such that
Protected Friend members can be accessed by any code inside the same
project/assembly and also by any derived classes that are defined in a
different project/assembly.
Now, to your TextBox issue ... In your form that contains your Button,
your text box control is defined as a "Private" member. Your form *is*
a class, and the TextBox is a member of your form's class. If you look
at the code generated by the form designer, you would see something
like the following:
Private TextBox1 As TextBox
Because this textbox member is 'Private', other classes (such as your
Class1 class) cannot access it. Remember, Private members can only be
accessed by code within the class itself. Therefore, your TextBox can
only be accessed by the code in your form.
Now, if you were to use the Properties window and change the TextBox
from 'Private' to 'Friend' or 'Public', then your Class1 could access
the textbox. ('Friend' would be better, because you want to use the
most restrictive modifier that provides the required level of access,
but not more than is needed).
The reason why it worked when passing the TextBox reference to the
routine instead of the form reference, is because in that case you are
not accessing the text box through the form object.
If you change the modifier for all text boxes you need to work with in
this manner from 'Private' to 'Friend', then your Class1 class would
be able to accept a reference to your form and manipulate those text
boxes as needed.
Does that help clarify things a bit?