I'm a beginner trying to declare some variables in a module to use them across multiple sub-procedures.
It gives me the error for frontPointer: Error BC30188 Declaration expected.
I've tried adding it as Public
Module Module1
Public queue(10) As Integer
Public frontPointer As Integer
Public endPointer As Integer
Public full As Integer
Public length As Integer
frontPointer = 1
Sub Main()
End Sub
End Module
And I've tried declaring it normally too
Module Module1
Dim queue(10) As Integer
Dim frontPointer As Integer
Dim endPointer As Integer
Dim full As Integer
Dim length As Integer
frontPointer = 1
Sub Main()
End Sub
End Module
Picture of error
It's not telling you that the variable is not declared. It's telling you that, where you have that code, only declarations are allowed. Every code block at the type (class, module, structure) level has to be a declaration. This:
frontPointer = 1
is not a declaration. You need to either combine the assignment and the declaration of the variable you're assigning to:
Public frontPointer As Integer = 1
or else perform the assignment inside a method:
Sub Main
frontPointer = 1
'...
End Sub
Related
I have two classes:
clsDataElement
Public dataA As String
Public dataB As String
Public dataC As String
clsInterface
Private mDataElements() As clsDataElement
Private mCountOfDataElements As Integer
Private Sub Class_Initialize()
mCountOfDataElements = 0
End Sub
Public Sub addDataElement(dataElement As clsDataElement)
ReDim Preserve mDataElements(mCountOfDataElements)
Set mDataElements(mCountOfDataElements) = New clsDataElement
mDataElements(mCountOfDataElements).dataA = dataElement.dataA
mDataElements(mCountOfDataElements).dataB = dataElement.dataB
mDataElements(mCountOfDataElements).dataC = dataElement.dataC
mCountOfDataElements = mCountOfDataElements + 1
End Sub
Public Function getDataElement(Optional index As Integer) As clsDataElement
If Not index >= mCountOfDataElements Then
getDataElement = mDataElements(index)
End If
End Function
So as you can see, the class clsDataElement is just a containe for some data.
The class clsInterface can contain multiple Elements of clsDataElement
Now if I want to read one Data Element from the interface class, it just does not work. However, I feel like this is related to the fact, that I cannot just assign custom classes:
Public Sub TestMyClass(myInterface As clsInterface)
Dim tmpDataElement As New clsDataElement
Set tmpDataElement = myInterface.getDataElement(0)
End Sub
So when running this code I get the error
Object variable or with block variable not set
I made sure that there are multiple data elements stored in myInterface.
Do you have any ideas what I am doing wrong?
Aren't you missing the 'Set' keyword when getting the element:
Public Function getDataElement(Optional index As Integer) As clsDataElement
If Not index >= mCountOfDataElements Then
Set getDataElement = mDataElements(index)
End If
End Function
I need to get access to the public functions in a module (not a class). This is what I have tried:
Dim asm As Reflection.Assembly = Assembly.GetExecutingAssembly
Dim my_modules = asm.GetModules
For Each my_module In my_modules
Dim all_methods = my_module.GetMethods
Stop
Next
But that doesn't even get down to the module itself, I just get the name of the executable.
As #jmcilhinney said in the comments a Module is like a Class when using reflection. You can access it using GetType or GetTypes method.
Dim asm As Assembly = Assembly.GetExecutingAssembly
Dim my_module = asm.GetType("Module_Full_Name")
Dim allMethods = my_module.GetMethods()
Module in VB.NET is nothing else than static class. You not create instance of static class, just call its members from anywhere in scope where module is declared.
Public Module Mathematics
Public Function Sum(x As Integer, y As Integer) As Integer
Return x + y
End Function
End Module
Class Form1
Public Sub New()
InitializeComponent()
Dim result = Mathematics.Sum(1, 2)
End Sub
End Class
I have created a class in VBA which I would like to have some pre-set values associated with it. I am new to classes, and am wondering what is the best (/a good) way of structuring my VBA code within the class object, so that I can access these default values easily as I type. An answer should preferably:
Require relatively few additional lines of code over and above the lines which I assume will be required for the actual hard-coding of values
ie. something like an additional Sub for each hardcoded value would not be ideal
this is to prevent my class from becoming too cluttered
Allow me to use intellisense in some way to access these hard coded values
It's worth noting that my main use of these hard coded values is in setting default values to variables of my class (by looping in the initialize event), but I may also want to access them in other portions of code
What I've tried:
Declaring an Enum to save my hard coded values
'Declarations
Private Enum startingVals
Top = 10
Column_Count = 4
Left = 15
...
End Enum
Private topVal As Long 'variables which I assign default values to
Private colCnt As Long
Private leftVal As Long
Private Sub Class_Initialize()
topVal = startingVals.Top
colCnt = startingVals.Column_Count
'etc.
End Sub
This has 2 limitations;
Enums can only store Longs
Get around this by using a load of Consts instead, but then you have to remember every constant's name, plus it looks cluttered in code
Although I get Intellisense for .Top and .Column_Count, I still have to type startingVals out in full
That's significantly better than having to remember all the hardcoded constant names though
Ideally I would be able to do this
Private Sub Class_Initialize()
With startingVals 'or Dim v As startingVals, With v
topVal = .Top
colCnt = .Column_Count
'etc.
End With
End Sub
But I can't
Another approach would be to use a function to save the values,that way you could declare different types to just long.
'Declarations
Private Enum startingVals
Top = 1
Column_Count = 2
Left = 3
...
End Enum
Private topVal As Long 'variables which I assign default values to
Private colCnt As Long
Private leftVal As Long
Private Sub Class_Initialize()
topVal = getval(Top)
colCnt = getval(Column_Count)
'etc.
End Sub
Then to access the hard coded data, you have a function which takes an enum input (allowing for intellisense)
Private Function getval(dataType As startVals) As String
Const savedData As String = "1,2,1.17171717,hey,me,you" 'save the return values for the index specified by dataType
getval = Split(savedData, ",")(dataType) 'use datatype as a direct index of the array
End Function
or another way of saving the values
Private Function getval(dataType As startVals) As String
Const colV As Long = 10 'index 1
Const topV As String = "This is the top" 'index 2
'...
If dataType = ColumnCount Then getval = colV 'use dataType to check what to return
If dataType = Top Then getval = colV 'could use a select case too
'etc
End Function
But either way we still can't access the constants unless we type the function name out.
Also this approach requires me to update both the enum declaration at my class declarations portion, and the const declaration within the function itself, making the code harder to maintain.
TL;DR
What's the best method to save hardcoded values in a class object, where best is defined as
Uses VBA intellisense (autofill) so I can quickly select the value I want as I type
Is neat, self contained and concise within my class module, to avoid clutter
Can preferably hold any kind (data type) of hardcoded value (although I am only using Long in the project I'm currently working on)
Can be accessed without the need of typing an initialisation portion each time (such as a function or enum name)
Of course a With block or function equivalent would be fine, as that only requires the one instance of specifying the enum/data collection name
...to prevent my class from becoming too cluttered
I would separate the class from its initialization process adding another class lets call it Initializer. Initializer will know how to initialize my objects, will contain the default values and will fill my object with this defaults. But in the initializer you will have to write the assignments, no magic intellisense, but just simply write m_ and select from the list. HTH
Class Foo
Option Explicit
'variables which I assign default values to
Private m_topVal As Long
Private m_colCnt As Long
'Private m_leftVal As Long
Private Sub Class_Initialize()
Dim initializer As FooInitializer
Set initializer = New FooInitializer
initializer.Initialize Me
End Sub
Public Property Get TopVal() As Long
TopVal = m_topVal
End Property
Public Property Let TopVal(ByVal vNewValue As Long)
m_topVal = vNewValue
End Property
Public Property Get ColCnt() As Long
ColCnt = m_colCnt
End Property
Public Property Let ColCnt(ByVal vNewValue As Long)
m_colCnt = vNewValue
End Property
' Add Get/Let(Set) for other member variables as well
Class FooInitializer
Option Explicit
' Default startingVals values
Private m_topValDefault As Integer
Private m_columnCountDefault As Integer
'etc.
Public Sub Initialize(ByRef fooInstance As Foo)
fooInstance.TopVal = m_topValDefault
fooInstance.ColCnt = m_columnCountDefault
'etc.
End Sub
Private Sub Class_Initialize()
m_topValDefault = 10
m_columnCountDefault = 4
'etc.
End Sub
Standard module
Option Explicit
Sub test()
Dim f As Foo
Set f = New Foo
' f is now initizlized via initializer with default values
Debug.Print f.TopVal
Debug.Print f.ColCnt
End Sub
You can use constants to define the default values in a single place.
You can then easily access them with Ctrl + Space + Default...
Const Default_Top = 10
Const Default_Text = "abcd"
Private m_topVal As Long
Private m_text As String
Private Sub Class_Initialize()
m_topVal = Default_Top
m_text = Default_Text
End Sub
Public Property Get TopVal() As Long
TopVal = m_topVal
End Property
I can't claim ownership to this solution, but when I ran into it over on Code Review it was genius enough for me to have incorporated it into quite a lot of my code since.
As used in some other object-oriented languages, accessing class-internal instance variables using a this construct is very familiar. The concept is extended into VBA using the example here.
I've created a class module called CustomClass, and within it created a private custom type for use only within that class.
Option Explicit
Private Type CustomType
Top As Long
Name As String
Temperature As Double
anotherCustomObject As CustomClass
End Type
Private this As CustomType
Working this way, you can create any number of internal variables of any combination of types (including objects). Accessing and initializing each of these values is now as simple as using the this structured variable. The Class_Initialize sub shows how:
Private Sub Class_Initialize()
this.Top = 150
this.Name = "Wayne"
this.Temperature = 98.6
Set this.anotherCustomObject = New CustomClass
End Sub
Set and initialize all your values to your heart's content.
Further, you can establish each with property accessors if you like. Some of these can be Read Only:
'--- Read Only Properties
Public Property Get Name() As String
Name = this.Name
End Property
Public Property Get Temperature() As Double
Temperature = this.Temperature
End Property
Public Property Get ContainedObject() As CustomClass
Set ContainedObject = this.anotherCustomObject
End Property
And you can create some that are Read/Write:
'--- Read/Write Properties
Public Property Let Top(ByVal newValue As Long)
this.Top = newValue
End Property
Public Property Get Top() As Long
Top = this.Top
End Property
Plus, you can still use the properties easily within the class using the Me keyword:
'--- Internal Private Methods
Private Sub TestThisClass()
Debug.Print "current temperature is " & Me.Temperature
Debug.Print "the Top value is " & Me.Top
End Sub
Of course, this all works when you declare an object of CustomClass in a different module as well.
Hopefully this helps goes a ways to helping regularize your code a bit.
(For convenience, here's the whole class:)
Option Explicit
Private Type CustomType
Top As Long
Name As String
Temperature As Double
anotherCustomObject As CustomClass
End Type
Private this As CustomType
Private Sub Class_Initialize()
this.Top = 150
this.Name = "Wayne"
this.Temperature = 98.6
Set this.anotherCustomObject = New CustomClass
End Sub
'--- Read Only Properties
Public Property Get Name() As String
Name = this.Name
End Property
Public Property Get Temperature() As Double
Temperature = this.Temperature
End Property
Public Property Get ContainedObject() As CustomClass
Set ContainedObject = this.anotherCustomObject
End Property
'--- Read/Write Properties
Public Property Let Top(ByVal newValue As Long)
this.Top = newValue
End Property
Public Property Get Top() As Long
Top = this.Top
End Property
'--- Internal Private Methods
Private Sub TestThisClass()
Debug.Print "current temperature is " & Me.Temperature
Debug.Print "the Top value is " & Me.Top
End Sub
The code below works for the class that I hard coded "XCCustomers" in my RetrieveIDandName method where I use CType. However, I would like to be able to pass in various classes and property names to get the integer and string LIST returned. For example, in my code below, I would like to also pass in "XCEmployees" to my RetrieveIDandName method. I feel so close... I was hoping someone knew how to use CType where I can pass in the class name as a string variable.
Note, all the other examples I have seen and tried fail because we are using Option Strict On which disallows late binding. That is why I need to use CType.
I also studied the "Activator.CreateInstance" code examples to try to get the class reference instance by string name but I was unable to get CType to work with that.
When I use obj.GetType.Name or obj.GetType.FullName in place of the "XCCustomers" in CType(obj, XCCustomers)(i)
I get the error "Type 'obj.GetType.Name' is not defined" or "Type 'obj.GetType.FullName' is not defined"
Thanks for your help.
Rick
'+++++++++++++++++++++++++++++++
Imports DataLaasXC.Business
Imports DataLaasXC.Utilities
Public Class ucCustomerList
'Here is the calling method:
Public Sub CallingSub()
Dim customerList As New XCCustomers()
Dim customerIdAndName As New List(Of XCCustomer) = RetrieveIDandName(customerList, "CustomerId", " CustomerName")
'This code below fails because I had to hard code “XCCustomer” in the “Dim item...” section of my RetrieveEmployeesIDandName method.
Dim employeeList As New XCEmployees()
Dim employeeIdAndName As New List(Of XCEmployee) = RetrieveIDandName(employeeList, "EmployeeId", " EmployeeName")
'doing stuff here...
End Sub
'Here is the method where I would like to use the class name string when I use CType:
Private Function RetrieveIDandName(ByVal obj As Object, ByVal idPropName As String, ByVal namePropName As String) As List(Of IntStringPair)
Dim selectedItems As List(Of IntStringPair) = New List(Of IntStringPair)
Dim fullyQualifiedClassName As String = obj.GetType.FullName
Dim count As Integer = CInt(obj.GetType().GetProperty("Count").GetValue(obj, Nothing))
If (count > 0) Then
For i As Integer = 0 To count - 1
'Rather than hard coding “XCCustomer” below, I want to use something like “obj.GetType.Name”???
Dim Item As IntStringPair = New IntStringPair(CInt(CType(obj, XCCustomers)(i).GetType().GetProperty("CustomerId").GetValue(CType(obj, XCCustomers)(i), Nothing)), _
CStr(CType(obj, XCCustomers)(i).GetType().GetProperty("CustomerName").GetValue(CType(obj, XCCustomers)(i), Nothing)))
selectedItems.Add(Item)
Next
End If
Return selectedItems
End Function
End Class
'+++++++++++++++++++++++++++++++
' Below are the supporting classes if you need to see what else is happening:
Namespace DataLaasXC.Utilities
Public Class IntStringPair
Public Sub New(ByVal _Key As Integer, ByVal _Value As String)
Value = _Value
Key = _Key
End Sub
Public Property Value As String
Public Property Key As Integer
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCCustomer
Public Property CustomerId As Integer
Public Property CustomerName As String
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCCustomers
Inherits List(Of XCCustomer)
Public Sub New()
PopulateCustomersFromDatabase()
End Sub
Public Sub New(ByVal GetEmpty As Boolean)
End Sub
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCEmployee
Public Property EmployeeId As Integer
Public Property EmployeeName As String
End Class
End Namespace
'+++++++++++++++++++++++++++++++
Namespace DataLaasXC.Business
Public Class XCEmployees
Inherits List(Of XCEmployee)
Public Sub New()
PopulateEmployeesFromDatabase()
End Sub
Public Sub New(ByVal GetEmpty As Boolean)
End Sub
End Class
End Namespace
From MSDN
CType(expression, typename)
. . .
typename : Any expression that is legal
within an As clause in a Dim
statement, that is, the name of any
data type, object, structure, class,
or interface.
This is basically saying you can't use CType dynamically, just statically. i.e. At the point where the code is compiled the compiler needs to know what typename is going to be.
You can't change this at runtime.
Hope this helps.
Since List(Of T) implements the non-generic IList interface, you could change your function declaration to:
Private Function RetrieveIDandName(ByVal obj As System.Collections.IList, ByVal idPropName As String, ByVal namePropName As String) As List(Of IntStringPair)
And then your troublesome line would become (with also using the property name parameters):
Dim Item As IntStringPair = New IntStringPair(CInt(obj(i).GetType().GetProperty(idPropName).GetValue(obj(i), Nothing)), _
CStr(obj(i).GetType().GetProperty(namePropName).GetValue(obj(i), Nothing)))
Of course, you could still have the first parameter by Object, and then attempt to cast to IList, but that's up to you.
ctype is used to convert in object type.
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.