When to use Private sub and when to use Public Sub
You also have Friend and Static. Study the official documentation:
Sub statement
Related
I have an interface IView:
Option Explicit
Public Event OnClientSelected()
Public Property Get ClientNames() As Variant
End Property
(...)
But I am not able to implement the event in my user form. Properties and subs are allowed to implement, but event not.
Is it possible to make interface implementation with events?
Did you use withevents?
I've done the following, added a test function to your class
Private WithEvents iface As clsIface
Private Sub UserForm_Initialize()
Set iface = New clsIface
End Sub
Private Sub iface_OnClientSelected()
' Event subscription
End Sub
Private Sub UserForm_Click()
iface.test
End Sub
Function added to class
Public Function test()
RaiseEvent OnClientSelected
End Function
Module Module1
Class Arithmetical
Public Function Subtract(ByVal a As Integer, ByVal b As Integer)
Return a - b
End Function
End Class
Sub Main()
Dim objArithmetical As New Arithmetical()
Console.WriteLine(objArithmetical.Subtract(80, 27))
Console.ReadKey()
End Sub
End Module
How would I implement a constructor into this? I am new to this coding and language(VB.NET) Anything would be greatly appreciated.
Thank You
If you need to implement a constructor, you can use the code below :
Public Sub New()
End Sub
For more info, you can find some documentation here : MSDN or DotNetPerls
Hope it helps !
is there a way to access a variable in Form_Load from an event handler?
Please dont mind the code, this is just a representation of my question.
Public Class Form
Public Sub Form_Load()
Dim x as string
x = MyClass.MethodGetValue()
End Sub
Private Shared Sub OnChanged()
MyClass2.MethodGetValue(x)
End Sub
End Class
It's about the scope of the variable. In your situation you need a class variable. This allows it to be used anywhere inside of this class.
Public Class Form1
Private x As Object 'pick the datatype that matches your needs
Public Sub Form_Load()
x = MyClass.MethodGetValue()
End Sub
Private Sub OnChanged()
MyClass2.MethodGetValue(x)
End Sub
End Class
Assuming an object with the following code...
Public Sub New()
Me.Name = "Default Name"
Initialize()
End Sub
Public Sub New(CustomName as String)
Me.Name = CustomName
Initialize()
End Sub
Private Sub Initialize()
'Initialize some other properties
End Sub
Is there a more elegant pattern for this use case? Some way where one constructor could call the other constructor and eliminate the need for the Initialize() method?
Yes, you could write one constructor with all the needed parameters and then write the rest with a call to Me.Constructor() without anything else in the method block that supplies the defaults.
Public Sub New(CustomName as String)
Me.Name = CustomName
End Sub
Public Sub New()
Me.New("Default Name")
End Sub
I'm not sure if there's a VB syntax for constructor chaining in the C# sense, but if I remember correctly VB can invoke other constructors internally by calling Me.New(). Which is kind of the same thing. So you should be able to do something like this:
Public Sub New()
Me.New("Default Name")
End Sub
Public Sub New(CustomName as String)
Me.Name = CustomName
'Initialize some other properties
End Sub
What are the differences between the following:
Private Sub
Private Function
Private Class
When should each one be used?
Private is a modifier than gives the scope of the class, sub, or function.
A sub and a function are both subroutines, or sections of code that can be called in the program. The difference between them is that a function has a return value and a sub does not.
A class is a group of code that can include subs, functions, and other stuff.
Sub is like a function but it doesnt returns any values it just executes a proccess
Class is a Class,
Sub and Function are methods,
private is an access modifier
now check this link,
http://msdn.microsoft.com/en-us/library/ms973814.aspx