How to use variable value to access class member in vb.net - vb.net

Not sure if this is possible:
i want to create a sub with 2 inputs a
Public sub testsub(byval x as string,byval y as string)
dim z as class specialdevicelcass
z.y = x
end sub
is this even posible, y variable will always be a member of the special device class

You can use reflection for this:
Public Sub testsub(ByVal x As String, ByVal y As String)
Dim z As New specialdevicelcass
Dim prop = z.GetType.GetProperty(y)
prop.SetValue(z, x)
End Sub

Related

Passing variables from one procedure to another

How can I use a vairable from Sub Main, in another sub.
Example
Sub Main()
Dim x As Long
End Sub
Sub Test() Dim y = x End Sub
Is this possible on vb.net
You could pass it to a function and return it again:
Sub Main()
Dim x As Long
' Give x a value
x = Test(x)
' Do something else with x
End Sub
Function Test(x As Long) As Long
' Do something with x
Return x
End Sub
Or declare it as a global variable:
Dim x As Long
Sub Main()
' Do something with x
End Sub
Sub Test()
Dim y = x ' Do something else
End Sub
There is a very easy way to do this.
Declare the value out of all the Subs on the top.
Public Class Form 1
Dim x As Long
Private Sub Main ()
End Sub
Private Sub Test ()
End Sub
End Class
And then write y = x on whichever Sub you want!

workaround to VB.Net 2003 System.Collections.Generic?

I'm working in an old web application (vb.net 2003) and I'm trying to use a generic list of a custom class.
I realized that System.Collections.Generic was introduced in .Net 2 according to link
Is there any alternative to the list? For instance an array of class?
Let's say I have the following class definition:
Public Class Box
Public x As Integer
Public y As Integer
End Class
And an array of Class Box:
Dim BoxList() As Box
BoxList(0).x = 1
BoxList(0).y = 1
BoxList(1).x = 2
BoxList(2).y = 2
But I'm getting an error when BoxList(0).x = 1 error: Object reference not set to an instance of an object
I'm just guessing here.
Use ArrayList, like this:
Dim BoxList As New ArrayList
Dim box = New Box()
box.x = 1
box.y = 2
BoxList.Add(box)
Note: It is recommended that you add a constructor to the Box class that will accept the x and y values, like this:
Public Class Box
Public x As Integer
Public y As Integer
Public Sub New(ByVal _x As Integer, ByVal _y As Integer)
x = _x
y = _y
End Sub
End Class
Now you can shorten your ArrayList code to this:
Dim BoxList As New ArrayList
BoxList.Add(New Box(1, 2))
To use the values in the ArrayList you will need to un-box (pun not intended) the value out of the ArrayList, like this:
For Each box In BoxList
' Use x value, like this
CType(box, Box).x
Next
OR (as Meta-Knight suggested)
For Each box As Box In BoxList
' Now box is typed as Box and not object, so just use it
box.x
Next
You can create your own custom collection class - this is what we had to do back before generics. This article from MSDN gives you the specifics:
''' Code copied directly from article
Public Class WidgetCollection
Inherits System.Collections.CollectionBase
Public Sub Add(ByVal awidget As Widget)
List.Add(aWidget)
End Sub
Public Sub Remove(ByVal index as Integer)
If index > Count - 1 Or index < 0 Then
System.Windows.Forms.MessageBox.Show("Index not valid!")
Else
List.RemoveAt(index)
End If
End Sub
Public ReadOnly Property Item(ByVal index as Integer) As Widget
Get
Return CType(List.Item(index), Widget)
End Get
End Property
End Class

Override a Function

The following "aMacro" returns the error "Ambiguous name Detected" I understand why. Anybody know a way to override the first definition and and only use the definition inside of the function so that aFunction will return x - y ?
Besides changing the name.
Function aFunction(x As Integer, y As Integer) As Integer
aFunction = x + y
End Function
Sub aMacro()
Function aFunction(x As Integer, y As Integer) As Integer
aFunction = x - y
End Function
MsgBox aFunction(4, 3)
End Function
A function can be Private or Public, but the scope is always the whole module.
This can simulate "override function" with 4 class modules:
Functions, IFunction, FunctionAdd, FunctionSubtract.
class module Functions:
Function aFunction(x As Integer, y As Integer) As Integer
aFunction = x + y
End Function
interface IFunctions:
Function aFunction(x As Integer, y As Integer) As Integer
End Function
class module FunctionAdd:
Implements IFunctions
Private mFunctions As Functions
Private Sub Class_Initialize()
Set mFunctions = New Functions
End Sub
Private Sub Class_Terminate()
Set mFunctions = Nothing
End Sub
Private Function IFunctions_aFunction(x As Integer, y As Integer) As Integer
IFunctions_aFunction = mFunctions.aFunction(x, y) ' Uses the standard aFunction
End Function
Class module FunctionSubtract:
Implements IFunctions
Private mFunctions As Functions
Private Sub Class_Initialize()
Set mFunctions = New Functions
End Sub
Private Sub Class_Terminate()
Set mFunctions = Nothing
End Sub
Private Function IFunctions_aFunction(x As Integer, y As Integer) As Integer
IFunctions_aFunction = x - y ' Override aFunction, subtract values
End Function
You can test this with this:
Dim f As IFunctions
Set f = New FunctionAdd: Debug.Print f.aFunction(1, 2)
Set f = New FunctionSubtract: Debug.Print f.aFunction(1, 2)
Of course this is tedious for one function.
I could be useful is you have a lot of functions to override in many classes.
Try adding in the Optional value in the function. If the optional value isnt included in the call then it wont be references in the function.
Function aFunction(x As Integer, y As Integer, Optional override As Boolean) As Integer
If Not override Then
aFunction = x + y
Else
aFunction = x - y
End If
End Function
Sub aMacro()
MsgBox aFunction(4, 3)
MsgBox aFunction(4, 3, True)
End Sub

Return an array of a classes method names

I've got the following. How do I query the class myExample and return an array of the class's method names?
Sub Main()
Dim x As New myExample
'<<would like to return an array of x's method names i.e. {"hello","world","foo","bar"}
Console.WriteLine("press [enter] to exit")
Console.Read()
End Sub
Class myExample
Public Shared y As Integer
Public Sub hello()
y = 1
End Sub
Public Sub world()
y = 2
End Sub
Public Sub foo()
y = 3
End Sub
Public Sub bar()
y = 4
End Sub
End Class
All methods (incl. inherited):
Dim example As New myExample()
Dim t As Type = example.GetType()
Dim methods = t.GetMethods()
Dim allMethodNames() As String = methods.Select(Function(m) m.Name).ToArray()
Type.GetMethods

Implementing my own interface in VBA - Error: Object module needs to implement 'x' for interface 'y'

How do I implement my class ClsInterface, which has this code:
Public Function add(x As Integer, y As Integer) As Integer
End Function
in my class Class2, which has this code:
Implements ClsInterface
Public Function add(x As Integer, y As Integer) As Integer
add = x + y
End Function
My test code is
Public Sub test()
Dim obj As New Class2
MsgBox obj.add(5, 2)
End Sub
This always comes up with the following error:
Microsoft Visual Basic
Compile error:
Object module needs to implement 'add' for interface 'ClsInterface'
OK/Help
but there is no help on Microsoft help (when I press on Help button).
Any Ideas?
Your Class2 must look like:
Implements ClsInterface
Private Function ClsInterface_add(x As Integer, y As Integer) As Integer
ClsInterface_add = x + y
End Function
Check out the drop-down boxes at the top of Class2's code window, you can see what base object you can refer to; Class or ClsInterface.
In your test code you want:
Dim obj As New ClsInterface
If you want to call across the interface.
I would also recommend naming interfaces in the form ISomeDescription and using Dim then Set rather than Dim As New.