Option Strict

  • Thread starter Thread starter Scott M.
  • Start date Start date
S

Scott M.

Why is it that even with Option Strict turned on I am allowed to use
non-string values in strings without having to cast the non-string type to a
string?

For example, when writing to the console with console.write(), I can write a
non-string value without having to cast it.
 
Scott M. said:
Why is it that even with Option Strict turned on I am allowed to use
non-string values in strings without having to cast the non-string type to a
string?

For example, when writing to the console with console.write(), I can write a
non-string value without having to cast it.

Console.Write will call the ToString( ) method on it's arguments automatically.
For value types like Integers passed to Console.Write this produces the string
representation you would expect from CStr and displays it.


Derek Harmon
 
Hi,

Console.write has many overloaded versions. One of them accepts
the object type. Therefore you can place anything in there without
converting it to string.

Ken
---------------
Why is it that even with Option Strict turned on I am allowed to use
non-string values in strings without having to cast the non-string type to a
string?

For example, when writing to the console with console.write(), I can write a
non-string value without having to cast it.
 
Scott,
To expand on Ken's comments.

VB.NET allows you to overload methods (Subs, Functions & Properties).
Overloading means creating the same method (same name) with a different
parameter list. When overloading only the types of the parameters are
considered the return type of a Function or Property is not.

For example if I was creating a class where a method sometimes expects a
String & sometimes expects an Integer I would overload the method.

Public Class Widget

Public Sub DoSomething(ByVal value As String)
End Sub

Public Sub DoSomething(ByVal value As Integer)
End Sub

End Class

Then when using the Widget class

Dim theWidget As New Widget

' this will call the string version of DoSomething
theWidget.DoSomething("Hello World")

' this will call the integer version of DoSomething
theWidget.DoSomething(100)

One advantage of doing this is that you can encapsulate the call to
Integer.ToString in DoSomething(Integer) rather then converting the Integer
& call DoSomething(String) in a 20+ places. For one or two I'm not sure I
would overload DoSomething. Which is why Console.Write has overloads for all
of the "primitive" types (String, Byte, Char, Integer, Short, Long, Single,
Double, ...)

Other classes may overload DoSomething as what happens when an Integer is
passed is different then when a String is passed, especially when the
Integer is the position in a collection, while String is the name of the
item in a collection.

For details see:
http://msdn.microsoft.com/library/d...ry/en-us/dndotnet/html/overloadingmethods.asp


As odd as it may sound Overloading is Overloading Polymorphism. As there are
4 kinds of polymorphism.

1: Coercion - a single abstraction serves several types through conversion
2: Overloading - a single identifier denotes several abstractions
3: Parametric - an abstraction operates uniformly across different types
4: Inclusion - an abstraction operates through an inclusion relation

Within VB.NET the math operators are examples of coercion polymorphism. You
can use the + operator on two integers to add them, you can also use the +
operator on singles & doubles which also adds them. In other words the +
operator is 'overloaded' for different built-in types. While C# allows
developers to define overload operators. VB.NET needs to wait for VS.NET
2005 (Whidbey, due out later in 2005) for operator overloading.

Within VB.NET we are allowed to overload members (overloading polymorphism)
within a single type as long as the parameter signatures are unique. Across
types we are free to overload members irregardless of signatures. Of course
VB.NET does define certain rules for overloading such as you cannot have a
function & a field with exactly the same name. ;-)

We will need to wait for VS.NET 2005 (Whidbey, due out later in 2005) to
gain Parametric Polymorphism (a.k.a Generics).

Within VB.NET Inclusion polymorphism is subtype polymorphism. Using Inherits
or Implements to define a type that is also another type (where type is a
Class, Structure, or Interface). Normally when developers are talking
Polymorphism they are referring to this type of Polymorphism.

Polymorphism definitions taken from:
http://www.javaworld.com/javaworld/jw-04-2001/jw-0413-polymorph.html

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top