How to get abstract dependent properties to work - oop

I would like to define an interface with properties in an abstract class like this
classdef A
properties (Abstract = true)
Valid;
end
end
with an implementation of this interface like this
classdef B < A
properties (Dependent = true)
Valid;
end
methods
function v = get.Valid(obj)
v = 1;
end
end
end
but when I try to make an instance of B I get the following error
>> c = B()
??? Error using ==> B
The property 'Valid' restriction defined in class 'B' must match the property definition in base class 'B'.
Can anyone tell me what I doing wrong?

Try setting the Dependent property attribute in the base class as well:
classdef A
properties (Abstract = true, Dependent = true)
Valid;
end
end
According to the documentation:
Concrete subclasses must redefine abstract properties without the
Abstract attribute set to true
The way I understood this, subclass property attributes must match the base class (without the Abstract property)

Related

How to let .NET know that I have an instance of the right class?

Let's suppose I have a
Class ViewerManager(Of ViewTable)
and inside that I have a
Protected ReadOnly Property StoredView As ViewTable
which has a Get inside which I have something like this
#Region "View Object Properties"
Enum ViewerManagerTemplate
Unkown = 1
TblMemorizedFilterPage = 2
TblMemorizedAEV = 3
End Enum
Protected _ManagerTemplate As ViewerManagerTemplate = ViewerManagerTemplate.Unkown
Protected ReadOnly Property ManagerTemplate As ViewerManagerTemplate
Get
If _ManagerTemplate = ViewerManagerTemplate.Unkown Then
If PageID > 0 Then
_ManagerTemplate = ViewerManagerTemplate.TblMemorizedFilterPage
Else
_ManagerTemplate = ViewerManagerTemplate.TblMemorizedAEV
End If
End If
Return _ManagerTemplate
End Get
End Property
Protected InitializedStoredView As Boolean = False
Protected _StoredView As ViewTable = Nothing
Protected ReadOnly Property StoredView As ViewTable
Get
If Not InitializedStoredView Then
InitializedStoredView = True
Select Case ManagerTemplate
Case ViewerManagerTemplate.TblMemorizedFilterPage
If PageObject.StoredViewID > 0 Then
_StoredView = CType(CType(BOs.CustomGridBO.GetMemorizedFilterPage(PageObject.StoredViewID), Object), ViewTable)
End If
End Select
End If
Return _StoredView
End Get
End Property
#End Region
Here, at the line of
_StoredView = CType(CType(BOs.CustomGridBO.GetMemorizedFilterPage(PageObject.StoredViewID), Object), ViewTable)
I know for sure that the BOs.CustomGridBO.GetMemorizedFilterPage will return a TblMemorizedFilterPage instance and I know for sure that if this line is executed, then ViewTable is TblMemorizedFilterPage as well, therefore the double CType seems to be an overkill for me. Can I simplify this somehow?
EDIT:
Since there was a confusion in the comment section, I think I need to give further information. TblMemorizedFilterPage and TblMemorizedAEV are both classes and the enum values having the same name are named after these classes.
You have no choice, when ViewTable will be a TblMemorizedAEV. Then that line will look like.
_StoredView = CType(CType(BOs.CustomGridBO.GetMemorizedFilterPage(PageObject.StoredViewID), Object), TblMemorizedAEV)
Since you can't cast a TblMemorizedFilterPage to a TblMemorizedAEV. Your only option is to trick the compiler by casting it to an Object first. Which is the only base class in common with each other.
If you have a lot of If statement, I would think of having two classes with Class ViewerManager(Of ViewTable) as the base class. The sub class would handle those specific situation.
I also find it strange that the template Type is dependent on the enum but the enum isn't based on the template Type. What is you do ViewerManager(Of TblMemorizedAEV) and the PageID is greater than 0.

How do I access a superclass's constant property within Matlab?

I have a simple class structure like so:
classdef super < hgsetget
properties(Constant = true, Access = private)
PROP1 = 1;
PROP2 = {2 [3 4] [5 6]};
end
methods
function self = super()
// Constructor code here
// ...
end
end
end
which is then inherited by a subclass like so.
classdef sub < super
properties
PROP3 = 7;
end
methods
function self = sub()
// Subclass constructor here
// ...
self = self#super();
test = self.PROP1; // I don't appear to have access to PROP1 from Super
end
end
end
My issue is when I try to access to the super's property PROP1 or PROP2 I don't seem to get access:
No appropriate method, property, or field PROP1 for class sub.
Is there a way to access to the super's property within Matlab?
In superclass super set properties attributes to
properties(Constant = true, Access = protected)
From the documentation an access attribute determines what code can access these properties:
public — Unrestricted access
protected — Access from methods in class or subclasses
private — Access by class methods only (not from subclasses)
You define the properties as private, those are not inherited.
Use Access = protected instead.

MATLAB OOP, can I call calling class methods?

For example:
I have two classes like this:
Look for a question inside class B comment, down.
classdef A < handle
properties
classBobj; % class B is a property of class A
end
methods
MethodFromA (obj)
end
end
end
classdef B <handle
methods
MethodFromB (obj)
% I is possible to call class A method MethodFromA here
end
end
end
There are two ways in which this is possible:
You try to call a static method from class A but then you have to define it as such:
classdef A < handle
properties
classBobj; % class B is a property of class A
end
methods (Static)
MethodFromA()
end
end
This can be called everywhere in your code (without a reference to an instance of A) as follows A.MethodFromA()
You have a reference to an instance of class A within your so instead of calling MethodFromB(obj), you should all MethodFromB(obj, classAobj)

vbscript static class variables/methods?

Is there a way to have one variable per class in vbscript?
If not what is the best way to emulate it? Prefixing a global variable declared next to the class?
Also is there a way to declare static/class methods(for a static constructor) or am I force to prefix a function?
In languages that support class-level/static data or methods you can
associate/bind data or methods explicitly to the set of objects defined by the class. So you can have Customer.Count and Product.Count and a plain Count (or ##Count) in Customer code will access the right number.
use such data or method without having an instance of the class (yet).
VBScript does not support static data or methods. You have to use global data or functions/subs and do the associating in your mind (perhaps with a little help from a naming convention). Accessing these 'static'=global elements without an object is trivial, but - obviously - should be done with care.
You can embed one or more singleton objects or code references (GetRef()) in your objects to bind them closer to the class, but that will increase the size of the instances.
You can do something like this to sort of emulate a static class:
Class Defines_
Public Sub DoSomethingUseful
End Sub
End Class
Dim Defines : Set Defines = New Defines_
...
Defines.DoSomethingUseful
This can be used to give you something analogous to constructors (really, factory methods):
Class Something
Private mValue
Public Property Get Value : Value = mValue : End Property
Public Property Let Value(x) : mValue = x : End Property
End Class
Class SomethingFactory_
Public Function Create(value)
Set Create = New Something
Create.Value = value
End Function
End Class
Dim SomethingFactory : Set SomethingFactory = New SomethingFactory_
...
Dim something : Set something = SomethingFactory.Create(5)

Returning an object of a second class through the constructor of first class

I have a node class and a tree class. I have defined the node class to contain the properties needed for a node declaration and the tree class is used to form a tree structure from the nodes. While the tree structure is formed from the node, I am having a problem in returning the node object. My code structure is:
classdef Node
properties
node_center;
node_size;
end
methods
function this = Node(center,size)
this.node_center = center;
this.node_size = size;
end
end
end % end of class Node
classdef Tree < handle
methods
function n = Tree(points,objects_in_tree)
n = Node(center_of_points,size);
n = insert_child(n,center,sizez);
end
end
Now the error I am getting is:
When constructing an instance of class 'Tree', the constructor must preserve the class of the returned object.
I know the reason of why its happening but would like to know the workaround to this. Thanks.
The return value from the constructor must be the object created - there's no way around it. You can make another function that returns the other values (like the Node) that you would like to get out of it. After the Tree is constructed, call the accessor function on that object.