is it possible (and how) to make a readonly automatic property in VB 2010?
Public Class Foo
Public Property Value As Integer
Public Sub New()
_Value = 123
End Sub
End Class
problem is that users can write to the property.
thanx
It is now supported in VB14 (Visual Studio 2015 and later):
Public Class Foo
Public ReadOnly Property Value As Integer = 123
End Class
See https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-VB-14#read-only-auto-properties
In earlier versions, you need to create a backing field.
No, VB.Net does not support readonly auto properties. See this MS Connect issue for the reasoning behind this (specifically the comment made by Jonathan Aneja).
No, it isn't possible. You will have to create an explicit backing field.
thinkthing,
you may create a code snippet to add a generic property.
http://msdn.microsoft.com/en-us/library/ms165392(v=vs.90).aspx
Be mindful that visual studio has changed the basic way we build properties, with the get set, and now only one line is required with the get set understood. The full getter and setter can be built if you do need logic built within. I refer you here:
http://msdn.microsoft.com/en-us/library/dd293589.aspx
and here, to a SO discussion regarding a similar discussion:
Using snippets to make Class properties in VB.net. prop only gives "property () as " Whats up?
Related
I'm having trouble inheriting a (public) variable, let's say
Public Var As ClassThatIsIndependent
The declaration above generates no trouble for itself, however, if i inherit the class that holds it
Implements BaseClass
I get the error "object module needs to implement variable for interface". I've tried these options (both inside ChildClass)
Public Var As ClassThatIsIndependent
and
Public BaseClass_Var As ClassThatIsIndependent
But none of them solves the problem. Any alternative? I'm open to possible Set/Get solutions, however, i'd prefer to maintain Var as a public variable.
Per the Visual Basic 6.0 Programmer's Guide, Polymorphism, Implementing Properties section:
Suppose we give the Animal class an Age property, by adding a Public variable to the Declarations section:
Option Explicit
Public Age As Double
The Procedure drop downs in the code modules for the Tyrannosaur and Flea classes now contain property procedures for implementing the Age property,
…
Using a public variable to implement a property is strictly a convenience for the programmer. Behind the scenes, Visual Basic implements the property as a pair of property procedures.
You must implement both procedures. The property procedures are easily implemented by storing the value in a private data member, as shown here:
Private mdblAge As Double
Private Property Get Animal_Age() As Double
Animal_Age = mdblAge
End Property
Private Property Let Animal_Age(ByVal RHS As Double)
mdblAge = RHS
End Property
The private data member is an implementation detail, so you have to add it yourself.
That is, the "public interface" is exactly the same whether you use a Public variable or define them with Property Get/Let. And to implement a property in an interface, you can't use the Public variable approach and need to use the Property Get/Let syntax and handle the data storage for it in your own private variables.
Ok, so this kind of follows after a previous question that I've asked involving structures and classes. So referencing this question (and I am using classes now for the base) I have one member of the class that is an array (and I know that I have to declare it without dimensions) that as part of the constructor I want it to define the dimensions of the array. When I was initially trying to do the ReDim the compiler was unhappy because I was declaring the member as ReadOnly. While what I'm doing with the array has it's own question of feasibility to it that's not what I'm asking about as it raised a different issue that I must answer first.
Is there a way to make members of a class/structure read only outside of the class/structure but modifiable with in the class/structure without having to use properties or internal functions/subs to gain the read access?
Basically like declaring the member private but you can at least read the member outside the class/structure. Just not anything else.
You can do something like this
Private _some As String
Public Property Some As String
Get
Return _some
End Get
Private Set(value As String)
_some = value
End Set
End Property
No. On its own, there is no way to make a class field public for reading, but private for writing. Accessibility modifiers on a field affect both read and write.
The cleanest way to do what you want is to define a private field in your class, and define a public property getter:
Private _dummy As String
Public Property Dummy() As String
Get
Return _dummy
End Get
End Property
Granted, it would be nice to be able to express this more succinctly, as is possible with C# using auto-implemented properties:
public string Dummy {get; private set;}
We are attempting to create a FogBugz plugin & have started naturally with the Hello World example [Wiki 38].
We are using Visual Studio 2005 and VB.Net.
However, whenever we add "Implements IPluginExtraMenus" to our class AND implement the appropriate function, Visual Studio reports that
Class 'xxxx' must implement Function ExtrasMenuLinks() as UI.CNavMenuLink()
for interface FogCreek.FogBugz.Plugins.InterfacesIPluginExtrasMenu
Here is an example:
Public Class DaysRemaining
Inherits Plugin
Inherits IPluginPagedisplay
Inherits IPluginExtrasMenu
Public Function ExtrasMenuLinks() As UI.CNavMenuLink
dim vMenu as CNavMenuLink
vMenu = new CNavMenuLink("", "", "", "")
Return vMenu
End Function
End Class
Also, if we try and add an "Implements IPluginExtrasMenu.ExtrasMenuLinks" keyword in the function definition, Visual Studio reports that
'ExtrasMenuLinks' cannot implement 'ExtrasMenuLinks' because there is
no matching function on interface
FogCreek.FogBugz.Plugins.InterfacesIPluginExtrasMenu
We are importing all of the correct namespaces etc.
Any assistance will be greatly appreciated e.g. pointing where we have gone wrong, pointing us in the direction of other VB.Net examples, etc.
It looks like Visual Studio is complaining because interface expects ExtrasMenuLinks to return an array of UI.CNavMenuLink objects, whereas your implementation only returns a single UI.CNavMenuLink.
I believe the modification you need to make to match the interface is:
Public Function ExtrasMenuLinks() As UI.CNavMenuLink()
You'll also need to modify the function body to return an array.
I have been charged with porting a VB6 project into VB.NET. In vb6, if you were in a class separate to a particular variable, you could access that variable easily:
Public Class Foo
Public k As Integer
End Class
Public Class Bar
k = 12
End Class
In VB.NET, my understanding is that before you can use a variable in another class, you must declare a new instance of it:
Dim foobar As New Foo
This would be fine, but I have to access these variables from different classes and every time I declare a new instance, it wipes all old values from the variables, which I need. Can anybody help? I tried using Inherits statements but they presented many problems.
Thanks.
Nick
Your're looking for the shared keyword. This makes the member available to other classes without having to have an instance of your class. See MSDN for more info
For the port just use Public module like you would in vb6
Public Module Foo
Public k As Integer
End Module
Public Module Bar
Foo.k = 12
End Module
Its not good practice but it will help you do your first pass at the port. Ideally you would refactor out modules/shared functions as being able to access variable from any part in the system will produce code that is harder to maintain
Dim YourobjName As YourClassName = Me.DataContext
Now you can use public methods and functions with YourobjName. Here YourClassName will be the class you want to access the public objects.
Here is a VB.NET code snippet
Public Class OOPDemo
Private _strtString as String
Public Function Func(obj as OOPDemo) as boolean
obj._strString = "I can set value to private member using a object"
End Function
End Class
I thought we cannot access the private members using the object, but perhaps CLR allows us to do that. So that means that access modifiers are based on the type and not on the instance of that type. I have also heard that c++ also allows that..
Any guesses what could be the reason for this?
Edit:
I think this line from the msdn link given by RoBorg explains this behaviour
"Code in the type that declares a private element, including code within contained types, can access the element "
Your question is quite confusing but I think I've understood it as:
"Why can I access another instance (of my class)'s private variables?"
And you're right - in all OOP languages I've used you can access private variables from other instances, precisely because access permissions are based on where the code is, rather than to which object instance it 'belongs'.
It might be hard to implement copy constructors or equality operators otherwise.
Here's the section about access levels in MSDN.