Debugging with my own custom Class Libraries - vb.net

I want my class library conditionally compiled so that it is in debugging mode when my project is, and is not when my project isn't.
For example, I have this Module in my class library:
Module MyDebug
<Conditional("DEBUG")>
Sub print(ByVal msg As String)
Debug.Print(Now.ToString("yyyy-MM-dd HH:mm:ss.fff") & " " & msg)
End Sub
<Conditional("DEBUG")>
Sub debugEnd(Byval bool As Boolean)
Environment.Exit(0)
End Sub
End Module
When I debug my project which references this library it run any of these when they are called.
I've tried searching online, but I haven't found anything that helps since I can only find things to do with debugging the actually class library, while I only want it to send these conditionally compiled statements while I'm debugging my project.
However, by experimenting around a bit, I've found that if in the class library I go to:
'My Project' -> the 'Compile' tab -> 'Advanced Compile Options'
and then tick 'Define DEBUG constant' (and then build the library), the project does run the debug statements when called.
However, I'm not entirely sure of the behaviour of 'Define DEBUG constant' in the class library. Does it define DEBUG if and only if my project is in debug mode?
If not, then is there a simple way to acheive what I aim to do? (I don't want to have to tick/untick the checkbox in the class library each time I switch between debug and release in my project, and my class library is referenced by more than one project, anyway)
Note that in the project I'm referencing the .dll in the Bin -> Release folder of my class library which I hope is the right way to reference it.
In addition, I would like to ask about how VB acheives this with the Debug class, because it is also imported with a reference like any other class library, and works in the way I would like the above to — surely I could do the same?

If you keep the class library project in the same solution as your main project it will use the same configuration (Debug or Release) as every other project in that solution. This means that you won't have to manually check/uncheck Define DEBUG constant as it will not be defined anyway if you have set the configuration to Release.
Even if your projects are not in the same folder you may still add your class library project to your solution. Here's how to do it:
Right-click your solution in the Solution Explorer and goto Add > Existing Project.
In the file browsing dialog that opens, locate the .vbproj-file of your class library and click OK.
Now when you change compilation configuration it should be reflected over the entire solution and your class library shall only have its DEBUG constant defined if you set the configuration to Debug.
If you cannot see your solution in the Solution Explorer:
In Visual Studio, goto Tools > Options > Projects and Solutions > General.
Check Always show solution and press OK.
For the above to work you must also change the way you reference your class library. The way you are currently doing it is correct, but it won't work in this case as then you'll only be referencing the Release version of your dll.
Start by removing your current reference to your class library.
Open the Add Reference dialog and go to the Projects tab.
Select your class library's project and press OK.
This should now reference the class library's output from the solution's current compilation configuration.

Related

Disable default instance of forms in VB.NET

Following this answer. Can I disable automatic instantiation? I want VS to show the error:
Reference to a non-shared member requires an object reference
Update:
When I originally wrote this answer, it was based on empirical evidence from the .vbproj file and the VB IDE and I had no reference material. Since then I have come across the article "Simplify Common Tasks by Customizing the My Namespace" published in the July 2005 MSDN Magazine (online version; full magazine edition download
This article shows to control the generation of the various My Namespace features via compilation constants. In particular it shows how to overide the MyType tag stored in the .vbproj file.
This is accomplished by setting the "Custom Constants" accessible by going to Project Properties->Compile Tab and clicking on the "Advanced Compile Options" button.
To override then MyType tag, set the custom constant _MyType.
If you want to include specific My Namespace items, you can set the pertinent constant(s) shown in the following table from the above referenced article.
The default form instances are part of the "My.Forms" member. Note that in order to prevent "My.Forms"
from being generated, set _MyType to either "Empty" or "Custom". You do not need to explicitly include _MYFORMS="False".
To use the additional constants defined in Table 5, set _MyType="Custom".
To include all the standard My Namespace items except for My.Forms, use the following constants:
_MyType="Custom", _MYAPPLICATIONTYPE="Windows", _MYCOMPUTERTYPE="Windows", _MYUSERTYPE="Windows", _MYWEBSERVICES="True"
End Update
The features enabled in Visual Studio are initialized based on the initial project template you select when creating a project. Normally, one would select the "Windows Forms App(.Net Framework) Visual Basic" template as shown below.
(Note: All images are based on VS 2017 Version 15.8.7)
This will load up a VB project environment that includes a lot of automatic code generation that supports the VB Application Framework including the "Default Form Instance" feature via additions to the My Namespace (see: My.Internals: Examining the Visual Basic My Feature. Now you could try to disable this framework via the Project Properties->Appliction Tab->Enable Application Framework checkbox, but all that does is require you to provide you own "Sub Main" implementation.
To create a project that free of all the My Namespace code generation including the the default form instances, you need to start with the "Empty Project (.Net Framework)" template.
This will create a bare-bones VB project. If you add a WinForm (Project Menu->Add Window Form) and go to the code-view and add the following:
Class Demo_NoDefaultForms
Sub ErrorOnForm1Reference()
Form1.Text = "ain't gonna work"
End Sub
End Class
You will see that the default Form1 instance is not supported.
Furthermore, all the "Application Framework" stuff in the project properties is disabled.
The project type My Namespace information is like all project information is stored in the projName.vbproj file and is stored in the <MyType> tag. Although I have never found any official documentation on this tag, the following summarizes my observations from creating various project types.
MyType Tag Value Project Type
----------------------------- ------------------------------------------------
WindowsForms Normal Windows Form App w/ Application Framework enabled
WindowsFormsWithCustomSubMain Normal Windows Form App w/ Application Framework disabled
Console Console App w/ Application Framework enabled
Custom WPF Application
Empty No My Namespace additions
Starting with the "Empty Project (.Net Framework)" template would be tedious as you would need to perform a lot of boiler-plate setup and declare a "Sub Main" each time. I recommend that you create a base project with all your own customizations and then export the project a new template (see: How to: Create project templates.

Generate manifest for regfree COM from a VB project

I am trying to generate a manifest for a set of COM objects that are being used in our code as registration-free COM.
For that purpose I created a VB project in VS 2010 and added the COM objects DLLs as dependencies. The problem is I do not see them listed as dependencies in the generated manifest. Am I missing something simple like a project setting to generate the proper manifest listing the dependencies?
You left out too many details and the VB.NET IDE hides too much to do this right. It is not automatic.
Starting point is to force it to show more. Locate the "Show All Files" toolbar button in the Solution Explorer window (use the tooltip to find it back). Click it and you now get the "References" node added to the project.
Open it and locate the COM component that you added to the project. Select it and look at the Properties window. Set the "Isolated" property to True. Rebuild the project. Be sure to look in the Output window for any warnings. And to look at the .manifest file on disk instead of the one the IDE shows you if you added a manifest to your project yourself.

VB NET class is recognized in VS, but I get a 'Type is Undefined' error on build

I am working on a VB NET project and had the strangest thing happen.
I created a class file(just like a dozen or so I have already created). I wrote in the code to access it. The autocomplete found the class, filled it in and colored it blue, just as it should.
But, when I run the app, I get a type is undefined error.
There is nothing in the class yet. And there is really no code to post..it is as straight forward as I described.
I tried restarting VS; Deleting and recreating the class; Deleting the class and creating a new one with a different name.
Is there something in the VB NET configuration I can check to see if it is not being added somewhere?
lee
Ok. I found the problem. I have 2 projects in one solution. They both share some classes that were trying to use my new classes. When I hit F5, both projects are compiled, and since I hadn't shared the classes with the second project, it errored.
So, now my question is changed; How do I specify to only build the specified Startup Project when debugging?
lee
Right click your solution -> properties -> configuration properties and untick everything you don't want to build.
And for your original question, check if build action on that files that don't compile is set to 'compile'

Calling a dll function

I am converting from vb5 and have a dll (now converted to VB.NET) which I use for security checks (mysecurity.dll). It contains the class cSecurityCheck and has a few interfaces, as shown in the VB5 code below, which is how I called it in VB5 from my app. I have copied the VB.NET compiled mysecurity.dll to my project's bin directory and added it as a reference to my VB.NET project(project\bin\mysecurity.dll) but in the project calling module (code below) it doesn't recognise CSecurityCheck, saying it has not been defined. How do I call the dll functions?
This dll is used by all my programs and when I finally get to distribute them to a client how do I ensure all the installed executables can look at the one single copy of the dll, which contains the registered user's name?
Dim s As New CSecurityCheck
s.AppPath = Application.StartupPath()
If Not s.DateIsValidated Then
s = Nothing
End
End If
mRegisteredUser = s.RegisteredUser
mDaysRemaining = s.DaysRemaining
s = Nothing
Adding a COM dll to your project automatically creates a namespace for the types in the DLL. Add Imports TheVb5ProjectName at the top of the file.
If you don't know what that name might be (it depends on VB5 project settings) then click the "Show All Files" icon in the Solution Explorer window, open the References node, locate the imported reference. Right-click it and click View in Object Browser.

Create Resource Icons Dll

i want to create resource dll in Vb.net mean like SHELL32.Dll
i want to store my icons and pngs for my custom file extensions.
Thanks..........
This is not supported by the IDE. These resources are not the kind that you're familiar with, they are unmanaged resources. The VB.NET compiler does support the option, /win32resource, you'll however have to run it by hand. You'll also need to run the rc.exe tool by hand to create the .res file.
Using VB.NET is just not the best way to get this done. It is natively supported by the C++ IDE.
File + New + Project
Select Visual C++, Win32, Win32 Project
Next, select DLL
Right-click the Resource Files folder in the Solution Explorer window
Add + Resource, Import and select the icon
Repeat as necessary for every icon you want to add
Build + Build.
You can use View + Other Windows + Resource View to edit the content. One thing you almost certainly want to do is change the ID of the icon from a symbol to a number. That's the number you'll use in the registry entry.
Cant you create a new class library add all of the icons and then change the Resources class to have a public modifier?
EDIT: the resources class is public
The link below works for setting the association: (based on comment feedback)
http://mel-green.com/2009/04/c-set-file-type-association/
You have to create blank DLL project with just added icons to project (not resources).
Check http://msdn.microsoft.com/en-us/library/aa984332(v=vs.71).aspx
VentyCZ