How to pass a class instance to library class in Windows Forms

iLens 6 Reputation points
2022-05-17T10:26:49.183+00:00

How should I pass a class instance to my class library so that it can access its properties?

I have a class library "classLib" with a class "libraryClass"

Public Class libraryClass
Public Sub New(sent As clsMyClass)
sent.prop = "changed value"
End Sub
End Class

I have a console app "myApp" with a module...

Sub Main()
Dim myClassInstance = New clsMyClass
Dim classLibInstance As New classLib.libraryClass(myClassInstance)
End Sub

and a class

Public Class clsMyClass
Property prop As String = "Initial value"
End Class

"Myapp" and "classLib" have clsMyClass, and "myApp" has a reference to "classLib"

The solution fails with the error BC30311 Value of type 'clsMyClass' cannot be converted to 'clsMyClass'

Pressing Alt+Enter for help shows I should add a 'widening operator' to "libraryClass"

Public Shared Widening Operator CType(v As clsMyClass) As clsMyClass
Throw New NotImplementedException()
End Operator

but this then gives the error message BC33024 Conversion operators cannot convert from a type to the same type.

I'm sure I'm misunderstanding something here - how should I pass the instance so my class library can refer to it properties?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,835 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,579 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 112.5K Reputation points
    2022-05-17T10:39:16.05+00:00

    Make sure that clsMyClass is defined in classLib only (not in myApp).

    0 comments No comments