473,770 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing Private Properties

How do I access a private property of a class from outside the class. I have
a class that has a property which I want to access in my code for read/write
but allow end users to only read.
--
Dennis in Houston
Nov 21 '05 #1
5 1430
This is one annoying limitation in VB.NET - the Getter and Setter must have
the same access modifier. This wasn't the case in VB6. However, this has
been resolved in VB2005 - you can have different access modifiers for the
Getter and Setter of the property. For the time being, you'll have to resort
to something like this:

Private Field As Integer

Public ReadOnly Property ReadOnlyProp() As Integer
Get
Return Field
End Get
End Property

Private Sub SetReadOnlyProp (ByVal i As Integer)
Field = i
End Sub

You can change the access modifier of the SetReadOnlyProp method to Friend
depending on whether you want to be able to write from within your class
only or from other classes within the project as well.
hope that helps..
Imran.
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:E3******** *************** ***********@mic rosoft.com...
How do I access a private property of a class from outside the class. I
have
a class that has a property which I want to access in my code for
read/write
but allow end users to only read.
--
Dennis in Houston

Nov 21 '05 #2
Dennis,

This is confusing,

I have a class that has a property which I want to access in my code for
read/write
but allow end users to only read.

Where is the code which need to access the property and who are the
endusers?

Cor
Nov 21 '05 #3
Thanks for answers. I resorted to using two properties, one public readonly
and the other Friend for readWrite. I thought I had seen an example on this
news group using reflection to write to private proerties. Thanks again.

"Dennis" wrote:
How do I access a private property of a class from outside the class. I have
a class that has a property which I want to access in my code for read/write
but allow end users to only read.
--
Dennis in Houston

Nov 21 '05 #4
Dennis,

Yes - you can do it via Reflection as well although it would be a little
slower. Here's how:

Public Class Testclass
Private Field As Integer = 1

Private Property TestProp() As Integer
Get
Return Field
End Get
Set(ByVal Value As Integer)
Field = Value
End Set
End Property
End Class

Private Sub TestMethod()
Dim o As New Testclass
o.GetType.GetPr operty("TestPro p", _
Reflection.Bind ingFlags.Instan ce Or _
Reflection.Bind ingFlags.NonPub lic).SetValue(o , 3, Nothing)
End Sub

However, what you've come up with is also a decent solution to this problem.
You can choose either of these methods.

hope that helps..
Imran.

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:65******** *************** ***********@mic rosoft.com...
Thanks for answers. I resorted to using two properties, one public
readonly
and the other Friend for readWrite. I thought I had seen an example on
this
news group using reflection to write to private proerties. Thanks again.

"Dennis" wrote:
How do I access a private property of a class from outside the class. I
have
a class that has a property which I want to access in my code for
read/write
but allow end users to only read.
--
Dennis in Houston

Nov 21 '05 #5
Thanks Imran, that's what I was looking for.

"Imran Koradia" wrote:
Dennis,

Yes - you can do it via Reflection as well although it would be a little
slower. Here's how:

Public Class Testclass
Private Field As Integer = 1

Private Property TestProp() As Integer
Get
Return Field
End Get
Set(ByVal Value As Integer)
Field = Value
End Set
End Property
End Class

Private Sub TestMethod()
Dim o As New Testclass
o.GetType.GetPr operty("TestPro p", _
Reflection.Bind ingFlags.Instan ce Or _
Reflection.Bind ingFlags.NonPub lic).SetValue(o , 3, Nothing)
End Sub

However, what you've come up with is also a decent solution to this problem.
You can choose either of these methods.

hope that helps..
Imran.

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:65******** *************** ***********@mic rosoft.com...
Thanks for answers. I resorted to using two properties, one public
readonly
and the other Friend for readWrite. I thought I had seen an example on
this
news group using reflection to write to private proerties. Thanks again.

"Dennis" wrote:
How do I access a private property of a class from outside the class. I
have
a class that has a property which I want to access in my code for
read/write
but allow end users to only read.
--
Dennis in Houston


Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3948
by: Toufani | last post by:
Hi everybody, I want to retrieve information about objects in active directory windows 2000 and their properties. I got some codes that don't work absolutely. for example I can't retrieve users list and group list separatedly.there is my code that downloaded from the internet : public class LdapAuthentication { private string _path;
6
1229
by: DKode | last post by:
Ok, kind of a silly question I just want to know what the proper method is, When I have local Properties, ie: private bool _someVal; public bool SomeVal { get { return _someVal; } }
3
1644
by: Craig G | last post by:
i have a user control which is basically a datagrid, which has add/edit/delete buttons on the grid is there anyway of accessing the actual datagrid from the form itself? basically i want to set the add/edit/delete options visble/invisible on the datagrid depending on the users's role, but found that i cannot access the grid to do this. is the only way of setting it in the page load of the usercontrol itself?
2
1713
by: Vivek Sharma | last post by:
Hi There, I have a situation where I wish to load the controls dynamically on the basis of user role. Hence, I am using this code. if (UserRole == "IS Administrator") { Control UC1 = LoadControl("../UserControls/ISJob/uctlJobGeneral.ascx");
8
2745
by: dwok | last post by:
I have been wondering this for a while now. Suppose I have a class that contains some private member variables. How should I access the variables throughout the class? Should I use properties that expose the variables or is it OK to just access the variables directly? Keep in mind that I am talking about accessing the variables from within the class that they are defined. Thanks!
4
3640
by: raj_genius | last post by:
I hav two queries, whc are as follows: FIRSTLY: is it possible to access the controls(by name) of a parent form(MDI) from its child forms??if yes then how??plzz provide a coded example in VB if possible.. for example..i hav a menu in the parent form named "Administrator" whic has an item "mnuLogIn"..now when i click on login..another child form named "frmLogIn" is displayed..what i want to happen is this: when login form(frmLogIn) is...
4
1750
by: Shak | last post by:
Hi My code looks like the follwing: namespace A.B.C { .... private void foo() { A.B.C.Properties.Settings.Default.Save();
8
6560
by: RSH | last post by:
Hi, I have a situation where I have a Parent Object (Company) which has several public properties. The Company Object also creates an Employees object which has its ow set of functions and properties. I am trying to figure out how to access the Parent properties from the child object. Simplified example: Class Company Public ReadOnly Property Employees() As Employees
3
2045
by: Beorne | last post by:
In the classes I develop my attributes are always private and are exposed using properties. directly or to access the attributes using the properties? Does "wrapper" setter/getter properties introduce time overhead in execution? Thanks, Matteo
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10259
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.