Bind Class to UserControl - vb.net

Is it possible to bind a declared class to a UserControl and will it keep the class as a snapshot or as a running code?
Example: I declare a class named MyClass that has a single property called strTest. I set that value as "What". I have a UserControl on the form that needs access to strTest, so I pass the class to it by declaring a Property in the UserControl Called MyClass and on the main form I send it MyClass.
UserControl.MyClass = MyClass
Now, if I update MyClass on the main form by changing strTest to equal "Where", will the class in UserControl also update to "Where" or will it still have "What"?
Edit, It takes it like a snapshot.
So, how do I data bind a class to a class inside a UserControl (or other form for that matter)?

Related

VBA Static Class "WithEvents"?

Disclaimer - I am by no means a VBA expert; I am a hack.
However, I've got some class modules that are static (using Attribute VB_PredeclaredId = True
)
I'd like to define some custom events with them, too (Public Event Foo(ByVal Bar As Boolean); however, since I am not instantiating the class, I'm not finding any info on any "Attributes" that will include "WithEvents" when "PreDeclaredID" is true.
Yes, I can use the class without its being static; however, I'd prefer to find a way for it to be declared automatically WithEvents, if it's possible.
The class has static semantics, but it's not static in the static sense you're referring to.
The VB_PredeclaredId attribute set to True means the compiler generates a global (or project-scoped, if the class is private) instance that is named after the class module itself.
In other words, there is literally an object/instance named Class1 (assuming the class module is named Class1), exposing the default interface defined by the Class1 module (i.e. it's an object whose compile-time type is Class1 regardless of what other interfaces that class might be implementing).
So you are not instantiating it, but the VBA compiler does.
And that object behaves every single bit the same as any other object you might have - there is no reason a Public Event could not be declared in it, and you can Set a WithEvents object variable to that "free" global instance, which you can refer to by name from anywhere in the project:
Private WithEvents Thing As Class1 '<~ requires Public Event declaration in Class1.
Private Sub Class_Initialize()
Set Thing = Class1 '<~ will not compile unless Class1 has VB_PredeclaredId=True.
End Sub

marked and unmarked class in pharo 2.0 smalltalk

i need to implement the message markedSubclass in pharo that works just like subclass but i need the class that gets created to be somehow marked,for example i tried adding a unique instance variable to it after creating it but it's just not working,maybe i'm adding it to a wrong place.
the requirments are:
every subclass of this marked class should also be marked even if it
was created via subclass (not markedSubclass).
other than that a marked class should function just as a regular class should.
any help would be appreciated.
example:
User markedSubclass: #MarkedUser
User subClass: #UnmarkedUser
MarkedUser subclass: #MarkerUser2
i need to somehow know that MarkedUser and UnmarkedUser are both marked classes.
what i thought of lately is adding the method "isMarked" to Class class and this way all
the classes will have it, and each class will override it accordingly so if we write
User class isMarked.
it will return false but if we write:
MarkedUser class isMarked.
MarkedUser2 class isMarked.
it will return true for both.
but where can i add this method?and how can i make a class override the method in runtime?
Add a class method like the following to your User class:
markedSubclass: className
| subclass |
subclass := self subclass: className asSymbol.
subclass class compile: 'isMarked', String cr, String tab, ' ^ true'.
^ subclass
Then try in a workspace:
User markedSubclass: 'MyMarkedSubclass'
Add an #unmarkedSubclass: class method accordingly.
You could then override the general #subclass: method in your User class to set the same marker as the receiver.

Inheritance for VBA code in MS Access

I've started to learn VBA in Access. I have read that the language has no inheritance.
And then I read an example code which seems like it actually has inheritance:
Dim ctrl As Control
...
If TypeOf ctrl Is TextBox Then ...
If TypeOf ctrl Is ListBox Then ...
It seems to me as the TextBox, ListBox were inherited from the Control. Could somebody explain this?
No. They are not derived from the Control class. They implement Control's definition/ methods and properties signatures. The way TypeOf and Is operators work it's they check whether the instance of a class Implements one of 3 categories (listed below).
open a new workbook
Go to VBE and add
a class module and name it: MyClass
in the code view only add Implements MyInterface
a class module and name it: MyInterface
in the code view - do nothing/leave empty
a module and copy paste the below code and run it
Sub Main()
Dim cls As MyClass
Set cls = New MyClass
Debug.Print TypeOf cls Is MyClass
Debug.Print TypeOf cls Is MyInterface
End Sub
The result may be surprising
True
True
cls variable is of two types - MyClass and MyInterface
as you can see cls doesn't inherit nothing from MyInterface but definition. When using TypeOf and Is it actually shows true because MyClass implements MyInterface. Not because it's derived from the MyInterface class but because it implements it.
Now, suppose
result = TypeOf objectexpression Is typename
The TypeOf operator determines whether the run-time type of variable is compatible with typename. The compatibility depends on the type category of typename. There are three categories
Class objectexpression is of type typename or inherits from typename
Structure objectexpression is of type typename
Interface objectexpression implements typename or inherits from a class that implements typename
Specifically try to understand the 3rd category - Interface.
I think at this point you should really understand why TypeOf varName Is varType shows True for the TextBox and ListBox...
When you do VBA inheritance, you can only use Implements keyword to
implements a class definition. That is, the class to be implemented is
equivalent to C++/C#'s abstract class: only having property/method
definition.
Generally, your example isn't a form of a class polymorphism. Class polymorphism occurs when you are actually deriving an instance of one to class to another. This isn't the case. Even though TextBox and ListBox are both of a Control type they aren't actually derived from Control class. Note: they may as well be members of another collection - they would be TypeOf the higher in the object hierarchy type ( forms, also Component and IComponent becuase Forms implements that).

How does "Overloads" work in a child class?

I have a base class and a child class and they both have the same property and I don't understand why VB wants me to use "Overloads" for the property in the child class. The difference is the child class version of the property is Shared while the parent class is basically there for structure. The properties look like this:
Public MustInherit Class Parent
Public ReadOnly Property Species As String
Get
Return "Should get species from a child."
End Get
End Property
End Class
Public Class Child
Inherits Parent
Public Shared ReadOnly Property Species As String
Get
Return "Species1"
End Get
End Property
End Class
Species is flagged in the line Public Shared ReadOnly Property Species As String in the child class with the warning message
property 'Species' shadows an overloadable member declared in the base
class 'Parent'. If you want to overload the base method, this method
must be declared 'Overloads'.
What I want to know is why does it want this to be overloaded? Overloading is typically used when different parameters are being passed into functions with the same name which is well documented, but I've found nothing explaining why overloads is suddenly suggested in a situation like this.
Note: that the code properly reports "Species1" regardless of if have the "Overloads" or not adding to my confusion of what it actually does...
If you want to overload the base method, this method must be declared 'Overloads'.
The error message is too generic. Note how it talks about a method even though the warning is about a property. You cannot overload a property.
If I were the King of France, I would have written the error message like:
Property 'Species' hides the 'Species' property inherited from the 'Parent' base class. Use the Shadows keyword to suppress this warning if hiding was intended. Change the name of the property if hiding was not intended.
This warning should almost never be ignored because is almost always identifies a code smell. Using the Shared keyword for Child.Species is very strange and almost certainly not what you intended. Any code that uses your Child object through a reference of type Parent will always get the wrong species name since it will use the base property. The more sane thing to do here is to declare the Parent.Species property Overridable and use the Overrides keyword in Child.Species property declaration, without Shared.
If you shadow a member - the base class will still use it's version of the member when called. For example, if a function in your base class called Species it would still get the value "Should get species from a child."
In this case, overloading will cause functions in the base class to use the child's value.
To make this a bit clearer... the following code's message box says "Original Value", not "New Value"
Public Class Form1
Dim X As New Child
Dim Y = MsgBox(X.ShowMe)
End Class
Public Class Parent
Public Function ShowMe() As String
Return member
End Function
Public Property member As String = "Original value"
End Class
Public Class Child
Inherits Parent
Public Property member As String = "New value"
End Class

Accessing the same instance of a class in another form

I'm sure this is a simple question, but I don't have enough experience to know the answer. :)
DataClass, Form1, Form2
I have a public class, DataClass, in a separate file, DataClass.vb. In DataClass I have data stored in several arrays that I need to access. I have methods in DataClass so that I can access the data. One of them is GetName. Everything works fine on Form1. I need to access the same data in the arrays on a another form, but I am required to call a new instance of the class, so when I call the methods to access the arrays, the data is empty.
I've seen some threads mention creating a singleton class, but most are about C# which I am not as familiar with.
What is the best practice?
There are many ways in which you can do this.
One of them would involve creating a Module and then making the variable that instantiates your class Public inside the module:
Module MyGlobalVariables
Public MyDataClass As DataClass
End Module
Now, all the forms in your project will be able to access the DataClass via MyGlobalVariables.MyDataClass.
A preferable method would be to add a property to your Form2 that can be set to the DataClass instance:
Public Property MyDataClass As DataClass
Then, you would instantiate your Form2 as follows (assuming the variable you use to instantiate DataClass in Form1 is called _dataClass):
Dim frm2 As New Form2()
frm2.MyDataClass = _dataClass
frm2.Show()
And finally, another way would be to override the constructor of Form2 and allow it to receive a parameter of type DataClass. Then, you could instantiate Form2 as:
Dim frm2 As New Form2(_dataClass)
Hope this helps...
You can create a singleton class like this
Public Class DataClass
Public Shared ReadOnly Instance As New DataClass()
Private Sub New()
End Sub
' Other members here
End Class
You can access a single instance through the shared Instance member which is initialized automatically. The constructor New is private in order to forbid creating a new instance from outside of the class.
You can access the singleton like this
Dim data = DataClass.Instance
But this is not possible
Dim data = new DataClass() 'NOT POSSIBLE!
Since the singleton class is accessed through the class name, you can access it from the two forms easily.