Communicating with a class that's in use in VB.NET - 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?

Related

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.

How to load an internal class in end-user compiled code ? (advanced)

I have a main program with two classes
1- A winform that contains two elements :
Memo Edit to type some code;
Button named compile.
The end user may type some VB.Net code in the memo edit and then compile it.
2 - A simple test class :
Code :
Public Class ClassTest
Public Sub New()
MsgBox("coucou")
End Sub
End Class
Now I would like to use the class ClassTest in the code that will be typed in the MemoEdit and then compile it :
When hitting compile I recieve the error :
The reason is that, the compiler can't find the namespace ClassTest
So to summarize :
The class ClassTest is created in the main program
The end user should be able to use it and create a new assembly at run time
Does anyone know how to do that please ?
Thank you in advance for your help.
Code of the WinForm :
Public Class Form1
Private Sub SimpleButtonCompile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButtonCompile.Click
Dim Code As String = Me.MemoEdit1.Text
Dim CompilerResult As CompilerResults
CompilerResult = Compile(Code)
End Sub
Public Function Compile(ByVal Code As String) As CompilerResults
Dim CodeProvider As New VBCodeProvider
Dim CodeCompiler As System.CodeDom.Compiler.CodeDomProvider = CodeDomProvider.CreateProvider("VisualBasic")
Dim Parameters As New System.CodeDom.Compiler.CompilerParameters
Parameters.GenerateExecutable = False
Dim CompilerResult As CompilerResults = CodeCompiler.CompileAssemblyFromSource(Parameters, Code)
If CompilerResult.Errors.HasErrors Then
For i = 0 To CompilerResult.Errors.Count - 1
MsgBox(CompilerResult.Errors(i).ErrorText)
Next
Return Nothing
Else
Return CompilerResult
End If
End Function
End Class
Here is the solution :
If the end user wants to use internal classes he should use the command : Assembly.GetExecutingAssembly
The full code will be :
Code :
Imports System.Reflection
Imports System
Public Class EndUserClass
Public Sub New()
Dim Assembly As Assembly = Assembly.GetExecutingAssembly
Dim ClassType As Type = Assembly.GetType(Assembly.GetName().Name & ".ClassTest")
Dim Instance = Activator.CreateInstance(ClassType)
End Sub
End class

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

Variable Protection Level in Script

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

My vb 2003 code can't compile

This is my first post. Please forgive me for asking a basic question as I'm new to programming.
I have following code and it just didn't compile
Module Module1
Public Sub Test
dim a as New TestClass()
dim b as string
b = a.ReturnString()
End Sub
End Module
Public Class TestClass
Public Function ReturnString() as string
Return "Hello World"
End Function
End Class
EDIT: problem solved
Lesson: Need to instantiate class before using it, many thanks to Gens and all of you!
You have 2 End Class statements, remove one.
It looks like you need to put your Test method inside a Module in order for it to Compile
Module Module1
Public Sub Test
dim a as TestClass()
dim b as string
b = a.ReturnString()
End Sub
End Module
Public Class TestClass
Public Function ReturnString() as string
Return "Hello World"
End Function
End Class
EDIT
As was pointed out by Blindy, you had double End Class statements
Try something like this
vbc <filename>.vb
with
Public Class Main
Shared Sub Main
Dim main as New Main
main.Test()
End Sub
Public Sub Test
dim a as New TestClass
dim b as string
b = a.ReturnString()
End Sub
Public Class TestClass
Public Function ReturnString() as string
Return "Hello World"
End Function
End Class
End Class
Your TestClass need to be instantiated before use. To instantiate a class, use new keyword before the class name
dim a as new TestClass()
dim b as string
b = a.ReturnString()