VB.Net Class Inherit Sub New - vb.net

I'm currently trying to convert some basics in C# class methods into VB, and I'm having some difficulties.
Say we have two classes, baseClass and newClass
Public Class baseClass
Public Sub New(ByVal obj As Object)
'... do something here
End Sub
End Class
Public Class newClass
Inherits baseClass
Public Sub New(ByVal obj as Object)
'... do something here
End Sub
end Class
However, whenever I tried to declear something like
dim nc as newClass(myObj)
I kept getting an error telling me that
First statement of this 'Sub New' must be a call to 'MyBase.New' or 'MyClass.New' because base class 'baseClass' of 'newClass' does not have an accessible 'Sub New' that can be called with no arguments.
When I was trying to do something like, if written in C#, like this
public partial class newClass : baseClass
{
public newClass(Object obj) : baseClass(obj)
{
// do something here
}
}
I understand that the debugger is telling me just add an empty public sub new() in baseClass, but that's not what I'm trying to do, I wanted it to invoke the other constructor, so hopefully someone can point me in the right direction, since I've been trying to find an easy conversion guide but I couldn't find any.
Any answer is much appreciated.

Public Class newClass
Inherits baseClass
Public Sub New(ByVal obj as Object)
MyBase.New(obj)
End Sub
end Class

Related

VB.NET WinForms implementing an interface

VB.NET
I want some WinForms to implement an interface, and be able to pass these to a procedure which can 'see' the implemented properties as well as the 'standard methods' of a Form. This is what I have so far...
Public Interface IMyInterface
Property MyProperty As String
End Interface
Public Class MyForm
Implements IMyInterface
Private _MyProperty As String
Public Property MyProperty() As String Implements IMyInterface.MyProperty
Get
Return _MyProperty
End Get
Set(ByVal value As String)
_MyProperty = value
End Set
End Property
End Class
then, elsewhere I have my method as follows...
Public Sub DoSomething(MyForm As IMyInterface)
MyForm.MyProperty = "x"
MyForm.ShowDialog()
End Sub
The obvious problem is that the compiler doesn't know what .ShowDialog is, and if I pass my form in as 'MyForm As Form' it doesn't know what 'MyProperty' is. I understand the reasons for this, but not how to solve this problem. Is a simple casting to Form the correct way to address this?
Many thanks.
You need to inherit System.Windows.Forms.Form to gain all regular form functionality and then Implement IMyInterface.
Public Class MyForm
Inherits System.Windows.Forms.Form
Implements IMyInterface
Passing MyForm As IMyInterface into the DoSomething() method is fine, however to use the regular form methods you'll need to cast it. Alternatively you could pass in the Form and then cast to IMyInterface, your choice.
Public Sub DoSomething(MyIForm As IMyInterface)
MyIForm.MyProperty = "x"
Dim MyForm As Form = TryCast(MyIForm, Form)
MyForm.ShowDialog()
Another aproach can be using your own base class
Public Class MyBaseForm
Inherits System.Windows.Forms.Form
Public Property MyProperty As String
End Class
Then all your forms can inherit that base form
Public Class MyForm
Inherits MyBaseForm
End Class
And you can use property and standard method of System.Windows.Forms.Form without casting
Public Sub DoSomething(someform As MyForm)
someform.MyProperty = "Some value"
someform.ShowDialog()
End Sub

How to use classes as properties only?

I have been trying to do this for a long time but I can't find anything anywhere. I think I am not searching it as it should...
A little example:
Class MainClass
Property ExampleProperty As New ExamplePropertyClass
Private Class ExamplePropertyClass
Sub DoSomething()
End Sub
End Class
End Class
In the previous code the ExamplePropertyClass is used as an property of the MainClass.
There is always an error that says I can't expose a private class as propery.
But how is it possible to make only the property "Visible", I mean The user who is going to use the code should only use the property and not the class, how can the class be not inherited or visible?
What is property actually syntactic sugar for setter and getter.So mostly it is default public
You declare class as private. So it will be invisible outside. Then there is conflict if it be not visible then how people will know to assign and get that object without knowing its type. So that type should be public and visible
dim m as new MainClass()
m.ExampleProperty=? ' What is ExampleProperty ?int , object. So it should not be unknown
Another way you claim that you are not going to use that property outside.This way it is ok to have private class inside.
'Explicitly make property to be used only within class
Private Property ExampleProperty As ExamplePropertyClass
You do this with interfaces:
Public Interface IDoesSomething
Sub DoSomething()
End Interface
Public Class MainClass
Public Sub New()
m_example = New InternalClass
End Sub
Private m_example As IDoesSomething
Public ReadOnly Property Example() As IDoesSomething
Get
Return m_example
End Get
End Property
Private Class InternalClass
Implements IDoesSomething
Public Sub DoSomething() Implements IDoesSomething.DoSomething
End Sub
End Class
End Class

vb.net extending a class with generics

is it possible to extend different classes with the same generic class?
I tried something like this:
Public Class A
Public Sub TestA()
Debug.Print("Test A")
End Sub
End Class
Public Class B(Of T)
Public Sub TestB()
Debug.Print("Test B")
End Sub
End Class
Public Class C
Inherits B(Of A)
Public Sub TestC()
TestA() '**<-- Thows error 'is not declared'**
TestB()
Debug.Print("Test C")
End Sub
End Class
I basicly have some usercontrols, which derive from Combobox or Textbox and i'd like both to implement some functions(and interfaces) that are defined in a base class. In C++ i'd do it with multi inheritance.
is it possible to extend different classes with the same generic class?
Generics isn't some kind of "workaround" for a lack of multiple inheritance, no. Your class C doesn't derive from A - it just means that the T in B(Of T) would be A in the context of C.
Which instance of A would you expect TestA() to be called on? Creating an instance of C certainly doesn't create an instance of A...
The fact that B(Of T) doesn't use T anywhere should be a warning signal - types which are generic but never use their generic type parameters are generally problematic.
It's hard to know exactly how to help you solve your real problem without more details, but you can't add a common base class in like this, when you also need to derive from other types which aren't under your control.
Perhaps extension methods would help?
You could make both your Combobox and your Textbox classes implement the same interface.
Then you could define extension methods on that interface class.
Thanks to your hint i got this working with extentions
Public Class Form1
Public Interface IA
Property val As String
End Interface
Public Class A
Public Sub test()
Debug.Print("test")
End Sub
End Class
Public Class C
Inherits A
Implements IA
Public Property val As String Implements IA.val
Public Sub TestC()
val = "testxxx"
TestA()
test()
End Sub
End Class
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim ct As New C
ct.TestC()
End Sub
End Class
Module TestModule
<Extension()>
Public Sub TestA(ByVal pvIA As IA)
Debug.Print(pvIA.val)
End Sub
End Module
This way every class can implement it's own 'parent' (like A here) and i don't need to implement the function TestA for every class.
thank you

How can i cast my interface object to my current object

I have following interface and class
Public Interface IMyInterface
Sub GenericImplementation()
End Interface
Public Class MyClass
Implements IMyInterface
Public Sub GenericImplementation() implements IMyInterface.GenericImplementation
'doing some stuff
end Sub
Public Sub DoRefresh()
'doing some stuff
End Sub
End Class
Now i have some code where i want to check if my object passed as interface is of the concrete type MyClass and then perform DoRefresh()
Public Sub TestMethod(testObject As IMyInterface)
Dim test = TryCast(testObject, MyClass)
If test IsNot Nothing Then
test.DoRefresh()
End If
End Sub
When i am debugging the code i can see clearly the my instance of the interface is a ´MyClass´. But when i TryCast this it return nothing.

Similar classes with different signatures

I have two classes:
Public Class Subscribing
Private _subscribingObjects As IList(Of String)
Public Sub Add(ByVal obj As SubscribeObject)
'...code...'
End Sub
Public Sub Remove(ByVal index As Integer)
'...code...'
End Sub
End Class
Public Class Providing
Private _providingObjects As IList(Of String)
Public Sub Add(ByVal obj As ProvideObject)
'...code...'
End Sub
Public Sub Remove(ByVal index As Integer)
'...code...'
End Sub
End Class
Is there a more elegant way to add do this? One class would suffice, but since the Add methods have different arguments, then one really wouldn't work.
Any help would be appreciated.
this?
Public Class SubscribingProviding(Of t)
Private _subscribingObjects As IList(Of String)
Public Sub Add(ByVal obj As t)
'...code...'
End Sub
Public Sub Remove(ByVal index As Integer)
'...code...'
End Sub
End Class
Your add functions should be fine. As long as you have different variable types being passed in you can have the function names be the same. Your remove Subs will not be allowed in the same class because it is using the same parameter Integer.
Eh.. probably not. They are different enough that you cant even Interface them.
I personally wouldn't mix the two responsibilities (of subscribing and providing) in one class. The classes themselves can easily be simplified by just inheriting from List(Of T)
Public Class Subscribing
Inherits List(Of SubscribeObject)
End Class
Public Class Providing
Inherits List(Of ProvideObject)
End Class
If you really want to get down to one class and make sure that it can only accept SubscribeObject and ProvideObject respectively, implement a common interface in both SubscribeObject and ProvideObject. Then create a generic class that accepts the interface:
' Common interface '
Public Interface ISubscribeProvideObject
End Interface
' SubscribeObject and ProvideObject both implementing the common interface '
Public Class SubscribeObject
Implements ISubscribeProvideObject
'...'
End Class
Public Class ProvideObject
Implements ISubscribeProvideObject
'...'
End Class
' Generic class accepting both types '
Public Class SubscribingProviding(Of T As ISubscribeProvideObject)
Inherits List(Of T)
'... Add() and Remove() methods only needed if custom logic applies ...'
End Class