I think I got to response...

First what happened is a miss-understood, in the example you showed me
back then, you started a thread in the constructor of a class, so,
since the constructor runs when you create the object...

Whenever we make instance of that class the thread start running
immediately

In addition that thread used another member of the same class to start
(It called a private method named "run").

Cerberus exposes the same fact, when we create an object (instance a
class) we are calling its constructor (note: the framework does a few
things more than that, that are not to mention now).

About the value of index in "int index", Glenn is accurate as always.
the parameter of the indexer (i. e. the subindex) is being set when
you access it from the main.

Hope this one gives you a better idea of the code flow:

using System;
class PwrOfTwo
{
    /* Access a logical array that contains
       the powers of 2 from 0 to 15. */
    public int this[int index]
    {
        // Compute and return power of 2.
        get
        {
            int R;
            Console.Write("\n\n<PwrOfTwo.this>");
            if ((index >= 0) && (index < 16))
                R = pwr(index);
            else
                R = -1;
            Console.Write("</PwrOfTwo.this>\n");
            return R;
        }
        // there is no set accessor
    }
    int pwr(int p)
    {
        Console.Write("<PwrOfTwo.pwr>");
        int result = 1;
        for (int i = 0; i < p; i++)
            result *= 2;
        Console.Write("</PwrOfTwo.pwr>\n");
        return result;
    }
}

class UsePwrOfTwo
{
    public static void Main()
    {
        int Tmp;
        Console.WriteLine("<UsePwrOfTwo.main>");
        PwrOfTwo pwr = new PwrOfTwo();
        Console.Write("First 8 powers of 2: \n");
        for (int i = 0; i < 8; i++)
        {
            Tmp = pwr[i];
            Console.Write(Tmp + " ");
        }
        Console.WriteLine();
        Console.Write("Here are some errors: ");
        Tmp = pwr[-1];
        Console.Write(Tmp + " ");
        Tmp = pwr[17];
        Console.Write(Tmp.ToString());
        Console.WriteLine();
        Console.WriteLine("</UsePwrOfTwo.main>");
        Console.ReadKey();
    }
}


I think I can remember something similar to "run from the constructor"
approach in Java, often used in Applets in Midlets, (I don't know
about Servlets), in particular Midlets are forced to run its main code
in the constructor of their main class. but still all the calls are
programatically done (by you, or by the libs you use).

If you get confuse on what is calling your code, set a breakpoint (F9
in VS if I'm not wrong), and when the code reaches that point open the
call stack window, to see who called who. Also try step by step, I
think you already know that one.

Reply via email to