VB.NET

Moderators: seancampbell
Number of threads: 4022
Number of posts: 10035

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
What is the wrong in this sample code Posted by Al_Mo3tasm on 29 Nov 2005 at 4:28 AM
This message was edited by Al_Mo3tasm at 2005-11-29 4:31:13

Module Module1

Sub Main()

Dim ob1 As New xx
ob1.h = 20
Console.WriteLine(ob1.h)


End Sub

Public Class xx

Property h()
Get
Return h
End Get
Set(ByVal Value)
h = Value
End Set
End Property

End Class

End Module



The error is:
An unhandled exception of type 'systrm.StackOverflowException'occurred in unknown Module.

Report
Re: What is the wrong in this sample code Posted by iwilld0it on 29 Nov 2005 at 12:13 PM
This message was edited by iwilld0it at 2005-11-29 12:17:59

Public Class xx 

    Property h() 
        Get 
            Return h 
        End Get 
        Set(ByVal Value) 
            h = Value
        End Set 
    End Property 

End Class 


The part in bold is causing the stack overflow because you are recusively causing property h to be re-set. The mere assignment is causing the Set portion of the property statement to be re-executed (think about it.)

The proper way to do it is this ...

Public Class xx 
    Private mH

    Property h() 
        Get 
            Return mH
        End Get 
        Set(ByVal Value) 
            mH = Value
        End Set 
    End Property 

End Class 


This is the typical pattern to assigning a property value to internal private variable (This is what encapsulation is.)

Report
Re: What is the wrong in this sample code Posted by cucu on 17 Dec 2005 at 12:50 PM
Define the type of variable in the following statement

: Set(ByVal Value)?
Set(Byval Value as Long)




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.