Reputation: 921
I have created a read-only property(name) in class1. How can I use this name property in class2?
Public Class Class1
ReadOnly Property name() As String
Get
Return System.IO.path.GetFileName("C:\Demo\Sample1")
End Get
End Property
Can I directly carry this name variable value into class2? Need suggestion.
Upvotes: 0
Views: 1478
Reputation: 108937
Your Readonly property is still an instance members and cannot be shared without instantiating Class1 and looking at the property definition, it can be Shared
. You can make your property Shared
and use it in class2
Public Class Class1
Shared Property name() As String
Get
Return System.IO.path.GetFileName("C:\Demo\Sample1")
End Get
End Property
and in class2, you can call
Dim class1Name = Class1.name
Upvotes: 1
Reputation: 460038
You can access this property everywhere where you have a reference to a Object of type Class1
. So if your Class2
objects have a reference, they can use it.
For example:
Class Class2
Property cls1 As New Class1
Function getClass1Name() As String
Return cls1.Name
End Function
End Class
Another option is to make the property shared, because it's a value that is independent of any instance of Class1
that makes sense.
Then you can access it without an instance of Class1
via the class-name:
Class Class1
Public Shared ReadOnly Property Name As String
Get
Return System.IO.Path.GetFileName("C:\Demo\Sample1")
End Get
End Property
End Class
Class Class2
Function getClass1Name() As String
Return Class1.Name
End Function
End Class
Upvotes: 3