Variable Protection Level in Script - vb.net

I am creating a script programatically. However I cannot access the dataset from the modeule. See "Sample" below. ('ds' is not declared. It may be inaccessible due to its protection level.)
In the Sub main() I can access ds without a problem
In the Sub test() in Cacl_Module I cannot access ds.
Namespace Evaluator
Public Class Evaluator
Public ds As New DataSet
Public ComboBox1 As New ComboBox
Public CheckBox1 As New CheckBox
Public TextBox1 As New TextBox
Sub main()
Debug.Print(ds.Tables("Table1").Rows.Count)
Debug.Print(CheckBox1.Checked)
End Sub
End Class
Public Module Calc_module
Sub test()
Debug.Print(ds.Tables("Table1").Rows.Count)
Debug.Print(CheckBox1.Checked)
End Sub
End Module
End Namespace

That's because ds is a member of the Evaluator class. In the Main method of the Evaluator class, you can access its members. But in the test method of the Calc_Module, you don't have an instance of Evaluator. You should create one, like that:
Public Module Calc_module
Sub test()
Dim MyEvaluator as new Evaluator()
Debug.Print(MyEvaluator.ds.Tables("Table1").Rows.Count)
Debug.Print(MyEvaluator.CheckBox1.Checked)
End Sub
End Module
Or even better (no need to duplicate code)
Public Module Calc_module
Sub test()
Dim MyEvaluator as new Evaluator()
MyEvaluator.main()
End Sub
End Module

Related

Assigning class Instance to Interface

Have two questions regarding the code below:
In the following code, the class Test implement the interface Test_Interafce. I have read in more than one articles that when a class implements an interface, we should assign the class instance to the interface. Going by that in the following code the statement Dim t As New Test() should be replaced by Dim t As Test_Interface = New Test(). However I do not understand the advantage or need for the same. When I instantiate the class Test by simply writing Dim t As New Test(), I am able to access all elements (even the procedures of the interface implemented by the class) through the instance "t" and the code seems to be working fine. So then why to assign a class instance to an interface?
Infact if I write Dim t As Test_Interface = New Test() then through "t" does not allow me to access the subroutine CHECK in the class Test. The error being displayed is: "CHECK is not a member of Test_Interface". So isnt that a disadvantage??!!
What is the use of the statement: Throw New NotImplementedException(). This statement comes automatically when I implement teh interface in a class/structure.
The code is found below:
Module Module1
Interface Test_Interface
Function Length(ByVal s As String) As Integer
Sub Details(ByVal age As Integer, ByVal name As String)
End Interface
Sub Main()
Dim t As New Test() 'Alternate definition: Dim t As Test_Interface = New Test()
t.Details(31, "Mounisha")
Console.WriteLine("Length of the string entered = {0}", t.Length("Hello"))
t.check()
Console.ReadLine()
End Sub
End Module
Class Test
Implements Test_Interface
Public Sub Details(age As Integer, name As String) Implements Test_Interface.Details
Console.WriteLine("Age of person = {0}", age)
Console.WriteLine("Name of person = {0}", name)
'Throw New NotImplementedException() ----> what does this do?
End Sub
Public Function Length(s As String) As Integer Implements Test_Interface.Length
Console.WriteLine("Original String: {0}", s)
Return s.Length()
'Throw New NotImplementedException()
End Function
Sub check()
Console.WriteLine("The sub can be accessed")
End Sub
End Class

Communicating with a class that's in use in VB.NET

I'm trying to communicate with a class that's already in use, something like this.
Assume "PROJECTNAME" as a namespace or the whole project
Class1:
Public Class Class1
Dim Text1 As String
Sub ChangeString(input as String)
Text1 = String
End Sub
Sub Main()
Do
' Code to prevent the Process stopping (Ignore This)
Loop
End Sub
End Class
Class 2:
Imports PROJECTNAME
Public Class Class2
Dim ForeignClass1 As New Class1
Dim ForeignClass2 As New Class1
Sub Main()
Do
'Code to prevent the Process stopping(Ignore This)
Loop
End Sub
End Class
Class 3:
Imports PROJECTNAME
Public Class Class3
Dim CustomClass As New Class2
Sub Main()
'Code here to interact between "ForeignClass2" and "ForeignClass1"
End Sub
End Class
The codes above is a concept that I'm trying to explain, not the actual code as it's a private code that I'm not suppose to expose. So what I want is to make "ForeignClass2" interact with "ForeignClass1" or make "ForeignClass1" interact with "ForeignClass2" without touching or making a new "Class1" array and just "ForeignClass1" and "ForeignClass2" only. How can I do that?

VBA get instance name inside of class

Sub Main()
Dim test As New Class1
End sub
Class1:
Private Sub Class_Initialize()
msgbox(Name_of_Class_Instance)
End Sub
I want the msgbox to show "Test"
Can that be done in VBA?
As per my understanding, you are trying to get the class instance name which is declared in module.
First of all you need to do is create a class like below.
Public classname As String
'Below method is going to get the value from module.
Public Sub Class_Initialize()
MsgBox classname
End Sub
Second, create a module like below.
Sub getname()
Dim test As Class1
Set test = New Class1
'Here we are passing the class name as 'test' and executing the 'Class_Initialize' method
With test
.classname = "test"
.Class_Initialize
End With
End Sub
Now you will get the instance name of the class.

Calling private sub from module, concatenated reference

I have a couple of forms (i.e. frmTest) with bound comboboxes (i.e. cboTest). I'm trying to solve the NotInList event by a public sub, which calls back a button click sub's in these forms (i.e. btnTest_Click).
Form frmTest:
Private Sub cboTest_NotInList(NewData As String, Response As Integer)
Response = acDataErrContinue
Item_NotInList NewData, Me, "btnTest"
End Sub
Public Sub btnTest_Click
'....
End sub
Module:
Public strNotInList_Text As String
'public variable to store entered text
Public Sub Item_NotInList (strNewData As string, frmForm As Form, strControl As String)
Dim strControl_Sub As String
strNotInList_Text = strNewData
strControl_Sub = "." & strControl & "_Click"
Application.Run frmForm.Name & strControl_Sub
End Sub
Acces returns an error "Program ... didn't found a procedure frmTest.btnTest_Click."
Why ?
Reference frmTest.btnTest_Click looks to be correct. Sub btnTest_Click is declared as public.
Thank you for yor help.
Couldn't you create an interface let say IMyInterface with this method btnTest_Click (should be named differently) and let the forms you want to call this method implement this interface. Then change the signature of Item_NotInList like this:
Public Sub Item_NotInList (strNewData As string, frmForm As IMyInterface, strControl As String)
' ...
frmForm.btnTest_Click
' ...
Because the instance of the form is available in the method Item_NotInList you simply call the target method. Does this help?
Example:
Add class module and name it e.g. IMyInterface (can be named according to your needs). Add empty body of the method (don't add any implementation).
IMyInterface
Public Sub TestClick()
' will be implemented in your forms
End Sub
Then implement this interface in your forms e.g. in Form frmTest and others which should be used with the Item_NotInList method.
Form frmTest example
Implements IMyInterface
Private Sub IMyInterface_TestClick()
' here goes your implementation
End Sub
Standard Module test code
Sub test()
Dim f1 As UserForm1
Set f1 = New UserForm1
Item_NotInList f1
Dim f2 As UserForm2
Set f2 = New UserForm2
Item_NotInList f2
End Sub
Sub Item_NotInList(testForm As IMyInterface)
testForm.TestClick
End Sub
Thats it. HTH

Retrieve Data/Variables Produced by a Thread in VB.NET

I want to run some processing-intensive code in a thread upon starting Excel, and have included a simplified example below. A call to clsInit.Main is the starting point. The code executed in this thread (Sub DoIt) then modifies a variable (rng) that will be saved for use later when I access it by calling Sub GetIt. It doesn't work. Why? Thanks for the help.
Imports System.Threading
Public Class clsInit
Shared Sub Main()
Dim t As New Thread(AddressOf clsTest.DoIt)
t.Start()
End Sub
End Class
Public Class clsTest
Private Shared rng As Excel.Range
Public Shared Sub DoIt()
rng = Globals.ThisAddin.Application.ActiveCell
End Sub
Public Shared Sub GetIt()
MsgBox(rng.Address)
End Sub
End Class