473,378 Members | 1,621 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,378 software developers and data experts.

Can anyone explain Private vs. Public properties in Class modules

TheSmileyCoder
2,322 Expert Mod 2GB
I've started using class modules, and admittedly is quite inexperienced in this particular area, and there is one thing I can't seem to wrap my head around.

Part of the code is the property StartHeight. Get and Let shown below:
Expand|Select|Wrap|Line Numbers
  1. Public Property Let StartHeight(intValue As Integer)
  2.     pIntStartHeight = intValue
  3. End Property
  4. Public Property Get StartHeight() As Integer
  5.     StartHeight = pIntStartHeight
  6. End Property
At another point in my code I want to use:
Expand|Select|Wrap|Line Numbers
  1. Me.StartHeight=500
Which works fine, but I would prefer to make the property a private property, but If I do that, I can no longer use the above syntax.

In short my question:
Can I make a property private, so that I can only manipulate it from within the class, but still retain the ability to use Me.
Apr 5 '11 #1

✓ answered by MikeTheBike

Hi

As the Me key word refers to the Class then the only thing exposed are Public declared Variables, Subs, Functions and Properties.

You are already refereing to the Private varable in your property definition ie
Expand|Select|Wrap|Line Numbers
  1. pIntStartHeight = intValue 
So to manipulate the Private varable (declared using Dim) you would just use
Expand|Select|Wrap|Line Numbers
  1. pIntStartHeight = 500
in you classes code!

As far as I can see the only reason for using Propert Let/Get definitions; that is to define Read or Write ONLY properties. All other properies (as opposed to Methods) can be delared ar Public!! But perhaps I am mistaken, I would be interested in other views/opinoins!

Hope that makes sense!

MTB

3 4533
MikeTheBike
639 Expert 512MB
Hi

As the Me key word refers to the Class then the only thing exposed are Public declared Variables, Subs, Functions and Properties.

You are already refereing to the Private varable in your property definition ie
Expand|Select|Wrap|Line Numbers
  1. pIntStartHeight = intValue 
So to manipulate the Private varable (declared using Dim) you would just use
Expand|Select|Wrap|Line Numbers
  1. pIntStartHeight = 500
in you classes code!

As far as I can see the only reason for using Propert Let/Get definitions; that is to define Read or Write ONLY properties. All other properies (as opposed to Methods) can be delared ar Public!! But perhaps I am mistaken, I would be interested in other views/opinoins!

Hope that makes sense!

MTB
Apr 5 '11 #2
TheSmileyCoder
2,322 Expert Mod 2GB
You are correct.

I think I misunderstood the proper use of properties (no pun intended...).

As I see it now, after your post, I realise that the idea behind making properties is to expose them to the outside world, and to possibly add extra code to the event of setting/getting them.

In my simple example, I might as well simply dim my pIntStartHeight as public.
Apr 5 '11 #3
hype261
207 100+
The reason you have Public and Private is for data hiding. Variable that are Public can be seen and modified by other parts of the code outside the class. Variables that are Private cannot. Generally you do not want all your variable Public because it can become a debugging nightmare.

I have one application that I inherited where every class is public and a lot of them are global to boot. I am afraid to touch anything in the application because it is very hard to figure out where everything is being used and the ramifications of changing something.
Apr 5 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
9
by: MLH | last post by:
I need a fundamental explanation of Class Modules - something suitable for newbies. Access 2.0 didn't seem to focus on them very much. Now that I'm using Access 97, it seems they're everywhere. thx...
8
by: MLH | last post by:
Have a look at the procedure below. In particular, note lines 1 and 21... Private Sub JudHrngReqBtn_Click() On Error GoTo Err_JudHrngReqBtn_Click Dim ThisForm As String ThisForm = Me.Name ...
5
by: Earl Teigrob | last post by:
Is there a way to rename the public properties of a inherited class? I am inheriting an asp.net control (class) and am adding addtional functionality. (in this case, up to 3 borders on an...
2
by: pietaster | last post by:
I really dont know what I should do. I am asking for best practices. currently I create my properties by doing this. Context.Items.Add("Info", new Info(Request.QueryString,...
5
by: Cerebrus | last post by:
Hi all, Is there any way to get a list of all the Public properties of a Class ? Something like the GetNames() function returns a list of all the items in an Enumeration. As a simple...
14
by: Jamey Shuemaker | last post by:
Greetings all, I've been reading for the last couple hours posts on this site and various MS sites about reference libraries and class modules. System: Windows 2K running an A2K db with...
6
by: t f | last post by:
Hi I have a class with a bunch of private variables in, is there an easy way to make these have public properties without having to type it all in? e.g. public class Fu { private float...
0
by: mvanroshum | last post by:
Hi, I have the following problem: The DataSource of a DataGrid can be set to an IList. The DataGrid nicely lists the objects in the IList, showing all the public properties of the objects as...
0
by: bettatronic | last post by:
The goal : to have class which is able to add some OLE objects on the sheet runtime. Instances of the class must be accessible from any public/private module. Okay, I've experimented for so long &...
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: 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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.