Property with Public Get and Friend Set?

T

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
 
H

Herfried K. Wagner [MVP]

* "Tom said:
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.
 
A

Armin Zingler

Tom said:
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.
 
F

Fergus Cooney

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 
F

Fergus Cooney

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
 
J

Jay B. Harlow [MVP - Outlook]

Fergus,
Actually I prefer scrambled.

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

Jay
 
H

Herfried K. Wagner [MVP]

* "Fergus Cooney said:
|| 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...
 
H

Herfried K. Wagner [MVP]

* "Fergus Cooney said:
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.
 
R

Rick Mogstad

Jay B. Harlow said:
Fergus,
Actually I prefer scrambled.

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

Jay


:) isnt that a grammar checker?
 
J

Jay B. Harlow [MVP - Outlook]

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
 
H

Herfried K. Wagner [MVP]

* "Jay B. Harlow said:
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.

;-)
 
J

Jay B. Harlow [MVP - Outlook]

Rick,
I was patially thinking grammar checker, yet I was also thinking something
higher than that.

Jay
 
F

Fergus Cooney

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
 
F

Fergus Cooney

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
 
V

VirusMan

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
 
H

Herfried K. Wagner [MVP]

* "Fergus Cooney said:
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.
 
A

_Andy_

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,
 

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