user1970090
user1970090

Reputation: 235

Using My namespace in a module

I tried to use MY namespace in a module in VB.NET , but it says "My isn't declared ..." Using MY namespace would save a lot of time , but can I use it in a module ?

Imports System.Windows.Forms

Public Module Entry_Module
    Sub Main()
        My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\Currentversion\Run\", "MyApp", application.executeablepath, RegistryValueKind.String)
    End Sub
End Module

I'm adding my application to startup , the application i have has no Form but modules instead . So How can I use my namespace in this module ?

Upvotes: 0

Views: 668

Answers (1)

SysDragon
SysDragon

Reputation: 9888

You have to use the My namespace inside a Method in order to be available, I think that is your problem. See:

Public Class MyClass
    My.<Does not work>

    Public Sub MyMethod()
        My.<Correct>
    End Sub
End Class

Anyway, if you want to use it anywhere you can create a new class in order to retrieve the information you want:

Public Class MyNS
    Public Shared Function CurrentDirectory() As String
        Return My.Computer.FileSystem.CurrentDirectory()
    End Function

    'etc
End Class

And then use it everywhere calling the class method: MyNS.CurrentDirectory()

Extra information:
My namespace and project types.

Upvotes: 2

Related Questions