I have the following code. Whem I am trying to call the method of the parent class from child class I get an error called "declaration expected".
Public Class ParentClass
Public Function Foo() As Integer
Return 1
End Function
End Class
Public Class ChildClass
Inherits ParentClass
Foo()
End Class
Related
This is the situation:
Class A
Implements ICloneable
Public Property Children As List(Of Child)
Public Function Clone() As Object Implements ICloneable.Clone
Return New A With {
.Children = Children.Select(Function(c) DirectCast(c.Clone(), Child)).ToList()
}
End Function
End Class
Class Child
Implements ICloneable
Public Property Parent As A
Public Function Clone() As Object Implements ICloneable.Clone
Return New Child With {
.Parent = DirectCast(Parent.Clone(), A)
}
End Function
End Class
The actual object is more complex, having several levels.
I'm not sure how to solve this because, at the moment, whenever you call Clone on the parent A class, you will end up with a circular reference.
How can I avoid this situation? Should I create my own Clone function and pass along a parameter?
The simplest solution is to just have the Child class not clone the Parent property at all. When a Child clones itself, it could either leave the Parent property the same, or just leave it null. For instance:
Class Child
Implements ICloneable
Public Property Parent as A
Public Function Clone() As Object Implements ICloneable.Clone
Return New Child() With { .Parent = Me.Parent }
End Function
End Class
Then, when the parent A class clones itself, it could set the Parent property of all the cloned children, like this:
Class A
Implements ICloneable
Public Property Children As List(Of Child)
Public Function Clone() As Object Implements ICloneable.Clone
Return New A() With
{
.Children = Me.Children.Select(
Function(c)
Dim result As Child = DirectCast(c.Clone(), Child))
result.Parent = Me
Return result
End Function).ToList()
}
End Function
End Class
Alternatively, as you suggested, you could make your own Clone method which takes the parent object as a parameter:
Class Child
Public Property Parent as A
Public Function Clone(parent As A) As Object
Return New Child() With { .Parent = parent }
End Function
End Class
It won't implement ICloneable, but as long as you don't need it to be interchangeable with other types of ICloneable objects, then that won't matter. Similarly, you could just overload your constructor:
Class Child
Public Property Parent as A
Public Sub New()
End Sub
Public Sub New(childToClone As Child, parent As A)
' Copy other properties from given child to clone
Me.Parent = parent
End Sub
End Class
I have two classes, one inheriting from the other. The first shows a summary view of data (4+ columns) and the child shows a detail view (40+ columns). Both classes are accessing the same table and share the same columns being accessed.
Can my child class inherit from the parent class so I only have to change mappings in one place? I'd rather not have duplicate code running rampant.
E.g.:
Public Class Parent
Public Overridable Property MyProp As String
Public Overridable Property MyProp2 As String
End Class
Public Class Child : Inherits Parent
Public Overridable Property MyProp3 As String
End Class
I want to do something like this:
Public Class ParentMapping
Inherits ClassMapping(Of Parent)
Public Sub New()
Me.Property(Function(x) x.MyProp, Sub(y) y.column("MyProp"))
Me.Property(Function(x) x.MyProp2, Sub(y) y.column("MyProp2"))
End Sub
End Class
Public Class ChildMapping
Inherits SubClassMapping(of Child)
Public Sub New()
' I want to be able to inherit the mappings of the parent here.
MyBase.New()
Me.Property(Function(x) x.MyProp3, Sub(y) y.column("MyProp3"))
End Sub
End Class
If you want the Child to be a subclass of parent in db also, you'll need a discriminator column.
If you just want to reuse code then share a mapping base class
public abstract class ParentChildMapping<T> : ClassMapping<T> where T : Parent
{
public ParentChildMapping()
{
// map shared properties here
}
}
public class ParentMapping : ParentChildMapping<Parent>
{
}
public class ChildMapping : ParentChildMapping<Child>
{
public ChildMapping()
{
// map additional properties here
}
}
I have the following interfaces:
Interface IViewModel
...
End Interface
Interface ISpecialViewModel
Inherits IViewModel
...
End Interface
Interface IView
WriteOnly Property MyViewModel As IViewModel
End Interface
Following are my classes:
Class VerySpecialViewModel
implements ISpecialViewModel
...
End Class
Class View
Implements IView
Public WriteOnly Property MyViewModel As VerySpecialViewModel Implements IView.MyViewModel
...
End Property
End Class
It tells me that 'MyViewModel' cannot implement 'MyViewModel' because there is no matching property on interface 'IView'.
Public Interface ISomething
WriteOnly Property Prop As IParent
End Interface
That interface declaration isn't satisfied by your class implementation. Consider following situation:
There is another interface called IChild2:
Public Interface IChild2
Inherits IParent
...
End Interface
According to ISomething interface you should be able to assign instance of class implementing IChild2 into Thing.Prop, because it inherits IParent.But you can't, because Thing.Prop property is of IChild type and IChild2 does not inherits IChild
Update
What about that solution:
Class ThingBase
Implements ISomething
Public WriteOnly Property Prop As IParent Implements ISomething.Prop
Set(value As IParent)
End Set
End Property
End Class
Class Thing
Inherits ThingBase
Public Overloads WriteOnly Property Prop As IChild
Set(value As IChild)
MyBase.Prop = value
End Set
End Property
End Class
Update2
Interface IView(Of T As IViewModel)
WriteOnly Property MyViewModel As T
End Interface
Class VerySpecialViewModel
Implements ISpecialViewModel
End Class
Class View
Implements IView(Of ISpecialViewModel)
Public WriteOnly Property MyViewModel As ISpecialViewModel Implements IView(Of ISpecialViewModel).MyViewModel
Set(value As ISpecialViewModel)
End Set
End Property
End Class
or
Class View
Implements IView(Of VerySpecialViewModel)
Public WriteOnly Property MyViewModel As VerySpecialViewModel Implements IView(Of VerySpecialViewModel).MyViewModel
Set(value As VerySpecialViewModel)
End Set
End Property
End Class
I have two libraries in a 3-tier project - call them BO and DAL. Almost every class in DAL implements a Save Method:
Public Function Save(ByVal someObject As BO.SomeType) As Boolean
As I have to add the missing Save methods, I though it was a good idea to create an interface which implements a 'MustInherit Save' function. So if I type:
MustOverride Function Save(ByVal someObject As BO.SomeType) As Boolean
That should do the trick; but the type of the parameter is always different. For example, in class DAL.TypeA:
Public Function Save(ByVal someObject As BO.SomeTypeA) As Boolean
And in class DAL.TypeB:
Public Function Save(ByVal someObject As BO.SomeTypeB) As Boolean
Is there a way to handle different type of parameters in an interface? Some kind of generic? Is this even possible?
Perfect use case for generics.
Using a generic interface:
Interface:
Interface ISave(Of T)
Function Save(someObject As T) As Boolean
End Interface
Implementation:
Class BoClass
Implements ISave(Of BoClass)
Public Function Save(someObject As BoClass) As Boolean Implements ISave(Of BoClass).Save
End Function
End Class
Class DalClass
Implements ISave(Of DalClass)
Public Function Save(someObject As DalClass) As Boolean Implements ISave(Of DalClass).Save
End Function
End Class
Or, using a generic base class instead of an interface
Base class
MustInherit Class BaseClass(Of T)
MustOverride Function Save(someObject As T) As Boolean
End Class
Implementation
Class BoClass
Inherits BaseClass(Of BoClass)
Public Overrides Function Save(someObject As BoClass) As Boolean
End Function
End Class
Class DalClass
Inherits BaseClass(Of DalClass)
Public Overrides Function Save(someObject As DalClass) As Boolean
End Function
End Class
This looks like an implementation detail, in which case, the objects you are saving should implement an interface that your DAL class would call.
Public Interface ISaveable
Function Save() As Boolean
End Interface
Then the objects that do the actual saving implement it:
Public Class BOItem
Implements ISaveable
Public Function Save() As Boolean Implements ISaveable.Save
'do the actual saving here
End Function
End Class
Your base class would then just ask for the interface:
Public MustInherit Class DAL
MustOverride Function Save(someObject As ISaveable) As Boolean
End Class
And then when you create DAL objects, you won't have to worry about what type of object it is, you just call save:
Public Class DALItem
Inherits DAL
Public Overrides Function Save(someObject As ISaveable) As Boolean
Return someObject.Save()
End Function
End Class
I'm getting this error when trying to implement an interface in vb.net:
Public Interface IFoo
ReadOnly Property Foo() As String
End Interface
Public Class myFoo
Implements IFoo
Public ReadOnly Property Foo() As String
Get
return "Foo"
End Get
End Property
...
End Class
What is missing?
You will want to tell the code that the myFoo.Foo implements the IFoo.Foo (notice the added Implements IFoo.Foo):
Public Interface IFoo
ReadOnly Property Foo() As String
End Interface
Public Class myFoo
Implements IFoo
Public ReadOnly Property Foo() As String Implements IFoo.Foo
Get
Return "Foo"
End Get
End Property
End Class
As far as I know, VB.NET does not support implicit interface implementations in the same way as C# does.