PDA

Click to See Complete Forum and Search --> : Why does Visual Basic allow a primative types to be converted to Objects?


Dillinger4
Jan 29th, 2002, 07:51 PM
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.

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);
}
}


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

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.... :confused:

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

Ozki1976
Jan 30th, 2002, 04:09 AM
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))

JoshT
Jan 30th, 2002, 11:37 AM
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?

Dillinger4
Jan 30th, 2002, 01:24 PM
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. :)

gijsj
Jan 30th, 2002, 03:51 PM
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.

Dillinger4
Jan 30th, 2002, 05:13 PM
Posted by gijsj
That's normal Object Oriented behavior.


Of course it's normal Object Oriented behavior. :p 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.

JoshT
Jan 31st, 2002, 11:11 AM
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:
'normal OO
String s = "hello";

STDOUT.Print s.length;

'Operate on literal object
STDOUT.Print "hello".length;