Question:
How do you define a read only property in a class module?
Answer:
Declare Only Get function of the property. Source: CoolInterview.com
Answered by: Roopa | Date: 1/23/2008
| Contact Roopa
Public Class YourClass Private yourName As String Private yourNumber As Decimal
Public Sub New(breed As String) yourName = breed End Sub
Public ReadOnly Property Name() As String Get Return yourName End Get End Property
Source: CoolInterview.com
Answered by: RajeeVenkat | Date: 1/28/2008
| Contact RajeeVenkat
we can create a read only property by using the keyword "Read only" before the syntax . e.g Read only property Property_name([args]) as type get [statement] end get end property Source: CoolInterview.com
Answered by: Ateev | Date: 1/28/2008
| Contact Ateev
Don't use Set block only use Get block in the property. Source: CoolInterview.com
Answered by: Sachin Bhalerao | Date: 1/29/2008
| Contact Sachin Bhalerao
Read-only is a variable. It can't change by user.It can change itself EX- class a { datetime dt=new datetime(); dt.now(); } Source: CoolInterview.com
Answered by: praveen Kumar | Date: 2/7/2008
| Contact praveen Kumar
To Define a read only property in a class module you have to specify the "ReadOnly" Access Identifier in the Property Definition.
The example below will explain it better.
Public Class clsConnection
Public ReadOnly Property strConnString() As String Get strConnString = "Some Connection String here" Return strConnString = ""
End Get End Property End Class Source: CoolInterview.com
Answered by: Ravindra Gautam | Date: 3/6/2008
| Contact Ravindra Gautam
Just enter the object name which is to be read only and after placing period select read only option from context menu and make it true
Textbox tb = new Textbox() tb.readonly = true Source: CoolInterview.com
Answered by: D.R.DILIP | Date: 3/14/2008
| Contact D.R.DILIP
Option Strict On
Public Class YourClass Private yourName As String Private yourNumber As Decimal
Public Sub New(breed As String) yourName = breed End Sub
Public ReadOnly Property Name() As String Get Return yourName End Get End Property
Public Property Number() As Decimal Get Return yourNumber End Get
Set yourNumber = CDec(value) End Set End Property
Public Sub ShowInfo() Console.WriteLine("This " & yourName & " weighs " & yourNumber & " pounds.") End Sub End Class
Public Class Tester Public Shared Sub Main() Dim mal As New YourClass("A")
mal.Number = 130 ChangeYourClassInfo(mal) mal.ShowInfo CompletelyChangeYourClassInfo(mal) mal.ShowInfo End Sub
Public Shared Sub ChangeYourClassInfo(ByVal aYourClass As YourClass) aYourClass.Number = 125 End Sub
Public Shared Sub CompletelyChangeYourClassInfo(ByVal aYourClass As YourClass) Dim newf As New YourClass("Newfoundland") aYourClass = newf End Sub End Class
Source: CoolInterview.com
Answered by: SORNALAKSHMI S.K. | Date: 3/16/2008
| Contact SORNALAKSHMI S.K.
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|