Go Back   CodingForums.com > :: Server side development > ASP.NET

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-21-2013, 02:35 PM   PM User | #1
Zaid
New Coder

 
Join Date: Sep 2012
Location: Mumbai
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Zaid is an unknown quantity at this point
Question Strange

I have read that VB.NET is a fully Object-Oriented Compatible language....but to my amazement I have found out this code in a Console Application....

Code:
Imports System.Console
   Module Module1
      Sub Main()
          WriteLine("Hello from Visual Basic")
          ReadLine()
      End Sub
   End Module
Now how can we access WriteLine() and ReadLine() without creating the object of Console class(as we have only IMPORTED it, NOT INHERITED it), because in Java even if u import a class in a package(namespace here) u have to still instantiate(create object of) that class to access its members(data and methods).......I am still a beginner to the .NET world(as i am still learning Console Application )

Last edited by Zaid; 01-21-2013 at 02:36 PM.. Reason: Forgot the [CODE]tag
Zaid is offline   Reply With Quote
Old 01-21-2013, 08:24 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
in Java even if u import a class in a package(namespace here) u have to still instantiate(create object of) that class to access its members
Not true.

If the members are *STATIC* members, you SHOULD NOT use an instantiated object to access them. *AT MOST* you would use the class name as a prefix.

In other words:
Code:
// This is simplified Java code:
class Foo 
{
    static void Zap( String s ) { ... }
}
...
// then you should *NEVER* do
Foo f = new Foo( );
f.Zap("xyz");

// yes, it works, but it is VERY VERY misleading to do so!  
// you should instead use
Foo.Zap("xyz");
The *SAME THING* applies with VB.NET (and with C# and with any other .NET language!).

You can *EITHER* do
Code:
System.Console.WriteLine("a message")
invoking the STATIC method WriteLine *OR* you can do
Code:
Imports System.Console
WriteLine("a message")
You need to keep the distinction between static and dynamic methods and members straight.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-22-2013, 04:39 AM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Java can do this as well, but you should use it *very* sparingly. Keeping (and spoofing) the Foo:
PHP Code:
package foo;

public class 
Fou
{
    public static 
void Zap(String s)
    {
        
System.out.println(s);
    }
}

// And used:
package foo;

import static foo.Fou.Zap;

public class 
Lu
{
    public static 
void main(String[] argv)
    {
        
Zap("doFou");
    }

I really would not recommend this with Java, and especially with custom classes. But for shortcut (ie: lazy ) usage, you can use it against the system.out for example, so you can just type in out.println. Unlike C#/VB, you can't get to the println this way as the PrintStream is an instance object and not a class.
BTW, default functionality in languages like Java already use an implicit static import for the entire java.lang package. Without it you'd need to import or instantiate all the base objects including object, and nobody wants to do new java.lang.Object();.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 01-28-2013, 02:50 PM   PM User | #4
Zaid
New Coder

 
Join Date: Sep 2012
Location: Mumbai
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Zaid is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
so you can just type in out.println.
I use the code
Code:
import java.lang.System;
class Demo
{
	public static void main(String args[])
	{
		out.println("Hello");
	}
}
but it is not working.....so u said wrong....and plz don't try to confuse me(with all those PHP's and irrelevant ****) when u seriously don't know the answer...
Zaid is offline   Reply With Quote
Old 01-28-2013, 02:55 PM   PM User | #5
Zaid
New Coder

 
Join Date: Sep 2012
Location: Mumbai
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Zaid is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
If the members are *STATIC* members, you SHOULD NOT use an instantiated object to access them. *AT MOST* you would use the class name as a prefix.
Didn't buy it....
Zaid is offline   Reply With Quote
Old 01-28-2013, 03:50 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by Zaid View Post
I use the code
Code:
import java.lang.System;
class Demo
{
	public static void main(String args[])
	{
		out.println("Hello");
	}
}
but it is not working.....so u said wrong....and plz don't try to confuse me(with all those PHP's and irrelevant ****) when u seriously don't know the answer...
I beg your pardon? Perhaps you are looking at my signature.
You are obviously not reading what I said:
Quote:
But for shortcut (ie: lazy ) usage, you can use it against the system.out for example, so you can just type in out.println. Unlike C#/VB, you can't get to the println this way as the PrintStream is an instance object and not a class.
You *must* have an instance of Java's PrintStream in order to invoke the println. The System.out is a static instance of PrintStream.
PHP Code:
import static java.lang.System.out// You cannot statically import System.
public class myclass
{
    public static 
void main(String argv[])
    {
        
out.println("Hello world");
    }

Quote:
Originally Posted by Zaid View Post
Didn't buy it....
Unlike Java, C# uses a static console and static WriteLine. You can import the System.Console directly and invoke the methods without referring to the class.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php

Last edited by Fou-Lu; 01-28-2013 at 04:02 PM..
Fou-Lu is offline   Reply With Quote
Old 01-28-2013, 09:27 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by Zaid View Post
Didn't buy it....
If you don't understand why using an *instance* of a class is a VERY BAD way to invoke a static method, then you have not understood Java completely, at all.

Perhaps you should study Java fundamentals more.

Either that or simply stop trying to compare Java with VB.NET.

There are similarities, but they are *NOT* the same, as FouLu tried to show you.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
oop, vb.net

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:43 PM.


Advertisement
Log in to turn off these ads.