Expression is not a method from within a class - vb.net

I am getting an error when trying to call a form from within a method:
Expression is not a method
My code structure looks like this:
Public Class frmMain
Class Server
Private Shared Sub StringMessageReceived()
Call frmMM()
End Sub
End Class
End Class
How can I call the windows form within the class?

You create an instance of the form, and then call its Show (or ShowDialog) method:
Public Class frmMain
Class Server
Private Shared Sub StringMessageReceived()
Call New frmMM().Show()
End Sub
End Class
End Class

Related

Force Full Path For Method

I'm not sure whether that's the best title but what I'm trying to do is set certain Subs and Functions to be only accessible from other functions by qualifying the exact location.
For example, I have a module called modShared.
In this module there is a function called LogForm which returns a Form.
In other areas of my code I have functions where the name begins with Log so I want the LogForm function to only be accessible and only appear on Intellisense when I type in modShared.LogForm and not just LogForm.
Is this possible because it would help me immensely?
Thanks
Just change your module to a static class and apply the Shared modifier to every method and you should be good to go.
Public NotInheritable Class modShared
Private Sub New() 'Prevent initialization.
End Sub
Public Shared Function LogForm() As Form
'Do stuff...
End Function
End Class
One way of doing this is placing your modules inside a Namespace
Namespace UtilityMethods
Module modShared
Public sub LogForm()
'Code Here
End sub
End Module
End Namespace
And you could use this code by either calling:
UtilityMethods.LogForm
or
Namespace UtilityMethods
Public Class MyClass
Public sub ClassMethod
LogForm()
end sub
End Class
End Namespace

I want main class event to be raised in supported form

I have public class, with event declared in it.
And have procedure which shows modal form.
The form has buttons, on whichs click I want the main event to be raised. How to achieve this?
for example:
Public class MyClass
Public Event MyEvent(arg as date)
Public Sub ShowForm()
using frm as new frmMyForm
frm.showDialog()
End Using
....
End Class
On forms button click event
RaiseEvent MyClass.MyEvent(Today)
I have passed egzemplar of class as property, but nothing helped.
I have solved problem. I have really created method in the main class with date parameter. In button click I'm calling above method of passed egzemplar with desired argument. and then raising event in a class. it works fine now.
In my situation code is more harder, therefore I provide only a simple example instead.
Public class MyClass
Public Event MyEvent(arg as date)
Public sub FromOtherPlanet(dt as Date)
RaiseEvent MyEvent(dt)
End sub
Public Sub ShowForm()
using frm as new frmMyForm
frm.Egz = Me
frm.showDialog()
End Using
End Sub
....
End Class
in the form
Public class MyForm
dim Temp_Egz as MyClass
public property Egz as Myclass
Get
.......
Set
....
End Property
private sub button1_click.....
egz.FromOtherPlanet(today)
end sub
end class

Inner class Factory Method

I have tried to implement the Factory method with a trick, but for some reason is not working:
Public Class MetamodelElement
Public Class MetamodelElementFactoryBase
End Class
Private Sub New()
End Sub
End Class
Public Class MetamodelElementFactory : Inherits MetamodelElement.MetamodelElementFactoryBase
Function CreateMetamodelElement() As MetamodelElement
Return New MetamodelElement()
End Function
End Class
It says that the class does not have access to the private method (constructor).
I have followed an example from C# in this post:
Hidden Features of C#?
The compiler complains that you are trying to use the Private constructor from outside of the class. That is not allowed. So either make it Public or don't call it.
Public Class MetamodelElement
Public Class MetamodelElementFactoryBase
End Class
Public Sub New() ' <---- HERE!!! Now it works because it's public
End Sub
End Class
Public Class MetamodelElementFactory
Inherits MetamodelElement.MetamodelElementFactoryBase
Function CreateMetamodelElement() As MetamodelElement
Return New MetamodelElement() ' <--- HERE was the error
End Function
End Class
You can access private class members only from inside of the class.
MSDN: Access Levels
According to the C# code that you've linked you have to move the constructor into the class that you are inheriting from. Then you can also use Protected:
Public Class MetamodelElement
Public Class MetamodelElementFactoryBase
Protected Sub New()
End Sub
End Class
End Class

Defer (lazy creation) or suppress creation of Unity Call Handler Attributes until the decorated method is actually called

I am trying to use Interception Call Handler and Handler Attributes on my interfaces (or implementation). Lets say my Interface has two methods DoSomething() and DoSomethingElse(), and I only have the interceptor for DoSomethingElse(); when I resolve my main interface the constructor for the Call Handler get called even if I never invoke DoSomethingElse().
I tried this by resolving to Lazy(of IMainInterface) but the moment I call the function DoSomething() the Call Handler is still created unnecessarily. Is there a way to prevent this from happening either by code or configuration. Here is my sample implementation
Handler Attribute and Call Handler
<AttributeUsage(AttributeTargets.Method)>
Public Class NormalSampleAttribute
Inherits HandlerAttribute
Public Sub New()
Console.WriteLine("Constructor of Normal Sample Attribute")
End Sub
Public Overrides Function CreateHandler(container As Microsoft.Practices.Unity.IUnityContainer) As Microsoft.Practices.Unity.InterceptionExtension.ICallHandler
Console.WriteLine("Create Handler")
Return New SampleCallHandler
End Function
End Class
Public Class SampleCallHandler
Implements ICallHandler
Public Sub New()
Console.WriteLine("Constructor of Sample Call handler")
End Sub
Public Function Invoke(input As IMethodInvocation, getNext As GetNextHandlerDelegate) As IMethodReturn Implements ICallHandler.Invoke
Console.WriteLine("Invoke of Sample Call handler - " & input.MethodBase.Name)
Return getNext.Invoke(input, getNext)
End Function
Public Property Order As Integer Implements ICallHandler.Order
End Class
Interface and implementation
Public Interface IMainInterface
Sub DoSomething()
<NormalSample()>
Sub DoSomethingElse()
End Interface
Public Class MainClass
Implements IMainInterface
Public Sub New()
Console.WriteLine("main class - Constructor")
End Sub
Public Sub DoSomething() Implements IMainInterface.DoSomething
Console.WriteLine("main class do something...")
End Sub
Public Sub DoSomethingElse() Implements IMainInterface.DoSomethingElse
Console.WriteLine("main class do something else...")
End Sub
End Class
Main module to register and execute the method
Module Module1
Public container As IUnityContainer
Sub Main()
container = New UnityContainer
DoRegistrations()
Console.WriteLine("Before lazy Resolve")
Dim lmc As Lazy(Of IMainInterface) = container.Resolve(Of Lazy(Of IMainInterface))()
Console.WriteLine("Before making lazy function call")
lmc.Value.DoSomething()
Console.ReadLine()
End Sub
Sub DoRegistrations()
container.AddNewExtension(Of InterceptionExtension.Interception)()
container.RegisterType(Of IMainInterface, MainClass)()
container.Configure(Of Interception).SetDefaultInterceptorFor(Of IMainInterface)(New InterfaceInterceptor)
End Sub
End Module
It produces the following output:
Before lazy Resolve
Before making lazy function call
main class - Constructor
Constructor of Normal Sample Attribute
Create Handler
Constructor of Sample Call handler
main class do something...
Even though DoSomethingElse() is never called the cost of the handler creation is being added on all flows. Is there any way to avoid this? Any help is appreciated.
Thanks in advance!
SV

Implement an inherited function - VB.net

I've a form class, named EditCellForm that should implement the interface ICoordsRequester.
This interface requires the Focus() method to be implemented.
As long it's a form, my class already implements the Focus() method but the compiler cannot see it because it's in the Windows.Forms.Form superclass.
Any idea?
You can create a new Focus method using the Shadows keywords to implement the interface:
Class EditCellForm
Inherits Form
Implements ICoordsRequester
Shadows Sub Focus Implements ICoordsRequester.Focus
Mybase.Focus
End Sub
End Class
or rename your method
Class EditCellForm
Inherits Form
Implements ICoordsRequester
Sub NewFocus Implements ICoordsRequester.Focus
Focus()
End Sub
End Class
You can implement the method as usual, but just call it something different. Then in the method, just call the Focus event of the base class. If all it does is the same thing, I'd just make it private so that it's only accessible via the interface.
Public Class EditCellForm
Implements ICoordsRequester
Private Sub ICoordsRequester_Focus() Implements ICoordsRequester.Focus
Me.Focus()
End Sub
End Class