Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code for review 1

Status
Not open for further replies.

Nademic

Programmer
Mar 25, 2008
18
0
0
CA
Hi,

just looking at some sample code for building a structure.
Can someone explain to me how this code works that follows.
I can see here that the tostring method is overridden and overloaded but how is this being called by just calling the structure. One of the last lines of code below shows console.writeline(p) which outputs to contents of the structure to a string using the tostring function. However I am not sure how this is automatically called, shouldn't it be console.writeline(p.tostring) to get that sort of result. Thanks in advance.

Code:
"
Public Overloads Overrides Function ToString() As String

            Return FirstName + " " + LastName + ", age " + Age.ToString

        End Function

Code:
Module Module1

    Structure Person
        Public FirstName As String
        Public LastName As String
        Public Age As Integer

        Public Sub New(ByVal _firstname As String, ByVal _lastname As String, ByVal _age As Integer)

            FirstName = _firstname
            LastName = _lastname
            Age = _age



        End Sub

        Public Overloads Overrides Function ToString() As String

            Return FirstName + " " + LastName + ", age " + Age.ToString

        End Function




    End Structure



    Sub Main()
        Dim p As Person = New Person("Dan", "Farnell", 28)
        Console.WriteLine(p)
        Console.ReadLine()


    End Sub

End Module
 
Whenever a non-string type is used in place of a string type in VB.Net, the object's ToString function is implicitly called to get the string value of that object. In this case the object would return "Dan Farnell, age 28". If you do not like this behavior you can force that all data type conversions be explicitly specified by using "Option Strict" at the top of all code files.
 
stravis said:
If you do not like this behavior you can force that all data type conversions be explicitly specified by using "Option Strict" at the top of all code files.

Additionally you can force Option Strict to be always set to On through:

[tt]Tools | Options | Projects and Solutions | VB Defaults[/tt]

(VS 2008, location may vary slightly in earlier versions)


or for a particular application using:

[tt]Project | <Application's Name> Properties | Compile[/tt]


Personally, I always have Option Strict and Option Explicit set to On.

If you are using late binding (for example to interact with MS Office or other Com objects), then it can be useful to set Option Strict to Off at the top of that particular code page.

Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top