Results 1 to 7 of 7

Thread: Why does Visual Basic allow a primative types to be converted to Objects?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Angry Why does Visual Basic allow a primative types to be converted to Objects?

    What's up with this? Why does Visual Basic allow a primative types to be converted to Objects? I don't understand why this is allowed. Has it always been allowed in VB? For instance the first two code blocks would be illegal in Java. First being primative to refrence type Second being refrence to primative. But the third would be allowed.
    Code:
    public class X{
       public static void main(String[] args){
          
        int i = 7; 
        Test(i);
      }
      public static void Test(Object i){
        System.out.println(" The value of i is " + i);
      }
    }
    Code:
    public class Y{
       public static void main(String[] args){
          
        Integer i = new Integer(7); 
        Test(i);
      }
      public static void Test(int i){
        System.out.println(" The value of i is " + i);
      }
    }
    Allowed
    Code:
    public class Z{
       public static void main(String[] args){
          
        Integer i = new Integer(7); 
        Test(i);
      }
      public static void Test(Object i){
        System.out.println(" The value of i is " + i);
      }
    }
    Now Visual Basic....
    Code:
    Module Module1
    
    Public Function Add(x as Object, y as Object)
      Console.WrintLine(x,y)
    End Function  
    
    Public Sub Main()
     Dim x as Integer = 2 
     Dim y as Integer = 3
    
     Add(x,y)
    End Sub 
    
    End Module

  2. #2
    Junior Member
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    30
    Don't quote me on this but I think that all "primitives" in VB .NET are actually primitive objects not primitives in the true sense.
    I like the fact that I can declare an Integer then use the ToString method instead of the slower CStr() function.
    I think that Microsoft were considering the fact that VB has always been an easy-to-read language and having a line of code that reads:

    Dim i as Integer=5

    MsgBox(i.ToString)


    is easier to read than:

    Dim i as Integer=5

    Msgbox(CStr(i))
    <! Ozki !>

  3. #3
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Hmm. VB6 is based on COM, which uses OLE Variants for data types (which then you have OLE Variant String, OLE Variant Date, OLE Variant Pointer to Object etc.). I'd think the CLR would use a similiar method of cross-language data?
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I think it's because Visual Basic lacks a way to represent primative types using seperate classes(such as Java's wrapper classes) so they went with this approach instead.

  5. #5
    Lively Member
    Join Date
    Aug 1999
    Location
    Amsterdam
    Posts
    117
    Every class in .NET finally derives from Object, so everything is an object. And just because the integer extendse Object it's convertable, like all other classes that extend Object. That's normal Object Oriented behavior.

  6. #6

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Posted by gijsj
    That's normal Object Oriented behavior.
    Of course it's normal Object Oriented behavior. It's just seems weird comming from a Java standpoint. In Java there is at least some form of seperation between primative types and Object types and if one want's to represent a primative type as an Object then one would just create a wrapper object that corresponds to the primative type wishing to be represented as an Object.

  7. #7
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Does a true OO language (I don't think any exist outside of theory) even have primitive data types?

    In a language I just made up:
    Code:
    'normal OO
    String s = "hello";
    
    STDOUT.Print s.length;
    
    'Operate on literal object
    STDOUT.Print "hello".length;
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

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