Why does Console namespace need to be imported into a Console application project - vb.net

If I create a new Console application then this compiles ok:
Module Module1
Sub Main()
Console.ReadKey()
End Sub
End Module
An alternative is this:
Imports System.Console
Module Module1
Sub Main()
ReadKey()
End Sub
End Module
What I don't now understand is, as this is a console application, why aren't these projects created in such a way that the following will compile?:
Module Module1
Sub Main()
ReadKey()
End Sub
End Module
Put another way: if it is a Console app why do I need to import the Console namespace?
Is this the same behavior as when using a winForms application I say Me.someControl? Although in that situation I can leave of the Me and it will still compile without importing a forms namespace.
EDIT
Imports System.Console
Module Module1
Sub Main()
ReadKey()
Console.ReadKey() '<<adding namespace seems a bit pointless as I still need to specify Console
End Sub
Function ReadKey() As Boolean
Return True
End Function
End Module
FURTHER EDIT
Hans has suggested further behaviour and to replicate this I have the following:
I've added a new ClassLibrary1 to the same Solution like so...
In the Console ConsoleSandpit I've got the following, with a reference to ClassLibrary1 like so...
...only problem is I'm expecting a compilation error from the above but it seems to be running ok!

System.Console is not a namespace. ReadKey is a shared method in the Console class which is in the System namespace
So, the breakdown of the following statement is:
System.Console.ReadKey()
System - Namespace
Console - Class
ReadKey - Shared method
So, when you call Console.ReadKey() (without specifying the namespace), you would normally expect to have to import System at the top of your code file. However, that is unnecessary because in VB.NET projects, you can specify a list of default namespaces that are automatically imported for all files in the project. You can modify these using the check-list of default namespaces in your project properties designer. System, among others, is automatically imported as a default namespace with all of the project templates.
As you demonstrated in your question, it is possible in VB.NET to import a class or module name. The Imports statement is not limited to just working with namespaces. However, as far as I know, there is no way to import a class name with the default namespaces option for the project (or at least not with the project properties designer, anyway).
If System.Console was a namespace, I agree that you'd expect the project template for console projects to import that namespace by default. However, since it's not a namespace at all, you wouldn't expect it to do that :)

1) You can do plenty of useful things, over many functions, inside a console application, without wanting or needing to actually interact with the Console.
2) I could easily envisage wanting to write a function called ReadKey which puts out a small prompt for one of two or three keys to be pressed - based on the user input, if its one of those keys, return it. Otherwise, indicate the error and re-prompt. Why should I have to pick a different name for this function, because my own namespace has been automatically polluted with the methods from Console?
Namespaces exist to help to separate and organize functionality. You're given options (such as the Imports statement) to hide these separations, where it makes sense for you within your application - but I wouldn't want this forced on all users.

Add a class library project to your solution and paste this code:
Module FooBar
Sub ReadKey(ByVal wait As Integer)
End Sub
End Module
And observe how your original code now suddenly fails to compile. With a seriously obtuse error message as well: "Argument not specified for parameter 'wait'". You can lose hours of your life trying to figure out why Console.ReadKey() wants a wait argument.

Related

Can I implement an interface containing public member variable of a specific UserForm?

I've succesfully created an UserForm, that implements an Interface which enables showing a warning tooltip (https://youtu.be/8dvWjXgVpxk). It is working just fine when all user forms, modules and classes are located in one common project, but now I'd like to take the Interface and store it in another project, which I could then reuse in many different projects by creating reference to it (Tools -> References). Unfortunately, when I transfer the Interface and an UserForm that this Interface uses as a member variable, I'm getting an Compile error: "Private object modules cannot be used in public object modules as parameters or return types for public procedures, as public data members, or as fields of public user defined types."
error message
I'm struggling with this few days now, and cannot find a propper approach.
The simplified code structure:
Main project:
module: UFManager:
Sub CATMain()
UFThatHasTooltip.Show
End Sub
user form: UFThatHasTooltip:
Implements ICanShowTooltip
Private Sub UserForm_Click()
Debug.Print "click!"
End Sub
External project:
class/interface: ICanShowTooltip:
Public myTooltip As UFTooltip
user form: UFTooltip
'empty
I think the main issue is caused by the fact, that the userform UFTooltip is considered Private and thus cannot be used in any form by anything that works as Public. I know how to solve this issue, when instead of user form I'd like to use a class as a member variable of an interface (by changing 'Instancing' property of the class from '1 - Private' to '2 - PublicNotCreatable'), but I cannot find a property of an user form that changes its behavior to 'Public'.
PS. I know that the userform UFThatHasTooltip is missing some Properties definitions demanded by the use of an Interface, but the compiler doesn't even goes that far to care about it, so I ignored them for now.
EDIT:
I've managed to export .frm file of UFTooltip, edit the attribute VB_Exposed and set its value to True. That helped with the initial issue, so I developed the code further and placed it on a repo:
https://github.com/hwnd-git/VBA-Interface-Sandbox
Now I'd like to know if there is a way to achieve this without the need of exporting the user form and editing its .frm file.

stimulsoft report add my function with vb.net

we can add our function with this way
https://www.youtube.com/watch?v=TEYuQmia9IY
but it in c#
i want the way in vb.net
the problem in using namspacing in vb.net
thanks
In the video posted there are some critical steps missing. It seems that they are given for granted but without it you could not achieve a successful reference to your code from the Report Designer.
First: To clarify the problem between VB.NET and C#. If you want to use VB as scripting language for your report you need to set it in the Report Properties. You will find the ScriptLanguage property in the same property grid for the report where they set the reference to your application. Changing this property is not required because this property refers to the code written inside the report not the code in which you have written your app.
Second: In your VB.NET app your code that defines the function should be as this
Namespace MyFunc
Public Class Main
Public Shared Sub ShowMessage(text As String)
MessageBox.Show(text)
End Sub
End Class
End Namespace
Usually in VB.NET you don't define explicitily a namespace (everything is inside the main namespace defined in the project properties) but if you want a separate namespace there is nothing to stop you.
Third: In the Report Code Page you write
Imports WindowsFormApplication2.MyFunc
Fourth: After completing the design of your report remember to save it in the same folder where the application is created. (Usually Project\BIN\DEBUG) Otherwise the reference to your executable will fail.
Fifth: The Stimulsoft assemblies are installed in the GAC, but to be sure that they are found when you run the report don't forget to set their references to Copy Local = True when you run your app inside Visual Studio

execute other class before executing the main windows form application class

I've been given a windows form application written in VB. For some reasons I will need to execute the second class before the form application in the first class. The form class has to be the first class in the file. I can't simply inherit the second class and call the functions, because it has already used up the only allowable inheritance. I did some research and found there is something called main procedure that determines which codes executes first? It is automatically generated for any windows form application, but I simply can't find that file. Any thoughts on that? or any other ways that I do this?
Follow Start VB.NET GUI app using Sub Main or form startup object? for better alternatives.
But if you really need to start with Main(), follow these steps.
Open application settings.
Uncheck "Enable application framework"
Set startup object to "Sub Main"
Then add a new source file (.vb) and include Main() in it
Module MainModule
Sub Main()
'Your code here
End Sub
End Module

A strange behaviour shared variable issue in VB.net

When I was using VB.net , I came across a very strange behaviour, I created a simple test WPF project to reproduce it. here is the details. I have a very simple class, which when an instance is created, the class will create a test.txt file
Public Class Test
Public Sub New()
Using writer As New System.IO.StreamWriter("test.txt")
writer.Write("test")
End Using
End Sub
End Class
Then in the Application.xaml.vb
Class Application
' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
' can be handled in this file.
Shared tt As New Test()
End Class
I simply define a shared variable. my expectation of this are, when I start the application, the variable will be initiated, and a "test.txt" file will be created.
If the Configuration is "Debug" , everything is fine.
If the Configuration is "release", When I start press F5 in Visual Studio 2010, everything is fine as well, it worked as expected, file had been create
But When I start it without debug, press (Ctrl+F5), the variable had not been initiated, file had not been created as I expected.
I am not fully understand why this happen, Can anyone help me out?
Thanks and regards
Is this shared variable accessed somewhere? It could been removed due to compiler optimization. Try to add some code that uses the variable.

MyProject.MyClass - vb.NET custom controls

In a Visual Basic project, I created a homemade TabControl in order to fix a visual bug. The control works properly, however whenever I modify the form using my tab, Visual Studio adds MyProject in front of the control in its declaration:
Me.tabMenu = New MyProject.MyClass 'Gives a BC30002 compile error
If I remove the MyProject., the project compiles properly.
MyClass is in a separate file MyClass.vb and looks mostly like this:
Public Class MyClass
Inherits System.Windows.Forms.TabControl
Public Sub New()
InitializeComponent()
MyBase.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed
End Sub
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
//OnDrawItem code
End Sub
Private Sub My_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
//My_DrawItem code
End Sub
End Class
I tried removing the file and adding it again, copy and pasting the class inside MyForm.designer.vb, adding MyProject. to the class name, but nothing stopped Visual Studio from adding this so-hated MyProject.
Edit regarding this answer:
I understand the thing about the namespace, however my problem is mostly that the compiler does not recognize the class with the project name appended but still adds it everytime.
What is the actual compile error you are getting? Is it possable that the VB compiler is interpreting MyProject as something other than a namespace identifier? You could also try changing the default namespace for the project, then see what it does, it might give you a hint as to what the actual problem is.
You could also try changing the offending line to
Me.tabMenu = New Global.MyProject.MyClass
then let us know what the results are.
I've seen this before when you have a public module with the same name as your default namespace (project name). If that's the case, either rename the module or the default namespace and the problem should go away,.
By default, Visual Basic .NET assigned a default namespace to your project. (I believe the default is, in fact, MyProject.)
This is what's being prepended, and it's being done to explicitly identify your class in the designer.
No matter what your default namespace is for your project, the WinForms designer will add the namespace name to the .designer.vb file.
To change the default namespace, go to your project properties; it should appear on the first tab.
Also, generally, don't modify the .designer.vb files if you can avoid it. Those files get completely blown away and rebuilt by Visual Studio often, so your changes will more likely than not be eliminated.