Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






UserState object.

A good question beans.

For the placeBets example of Step 28:

In the async response object e the property e.UserState is of type Object, hence it can contain an object of any type. At design time Intellisense does not know what type of object will ultimately be assigned to this property, so it cannot offer any suggestions (apart from the members of the fundamental Object class). The complier will accept anything you type in here. This property name will be resolved at run time when the expression:

e.UserState.Param

is evaluated (in a process called "late binding"). If the Param member doesn’t exist (or you’ve miss-spelt it) you will get "MissingMemberException (Public member 'xxx' on type 'UserState' not found)" when your program runs.

If you wish you can introduce an intermediate variable u in a separate statement to cast the object back to type UserState:

Dim u As UserState = e.UserState
Print("MarketId = " & u.Param)

Intellisense will now work as expected on the variable u.

A point of confusion: Perhaps unwisely, UserState is the name I’ve given to the class which holds the user parameter. It is also the name of the property of the async response object (e.UserState) which holds our parameter-containing object. So the name UserState refers to two completely different entities. The compiler does not have a problem with this because it sorts names out by context. Thus names can be reused freely as long as their context is unambiguous.


Response time measurement

Because UserState can be an object of any complexity, it can hold more than one parameter. Let’s now define another class to hold two parameters:

Code:

Class Params

Property MarketId As Integer

Property SendTime As Date

Sub New(ByVal _MarketId As Integer)

MarketId = _MarketId 'The marketId parameter

SendTime = Now 'The current time parameter

End Sub

End Class

and use an object from this class instead in the async call statement:

BetfairUK.placeBetsAsync(oPlaceBetsReq, New Params(MarketId)) 'Call API to place the bets

This sends the two parameters along with the request object. The parameters are MarketId (as before) and SendTime (which holds the time when the async call was made). These parameters can be recovered in Sub BetfairUK_placeBetsCompleteted (and the overall response time calculated) using code something like this:

Code:

............

 

With e.Result

CheckHeader(.header)

 

Dim p As Params = e.UserState 'The object containing the parameters

Dim t As TimeSpan = Now - p.SendTime 'The overall response time

Print("MarketId = " & p.MarketId & " Time = " & t.ToString)

 

If .errorCode = BFUK.PlaceBetsErrorEnum.OK Then 'placeBets call OK

............

............

You can extend on this idea to pass as many parameters as you like to the event handler for any async call. The UserState object can provide a very useful facility.

 


Date: 2014-12-21; view: 1195


<== previous page | next page ==>
Step 28. Placing Multiple Bets with placeBets. | Declaring variables
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.008 sec.)