Results 1 to 3 of 3

Thread: Console app without the Imports statement

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2003
    Location
    C:\Windows\Microsoft.NET\Framework
    Posts
    574

    Console app without the Imports statement

    I am practicing VB.NET and am still a neophyte in the order, though I've done VB from version 4 to 6 for many years now. I was wondering how in one of my programs (a console app) I forgot to include the Imports statement and the compiler did not complain.

    For instance,

    Code:
    Option Strict On
    
    
    Class zzz
    
    
    'Properties
    Dim j as integer
    Shared dim k as integer
    
    
    
    Shared Sub Main()
    
    
    dim a,b as zzz
    a = new zzz(10)
    a.abc(100)
    b = new zzz(20)
    b.abc(200)
    
    End Sub		'End of Main
    
    
    
    Sub New(i as integer)
    
    System.Console.WriteLine("in new {0} {1} {2}", i , j , k)
    j = i
    k = i
    System.Console.WriteLine("in new {0} {1} {2}", i , j , k)
    
    End Sub		'End of constructor for the zzz class
    
    
    
    
    Sub abc(p as integer)
    
    System.Console.WriteLine("in abc {0} {1}",  j , k)
    k = p
    j = p
    
    End Sub		'End of sub procedure abc
    
    
    
    End Class		'End of class zzz
    
    
    
    
    
    
    
    ' Guessed Output
    
    '10 0 0
    '10 10 10
    '10 10
    '20 0 100
    '20 20 20
    '20 20
    
    
    'Conclusion
    'Since the class member k is shared, it is the same accross all instances of the class zzz.

    Why is it that the compiler did not complain on not finding the Imports statement where I should have written Imports System since the Console class is a member of the System namespace. Besides, I was writing this in Notepad, so if at all the VS.NET IDE adds namespaces on its own, there was no such possibility in the Notepad environment I was writing code in. What's the secret?

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    It didn't complain because you completely declared the Namespace, Class, Method like this
    Code:
    System.Console.WriteLine
    However, if you had just written this line of code
    Code:
    Console.WriteLine
    It would have yelled, because you can't use the Console class without Importing (using in C#) the System namespace.

    It can be done either way
    VB Code:
    1. 'Complete declaration
    2. System.Console.WriteLine
    3.  
    4. 'Or Importing the namespace
    5. Imports System
    6.  
    7. Console.WriteLine
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2003
    Location
    C:\Windows\Microsoft.NET\Framework
    Posts
    574
    Silly me, that was common sense, but I was being careless. I did not write the code but I pasted it from somewhere and guessed the output (I only wrote the footnote to guess the output and the conclusion). So I did not look at the WriteLine function call to see it already had a fully qualified namespace prefixed.

    Thanks very much.

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