-
Mar 13th, 2025, 07:51 PM
#1
Thread Starter
Addicted Member
[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.
-
Mar 13th, 2025, 08:18 PM
#2
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
 
-
Mar 13th, 2025, 08:29 PM
#3
Thread Starter
Addicted Member
Re: Define Files In VB, like modules in VBA
 Originally Posted by Shaggy Hiker
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.
-
Mar 13th, 2025, 08:51 PM
#4
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.
-
Mar 13th, 2025, 08:52 PM
#5
Re: Define Files In VB, like modules in VBA
 Originally Posted by Shaggy Hiker
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
-
Mar 13th, 2025, 09:24 PM
#6
Re: Define Files In VB, like modules in VBA
 Originally Posted by jmcilhinney
I assume that you meant:
Code:
Public Class MyClass
Public Shared Sub Foo()
End Class
That’s what I was going to say
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 13th, 2025, 09:26 PM
#7
Thread Starter
Addicted Member
Re: Define Files In VB, like modules in VBA
 Originally Posted by jmcilhinney
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.
-
Mar 13th, 2025, 09:32 PM
#8
Re: Define Files In VB, like modules in VBA
 Originally Posted by FunkMonkey
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 13th, 2025, 09:33 PM
#9
Re: Define Files In VB, like modules in VBA
Just click Project (menu)-->Add module
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 13th, 2025, 09:48 PM
#10
Re: Define Files In VB, like modules in VBA
 Originally Posted by FunkMonkey
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?
-
Mar 13th, 2025, 09:48 PM
#11
Thread Starter
Addicted Member
Re: Define Files In VB, like modules in VBA
 Originally Posted by .paul.
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?
-
Mar 13th, 2025, 10:06 PM
#12
Re: Define Files In VB, like modules in VBA
I told you: the Project menu: the Add module menuitem
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 13th, 2025, 10:08 PM
#13
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 14th, 2025, 12:22 AM
#14
Re: Define Files In VB, like modules in VBA
 Originally Posted by FunkMonkey
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.
-
Mar 14th, 2025, 09:57 AM
#15
Thread Starter
Addicted Member
Re: Define Files In VB, like modules in VBA
 Originally Posted by jmcilhinney
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?
-
Mar 14th, 2025, 10:25 AM
#16
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…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 14th, 2025, 10:57 AM
#17
Thread Starter
Addicted Member
Re: Define Files In VB, like modules in VBA
 Originally Posted by .paul.
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!
-
Mar 14th, 2025, 03:04 PM
#18
Re: Define Files In VB, like modules in VBA
 Originally Posted by jmcilhinney
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
 
-
Mar 14th, 2025, 03:09 PM
#19
Re: Define Files In VB, like modules in VBA
 Originally Posted by FunkMonkey
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
 
-
Mar 14th, 2025, 10:26 PM
#20
Re: Define Files In VB, like modules in VBA
 Originally Posted by FunkMonkey
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|