I figured I would ask... but is there a way to have the Get part of a property available as public, but keep the set as private?
Otherwise I am thinking I need two properties or a property and a method, just figured this would be cleaner.
Yes, quite straight forward:
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Private Set(ByVal value As String)
_name = value
End Set
End Property
I'm not sure what the minimum required version of Visual Studio is, but in VS2015 you can use
Public ReadOnly Property Name As String
It is read-only for public access but can be privately modified using _Name
Public Property Name() As String
Get
Return _name
End Get
Private Set(ByVal value As String)
_name = value
End Set
End Property
One additional tweak worth mentioning: I'm not sure if this is a .NET 4.0 or Visual Studio 2010 feature, but if you're using both you don't need to declare the value parameter for the setter/mutator block of code:
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Private Set
_name = value
End Set
End Property
I find marking the property as readonly cleaner than the above answers. I believe vb14 is required.
Private _Name As String
Public ReadOnly Property Name() As String
Get
Return _Name
End Get
End Property
This can be condensed to
Public ReadOnly Property Name As String
https://msdn.microsoft.com/en-us/library/dd293589.aspx?f=255&MSPPError=-2147217396
If you are using VS2010 or later it is even easier than that
Public Property Name as String
You get the private properties and Get/Set completely for free!
see this blog post: Scott Gu's Blog
Related
So far I had a read-only property in VB.NET as
Public ReadOnly Property Username() As String
Now I need to make changes to my code as the setter to this property needs to be protected. In C# I would have done:
public string Username {get; protected set;}
But I can't find such a short hand and good-looking solution in VB.NET. Does VB.NET provide that and I am unaware of this? Or do I have to write so much code and make it look little uglier?
Public Property Username() As String
Get
Return m_Username
End Get
Protected Set
m_Username = Value
End Set
End Property
Private m_Username As String
It's going to make my class so dirty if I had 20 properties and set them like this.
Unfortunately, there is no such shorthand syntax in VB. Also see: https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/auto-implemented-properties
You could make your code a bit shorter by doing it like this:
Protected _username As String
Public ReadOnly Property Username As String
Get
Return _username
End Get
End Property
That way you can set the username through _username in classes that inherit this class.
Can anyone explain me when should i use Property, Private or both? When i create class what is good practice choose between them. I always see in many examples people used to do it diffrent way.
Some of them doing like this:
Private _name as string
Private _age as Integer
Other people does:
Property Name as string
Property Age as Integer
and other people:
Private _name As String
Private _age As Integer
Public Property name() As String
Get
Return _name
End Get
Set
_name = value
End Set
End Property
Public Property age() As Integer
Get
Return _age
End Get
Set
_age = value
End Set
End Property
in this case what is a sense to use private as Property already hiding private variable so in this case we could call it: _name and _age without specifying it as here.
You use fields (variables) when you want to only store value in memory. You use properties when you want to do more, for example validating it. Property is basically two methods for getting value (Get) and setting value (Set).
I'm trying to create an inheritable class(OF t) in vb.net that I will pass it a class of objects. Inside the class of objects I want to use the class properties to create a corresponding database table. Like below
Public Class SampleClass
#Region "Properties"
Private newPropertyValue As String
Public Property NewProperty() As String
Get
Return newPropertyValue
End Get
Set(ByVal value As String)
newPropertyValue = value
End Set
End Property
#End Region
Public Sub New()
End Sub
End Class
I'm new to vb.net so I don't know my way around exactly.
I was looking into class attributes for this action but they do not fully make sense to me yet. Thanks in advance.
You will want to get well versed on something called Code First. This should get you started.
Having a bit of a brain fart, right before bed. But i have the need to remap a ReadOnly Property from one name to a specified name i want.
I figured i could do
Public Readonly Property DocName as String
Get
Return Mybase.Name
End Get
End Property
And yes i am trying to remap the Name Property for an XMLDocument object. Just want to make sure that as long as i declare this property and then type:
Public Overrides ReadOnly Property Name As String
Get
Return SomeValue
End Get
End Property
I will be good togo? I know i will get the method has multiple definitions with identical signatures message, which brings me to my 2nd Question:
How do i prevent the Multiple Signatures error message from popping up with this type of declaration?
Unless i am missing some declaration attribute for this type of override.
You can use Shadows to accomplish this:
Public Class A
Public ReadOnly Property Name As String
Get
Return "Name"
End Get
End Property
End Class
Public Class B
Inherits A
Public ReadOnly Property DocName As String
Get
Return MyBase.Name
End Get
End Property
Public Shadows ReadOnly Property Name As String
Get
Return "SomeValue"
End Get
End Property
End Class
How to pass a object that is initialized from a derived class to a method that has Interface as the parameter? Below is the example of what I'm trying. Is it possible? Please suggest any better way of doing.
Public Interface IFruit
Property Name As String
Property Color As String
End Interface
Public Class Fruit
Implements IFruit
Private _Name As String
Private _Color As String
Public Property Color As String Implements IFruit.Color
Get
Return _Color
End Get
Set(value As String)
_Color = value
End Set
End Property
Public Property Name As String Implements IFruit.Name
Get
Return _Name
End Get
Set(value As String)
_Name = value
End Set
End Property
End Class
Public Class FruitExtended
Inherits Fruit
Private _Taste As String
Public Property Taste() As String
Get
Return _Taste
End Get
Set(ByVal value As String)
_Taste = value
End Set
End Property
End Class
Public Class A
Public Sub ProcessFruit(F as IFruit)
'...
'Do something
End Sub
will the below code work? or how to achieve this in other ways?
Public Sub Test()
Dim F1 as new FruitExtended()
ProcessFruit(F1)
End sub
End Class
I didn't try your code, but - in general - interfaces are good because they define a behaviuor rather than a state (properties). So maybe you could rethink your design and ask yourself what the ProcessFruit is supposed to do with a IFruit.
Some languages even disallow to declare properties in interfaces, other than constants. Java is an example.