By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,150 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

Property with Public Get and Friend Set?

P: n/a
Tom
I have written a property, where I want to have the GET be available to
anyone (i.e. Public); however, I want the SET to be available ONLY to the
class or program itself (i.e. Friend).

In VB6 this was easy, since the Gets and Sets were seperate. How can one do
this in VB.NET?

Tom
Nov 20 '05 #1
Share this Question
Share on Google+
17 Replies


P: n/a
* "Tom" <to*@nospam.com> scripsit:
I have written a property, where I want to have the GET be available to
anyone (i.e. Public); however, I want the SET to be available ONLY to the
class or program itself (i.e. Friend).

In VB6 this was easy, since the Gets and Sets were seperate. How can one do
this in VB.NET?


You will have to write separate _procedures_, for example a 'GetFoo'
function and a 'SetFoo' sub.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #2

P: n/a
"Tom" <to*@nospam.com> schrieb
I have written a property, where I want to have the GET be available
to anyone (i.e. Public); however, I want the SET to be available ONLY
to the class or program itself (i.e. Friend).

In VB6 this was easy, since the Gets and Sets were seperate. How can
one do this in VB.NET?


Unfortunatelly, this is not possible in VB.NET. You would have to write a
Set procedure and a Get procedure to individually specify the modifier.
--
Armin

Nov 20 '05 #3

P: n/a
Hi Tom,

The only way that I know takes a lot of patience. You submit a wish to the
VB.NET development team and get all your friends, relatives, neighbours, etc
to do the same. Then you wait for it to come out in a future version.

I'd like it too, or rather Private/Protected Set and Public Get.

Regards,
Fergus
Nov 20 '05 #4

P: n/a
Tom,
What I do is create a public readonly property, then a Set Sub similar to
what Herfried & Armin stated.

This allows consumers (other assemblies) of your class to use the property
notation, while still giving you the 'hidden' method of setting the value.

Something like:

Public Class Person

Private m_name As String

Public Readonly Property Name() As String
Get
Return m_name
End Get
End Property

Friend Sub SetName(ByVal value As String)
m_name = value
End Sub

End Class

However a lot of the times I pass the value to the constructor, and it is
never changed:

Public Class Person

Private ReadOnly m_name As String

Public Sub New(ByVal name As String)
m_name = name
End Sub

Public Readonly Property Name() As String
Get
Return m_name
End Get
End Property

End Class

Where I may make the constructor Fried, as only the current assembly can
create instances of the class.

Hope this helps
Jay

"Tom" <to*@nospam.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I have written a property, where I want to have the GET be available to
anyone (i.e. Public); however, I want the SET to be available ONLY to the
class or program itself (i.e. Friend).

In VB6 this was easy, since the Gets and Sets were seperate. How can one do this in VB.NET?

Tom

Nov 20 '05 #5

P: n/a
Hi Jay,

|| Where I may make the constructor Fried, as only the
|| current assembly can create instances of the class.

I like to expose the fields directly when I use that keyword.

Class YummyBreakfast
Fried Egg As New BreakfastItem
Fried Sausage As New BreakfastItem
: : :
End Class

Now that's a class which I'd have no trouble consuming and anyone in the
Assembly is welcome to join me ;-)).

Regards,
fergus
Nov 20 '05 #6

P: n/a
Fergus,
Actually I prefer scrambled.

Damn spell checker, why can't it find correctly spelled incorrectly used
words.

Jay

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Hi Jay,

|| Where I may make the constructor Fried, as only the
|| current assembly can create instances of the class.

I like to expose the fields directly when I use that keyword.

Class YummyBreakfast
Fried Egg As New BreakfastItem
Fried Sausage As New BreakfastItem
: : :
End Class

Now that's a class which I'd have no trouble consuming and anyone in the Assembly is welcome to join me ;-)).

Regards,
fergus

Nov 20 '05 #7

P: n/a
* "Fergus Cooney" <fi******@tesco.net> scripsit:
|| Where I may make the constructor Fried, as only the
|| current assembly can create instances of the class.
Bad quoting.
I like to expose the fields directly when I use that keyword.

Class YummyBreakfast
Fried Egg As New BreakfastItem
Fried Sausage As New BreakfastItem
: : :
End Class

Now that's a class which I'd have no trouble consuming and anyone in the
Assembly is welcome to join me ;-)).


I don't see any advantages...

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #8

P: n/a
* "Fergus Cooney" <fi******@tesco.net> scripsit:
The only way that I know takes a lot of patience. You submit a wish to the
VB.NET development team and get all your friends, relatives, neighbours, etc
to do the same. Then you wait for it to come out in a future version.


I remember there were thousands of wishes when the first beta of VB.NET
was available, but they didn't hear at these wishes.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #9

P: n/a
Herfried,
I don't see any advantages... I think Fergus was giving me a hard time for Fried instead of Friend.

This may be the first time I made it Fried, most of the time I catch myself
making it Fiend.

Jay

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:OL**************@TK2MSFTNGP09.phx.gbl... * "Fergus Cooney" <fi******@tesco.net> scripsit:
|| Where I may make the constructor Fried, as only the
|| current assembly can create instances of the class.


Bad quoting.
I like to expose the fields directly when I use that keyword.

Class YummyBreakfast
Fried Egg As New BreakfastItem
Fried Sausage As New BreakfastItem
: : :
End Class

Now that's a class which I'd have no trouble consuming and anyone in the Assembly is welcome to join me ;-)).


I don't see any advantages...

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Nov 20 '05 #10

P: n/a

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:OX**************@TK2MSFTNGP09.phx.gbl...
Fergus,
Actually I prefer scrambled.

Damn spell checker, why can't it find correctly spelled incorrectly used
words.

Jay

:) isnt that a grammar checker?

Nov 20 '05 #11

P: n/a
* "Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> scripsit:
I don't see any advantages...
I think Fergus was giving me a hard time for Fried instead of Friend.


Ooops. I didn't realize that.
This may be the first time I made it Fried, most of the time I catch myself
making it Fiend.


;-)

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #12

P: n/a
Rick,
I was patially thinking grammar checker, yet I was also thinking something
higher than that.

Jay
"Rick Mogstad" <ri**@NOSPAM.computetosuit.com> wrote in message
news:uD****************@TK2MSFTNGP09.phx.gbl...

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message news:OX**************@TK2MSFTNGP09.phx.gbl...
Fergus,
Actually I prefer scrambled.

Damn spell checker, why can't it find correctly spelled incorrectly used
words.

Jay

:) isnt that a grammar checker?

Nov 20 '05 #13

P: n/a
Hi Herfried,

Maybe they were too busy with such newness. Maybe some of those wishes are
on the later version lists.

Did you submit any juicy ones yourself?

Regards,
Fergus
Nov 20 '05 #14

P: n/a
* "Fergus Cooney" <fi******@tesco.net> scripsit:
Maybe they were too busy with such newness. Maybe some of those wishes are
on the later version lists.

Did you submit any juicy ones yourself?


No. They won't change it.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #15

P: n/a
Hi Jay,

I prefer scrambled eggs too but you didn't oblige with such a mega typo.
Hmm, but not the scrambled sausages, perhaps.

Lol. When you find that spellchecker you'll be an old, old man. :-(

Regards,
fergus
Nov 20 '05 #16

P: n/a
Hi there,
This may be the first time I made it Fried, most of the time I catch myself

making it Fiend.

That's what I use when I write **my** applications.

VirusMan
Nov 20 '05 #17

P: n/a
On Fri, 17 Oct 2003 16:18:18 -0400, "Tom" <to*@nospam.com> wrote:
I have written a property, where I want to have the GET be available to
anyone (i.e. Public); however, I want the SET to be available ONLY to the
class or program itself (i.e. Friend).

In VB6 this was easy, since the Gets and Sets were seperate. How can one do
this in VB.NET?

Tom


If you are determined to use Property as opposed to individual
accessors (which is far easier), you can go along the lines of the
following:

--begin example--

Public MustInherit Class TestBase
Protected mValue As String

Friend Property Value() As String
Get
Return mValue
End Get
Set(ByVal Value As String)
mValue = Value
End Set
End Property

End Class

Public Class Test
Inherits TestBase

Public Shadows ReadOnly Property Value() As String
Get
Return mValue
End Get
End Property

End Class

--end example--

To set the value from within the same project, you can use:

Dim oTest As Test
...
CType(oTest,TestBase).Value = "Testing"

This can be neatened up by using a separated interface pattern. ITest
is implemented (and therefore exposed), and there is no need to expose
"TestBase" at all.

But I do agree with the other chaps: The following is by far the
easiest:

--begin example--
Public Class Test
Private mValue As String

Public ReadOnly Property Value() As String
Get
Return mValue
End Get
End Property

Friend Sub SetValue(Value As String)
mValue = Value
End Sub

End Class
--end example--

Rgds,

Nov 20 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.