Search code examples
c#javascriptc#-3.0activexstack-overflow

C# - error while accessing a simple property of activeX control in js - stackoverflowexception


I am creating an ActiveX object in javascript, and calling a method on the object. This works perfectly fine. But, if I try to access a property of the activeX control (or return a value from the called method), the browser crashes (message : IE has stopped working). In the details section, it says that System.StackOverflowException was not caught. The C# code is :

interface IScreenshot{
   int data {get; set;}
}

 public class ScreenShot : UserControl, IScreenShot, IObjectSafety
    {
       public int data
        {
            get
            {
                return data;
            }
            set
            {
                data=13;
            }
        }
   }

The js code to access it is :

<script type="text/javascript" language="JavaScript">
 var x = new ActiveXObject("Try1.ScreenShot");
 var value = x.data;
</script>

Detailed error is :

Problem signature:
  Problem Event Name: CLR20r3
  Problem Signature 01: iexplore.exe
  Problem Signature 02: 9.0.8112.16448
  Problem Signature 03: 4fecf1b7
  Problem Signature 04: Try1
  Problem Signature 05: 1.0.0.0
  Problem Signature 06: 507399f0
  Problem Signature 07: 5
  Problem Signature 08: 0
  Problem Signature 09: System.StackOverflowException
  OS Version: 6.1.7601.2.1.0.768.11
  Locale ID: 1033

Solution

  • Look at this code:

     public int data
     {
        get
        {
            return data;
        }
    }
    

    In order to return the data, you... return the data. Bang. You need to have a field to store the value.

    Your setter also ignores the incoming value, which is distinctly odd.

    Assuming you don't really need any logic for your property, you can use an automatically implemented property - ideally having changed the name to follow .NET naming conventions:

    public int Data { get; set; }
    

    (Ideally you'd give it a more meaningful name at the same time...)

    Note that this has nothing to do with Javascript - you'd see the same problem with that property implementation in a pure C# app.