Results 1 to 20 of 20

Thread: [RESOLVED] Define Files In VB, like modules in VBA

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    178

    Resolved [RESOLVED] Define Files In VB, like modules in VBA

    I'm used to VBA, which is quite different than VB. Are there definition files like *.h? I have not seen a file of this type defined in the project I created to work with, or even a module file. If Definition files are not used then where do all your defines for the project go?

    In VBA I have a rather large Define File that holds all my defines/enums/API prototypes, etc.

    Appreciate the input.
    Last edited by FunkMonkey; Mar 13th, 2025 at 07:57 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,792

    Re: Define Files In VB, like modules in VBA

    You can add modules to VB. A module is really just a class with all static members. Because people coming from VBA and VB6 would be used to modules, they were added to VB.NET. However, everything in VB.NET is in a class, so the module is just a class as well. You could do the same thing as a module by creating a static class, like so:
    Code:
    Public Class MyClass
        Public Sub Foo()
    End Class
    If you did that, then you could call Foo from anywhere with:

    MyClass.Foo()

    If you used a module, then you wouldn't need to decorate the name with MyClass, but it's effectively the same thing.

    However, you talk about "all your defines". That makes me a bit nervous. In VBA, and in some ways of writing VB6, global variables were fairly common. They are very rare in VB.NET, but that might not be what you mean with the word, "defines".
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    178

    Re: Define Files In VB, like modules in VBA

    Quote Originally Posted by Shaggy Hiker View Post
    You can add modules to VB. A module is really just a class with all static members. Because people coming from VBA and VB6 would be used to modules, they were added to VB.NET. However, everything in VB.NET is in a class, so the module is just a class as well. You could do the same thing as a module by creating a static class, like so:
    Code:
    Public Class MyClass
        Public Sub Foo()
    End Class
    If you did that, then you could call Foo from anywhere with:

    MyClass.Foo()

    If you used a module, then you wouldn't need to decorate the name with MyClass, but it's effectively the same thing.

    However, you talk about "all your defines". That makes me a bit nervous. In VBA, and in some ways of writing VB6, global variables were fairly common. They are very rare in VB.NET, but that might not be what you mean with the word, "defines".
    Thanks Shaggy.

    What I mean by "defines" are shown below. I need to have these global for various reasons, so I would like to have just a module without having to call a function.

    Code:
    #If VBA7 Then
        Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64-Bit versions of Excel
        Private Declare PtrSafe Function DispCallFunc Lib "OleAut32.dll" (ByVal pvInstance As LongPtr, ByVal FuncAddr As LongPtr, ByVal CallConvention As Integer, ByVal rtnType As VbVarType, ByVal FuncArgsCnt As Long, ByRef FuncArgTypes As Any, ByRef FuncArgVarAddresses As Any, ByRef FuncResult As Any) As Long
        Private Declare PtrSafe Function IsIconic Lib "user32.dll" (ByVal hWnd As Long) As Long
    #Else
        Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32-Bit versions of Excel
        Private Declare Function DispCallFunc Lib "OleAut32.dll" (ByVal pvInstance As LongPtr, ByVal FuncAddr As LongPtr, ByVal CallConvention As Integer, ByVal rtnType As VbVarType, ByVal FuncArgsCnt As Long, ByRef FuncArgTypes As Any, ByRef FuncArgVarAddresses As Any, ByRef FuncResult As Any) As Long
        Private Declare Function IsIconic Lib "user32.dll" (ByVal hWnd As Long) As Long
    #End If
    
    Public Const C_ERR_NO_ERROR = 0&
    Public Const C_ERR_OBJECT_VARIABLE_NOT_SET = 91&
    Public Const C_ERR_OBJECT_REQUIRED = 424&
    Public Const C_ERR_DOES_NOT_SUPPORT_PROPERTY = 438&
    Public Const C_ERR_APPLICATION_OR_OBJECT_ERROR = 1004&
    
    Public Enum ENUM_EXCEL_SHEETS
        eEXCEL_USER_TABS = 30
        eEXCEL_SETTINGS
        eEXCEL_CAPA
        eEXCEL_MAIN
        eEXCEL_OPTIONS
        eEXCEL_CYBER_SEC
    '    eEXCEL_RISK_SUMMARY
    End Enum
    Thanks for the assist.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,066

    Re: Define Files In VB, like modules in VBA

    In VB.NET, everything is or goes in a type. Types include classes, structures, enumerations and modules (which are classes under the hood). Generally speaking, each type should go in its own code file, named after the type. I tend to bend that rule for enumerations, creating one file named Enumerations.vb and declaring every Enum in there. I do that because those types are usually simple and small, so having them together is still easy to read.

    In your case, you would either create a file named ENUM_EXCEL_SHEETS.vb and put your Enum definition in there or do as I do and create a file named Enumerations.vb (or similar) and put that and any other Enum definitions in there. Your constants should probably be declared in a class named Constants and in a file named Constants.vb. Constants behave as Shared by default so you would then access them via that class, e.g. Constants.C_ERR_NO_ERROR. If you want to be able to use them without qualifying with a type name, use a module instead of a class. Not having to use a qualifying type name to access members is a feature of VB modules. As for the API declarations, it is convention to either declare them in the types where they are needed, if that's just one place, or use a class named NativeMethods in a file named NativeMethods.vb. Again, you then access then by name and qualify with the type, e.g. NativeMethods.Sleep(). Again, you can use a module instead of a class if you want to be able to access them without qualifying the name.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,066

    Re: Define Files In VB, like modules in VBA

    Quote Originally Posted by Shaggy Hiker View Post
    You can add modules to VB. A module is really just a class with all static members. Because people coming from VBA and VB6 would be used to modules, they were added to VB.NET. However, everything in VB.NET is in a class, so the module is just a class as well. You could do the same thing as a module by creating a static class, like so:
    Code:
    Public Class MyClass
        Public Sub Foo()
    End Class
    If you did that, then you could call Foo from anywhere with:

    MyClass.Foo()

    If you used a module, then you wouldn't need to decorate the name with MyClass, but it's effectively the same thing.

    However, you talk about "all your defines". That makes me a bit nervous. In VBA, and in some ways of writing VB6, global variables were fairly common. They are very rare in VB.NET, but that might not be what you mean with the word, "defines".
    I assume that you meant:
    Code:
    Public Class MyClass
        Public Shared Sub Foo()
    End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,978

    Re: Define Files In VB, like modules in VBA

    Quote Originally Posted by jmcilhinney View Post
    I assume that you meant:
    Code:
    Public Class MyClass
        Public Shared Sub Foo()
    End Class
    That’s what I was going to say

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    178

    Re: Define Files In VB, like modules in VBA

    Quote Originally Posted by jmcilhinney View Post
    In VB.NET, everything is or goes in a type. Types include classes, structures, enumerations and modules (which are classes under the hood). Generally speaking, each type should go in its own code file, named after the type. I tend to bend that rule for enumerations, creating one file named Enumerations.vb and declaring every Enum in there. I do that because those types are usually simple and small, so having them together is still easy to read.

    In your case, you would either create a file named ENUM_EXCEL_SHEETS.vb and put your Enum definition in there or do as I do and create a file named Enumerations.vb (or similar) and put that and any other Enum definitions in there. Your constants should probably be declared in a class named Constants and in a file named Constants.vb. Constants behave as Shared by default so you would then access them via that class, e.g. Constants.C_ERR_NO_ERROR. If you want to be able to use them without qualifying with a type name, use a module instead of a class. Not having to use a qualifying type name to access members is a feature of VB modules. As for the API declarations, it is convention to either declare them in the types where they are needed, if that's just one place, or use a class named NativeMethods in a file named NativeMethods.vb. Again, you then access then by name and qualify with the type, e.g. NativeMethods.Sleep(). Again, you can use a module instead of a class if you want to be able to access them without qualifying the name.
    Thanks! Boy this is strange for me. I worked with Pascal, C/C++, VBA and all of them allow for defines. I guess this is going to be painful.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,978

    Re: Define Files In VB, like modules in VBA

    Quote Originally Posted by FunkMonkey View Post
    Thanks! Boy this is strange for me. I worked with Pascal, C/C++, VBA and all of them allow for defines. I guess this is going to be painful.
    Those API and Structure, enum declarations, and any global constants or variables, you can declare them in a Module as i think both Shaggy and jmcilhinney told you

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,978

    Re: Define Files In VB, like modules in VBA

    Just click Project (menu)-->Add module

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,066

    Re: Define Files In VB, like modules in VBA

    Quote Originally Posted by FunkMonkey View Post
    I guess this is going to be painful.
    Why? The code is basically the same. The only difference is that the file has a different extension and the declarations are within a type, where a type is just two lines of code. What exactly is painful about that?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    178

    Re: Define Files In VB, like modules in VBA

    Quote Originally Posted by .paul. View Post
    Those API and Structure, enum declarations, and any global constants or variables, you can declare them in a Module as i think both Shaggy and jmcilhinney told you
    Well I'm working with VS 2022 and have looked at several videos, which use older versions of VB, and by the video directions there are no "modules" shown when I select "add new item". There are many other things, including a text file, but no module.

    So where in 2022 do I find a module?

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,978

    Re: Define Files In VB, like modules in VBA

    I told you: the Project menu: the Add module menuitem

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,978

    Re: Define Files In VB, like modules in VBA

    A lot of the people putting videos on YouTube don’t know as much as they profess.
    Ask in this forum, or use (the formally known as) MSDN for better help

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,066

    Re: Define Files In VB, like modules in VBA

    Quote Originally Posted by FunkMonkey View Post
    Well I'm working with VS 2022 and have looked at several videos, which use older versions of VB, and by the video directions there are no "modules" shown when I select "add new item". There are many other things, including a text file, but no module.

    So where in 2022 do I find a module?
    Modules have existed since the very first VB.NET in 2002 and they have always been found in the same multiple places:

    - Right-click the project in the Solution Explorer and select Add -> Module
    - On the Project menu, select Add Module
    - Open the Add New Item dialogue any way you like and select Module from either the Common Items section or its Code subsection

    If you're not seeing these options then you are doing or have done something wrong, but what that may be is anyone's guess. It might just be that VS is corrupted somehow and requires a repair. It sounds to me like you should spend some time generally getting to know the IDE. Explore it fully to see what's available instead of getting annoyed when you don't know where a specific feature that you want is located. Every developer should have looked through the whole menu system and the Options dialogue before trying to do anything in VS, or any new software for that matter.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    178

    Re: Define Files In VB, like modules in VBA

    Quote Originally Posted by jmcilhinney View Post
    Modules have existed since the very first VB.NET in 2002 and they have always been found in the same multiple places:

    - Right-click the project in the Solution Explorer and select Add -> Module
    - On the Project menu, select Add Module
    - Open the Add New Item dialogue any way you like and select Module from either the Common Items section or its Code subsection

    If you're not seeing these options then you are doing or have done something wrong, but what that may be is anyone's guess. It might just be that VS is corrupted somehow and requires a repair. It sounds to me like you should spend some time generally getting to know the IDE. Explore it fully to see what's available instead of getting annoyed when you don't know where a specific feature that you want is located. Every developer should have looked through the whole menu system and the Options dialogue before trying to do anything in VS, or any new software for that matter.
    Thanks guys. Yes, but in some cases, like now, if it is not there to begin with how would one know? I have looked in all the menus, project/Solution Explorer Right Click, and there is nothing about Add Module. IT just installed VS about a week ago, so don't think it's an install issue as far as I know. Is there a module that needed to be selected during the install?

    Thanks again, but I'll get with IT. Though they were not much help when asked about things during the install.

    P.S. Can I just generate one by hand? Module <NAME> end module?

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,978

    Re: Define Files In VB, like modules in VBA

    Try the main vs menus. Open your project, click the Project menu in the main IDE menus, and you’ll find Add module there…

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    178

    Re: Define Files In VB, like modules in VBA

    Quote Originally Posted by .paul. View Post
    Try the main vs menus. Open your project, click the Project menu in the main IDE menus, and you’ll find Add module there…
    Thanks Paul. Looked there but it flew right by me. THANKS again!

  18. #18
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,792

    Re: Define Files In VB, like modules in VBA

    Quote Originally Posted by jmcilhinney View Post
    I assume that you meant:
    Code:
    Public Class MyClass
        Public Shared Sub Foo()
    End Class
    Yes, I did. Too many things going on I suppose.
    My usual boring signature: Nothing

  19. #19
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,792

    Re: Define Files In VB, like modules in VBA

    Quote Originally Posted by FunkMonkey View Post
    Thanks Paul. Looked there but it flew right by me. THANKS again!
    One thing to note is that all files in VB.NET are just text. There's nothing special about the different things you can add. You could add a text file in each case and it would work...if you put the right stuff into it. It's that last part that is the reason those different file types even exist. Adding a form completely by hand in a text file is tedious and error prone, so it is always easier just to add a form. The same is true to a MUCH lesser extent for something like a module, class, or structure. It's all just text, the template files give you a bit of structure without you needing to remember it.
    My usual boring signature: Nothing

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,066

    Re: Define Files In VB, like modules in VBA

    Quote Originally Posted by FunkMonkey View Post
    Thanks Paul. Looked there but it flew right by me. THANKS again!
    So, it is present in one place that you claimed it wasn't. What about the other two I mentioned in post #14? Did those just fly right by you too? If they didn't and they really are not present then there's still something wrong with your VS installation, but I'm guessing that that's not the case.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width