简体   繁体   中英

Creating and initializing an object with '(Class)Object' type of Initialization

StateObjClass State = (StateObjClass)StateObj;

Can someone explain the method of initialization of 'State'. Need help in understanding the above statement.

Surrounding Code:

private class StateObjClass
{
    // Used to hold parameters for calls to TimerTask. 
    public int SomeValue;
    public System.Threading.Timer TimerReference;
    public bool TimerCanceled;
}

public void RunTimer()
{
    StateObjClass StateObj = new StateObjClass();
    StateObj.TimerCanceled = false;
    StateObj.SomeValue = 1;
    System.Threading.TimerCallback TimerDelegate =
    new System.Threading.TimerCallback(TimerTask);

    // Create a timer that calls a procedure every 2 seconds. 
    // Note: There is no Start method; the timer starts running as soon as  
    // the instance is created.
    System.Threading.Timer TimerItem =
    new System.Threading.Timer(TimerDelegate, StateObj, 2000, 2000);

    // Save a reference for Dispose.
    StateObj.TimerReference = TimerItem;  

    // Run for ten loops. 
    while (StateObj.SomeValue < 10) 
    {
        // Wait one second.
        System.Threading.Thread.Sleep(1000);  
    }

    // Request Dispose of the timer object.
    StateObj.TimerCanceled = true;  
}

private void TimerTask(object StateObj)
{
    StateObjClass State = (StateObjClass)StateObj;
    // Use the interlocked class to increment the counter variable.
    System.Threading.Interlocked.Increment(ref State.SomeValue);
    System.Diagnostics.Debug.WriteLine("Launched new thread  " +    DateTime.Now.ToString());
    if (State.TimerCanceled)    
    // Dispose Requested.
    {
        State.TimerReference.Dispose();
        System.Diagnostics.Debug.WriteLine("Done  " + DateTime.Now.ToString());
    }
}

It would help if you'd show surrounding code, but I suppose StateObj is probably declared as a different type as StateObjClass , like foo here:

object foo = new Foo();
Foo foo2 = (Foo)foo;

It is a Foo , but foo is declared as object , so whenever you use foo in your code, the compiler sees it as object .

In order to assign it to a more derived variable, like foo2 , you'll have to cast foo to Foo .

If you don't the compiler will complain:

object foo = new Foo();
Foo foo2 = foo;

Cannot implicitly convert type 'object' to 'Foo'

It isn't really initialized. The () notation in this sense is actually a type cast. So StateObj must inherit from StateObjClass , and it is just being cast to that here and assigned to the State variable.

It is hard to know exactly why it is being done here, as there is only really one line of code with no context.

This is commonly done to specify which set of methods to expose if the StateObj has multiple overloads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM