Dice Sample (Consume C# Classes)
This sample illustrates how to consume C# classes from Visual J# by means of the following:
Implementing an interface (defined in C#) on a Visual J# class.
Handling exceptions thrown from a C# class, in Visual J#.
Examining properties exposed by a C# class, in Visual J#.
Sinking events raised by a C# class in Visual J#.
The cscServer project implements a die (the kind used in games).
The number of sides (faces) on the die can be set programmatically. The die is rolled by the Roll method. The Roll operation returns the number on the top face of the die.
The die uses the interface IRoll to perform the roll operation. Clients are required to provide an implementation of this interface. The client is required to set this interface on the die before invoking a Roll method on the die.
If the die is constructed with an invalid number of sides (for example, the number of sides being less than or equal to 0), the die throws a BadDieSizeException. If the Roll method on the die is invoked prior to the number of sides being set, the die throws aexception.
The die exposes a read-only property (NumSides) that can be used to inspect the number of sides for the die.
Every time an implementation of the IRoll interface is set on the die, it raises the LoadedDice event.
The Dice project does the following:
Provides implementations for the IRoll interface.
Catches both the exceptions thrown.
Examines the NumSides property.
Provides a delegate object to sink the events raised by the die.
This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
Building and Running the Sample
To open the sample file in Solution Explorer
Click Download Sample.
The File Download message box appears.
Click Open, and on the left column of the zip folder window, click Extract all files.
The Extraction Wizard opens.
Click Next. You can change the directory that the files will be extracted to, and then click Next again.
Make sure that the Show extracted files check box is selected, and click Finish.
Double-click the sample's .sln file.
The sample solution is displayed in Solution Explorer. You might get a security warning that says the solution location is not trusted. Click OK to continue.
To build from the development environment
On the Build menu, click Build Solution, or press CTRL+SHIFT+B.
From the command line, type BUILD.bat.
For more information, see.
To run this sample
From the development environment, press F5.
From the command line, type Dice and press ENTER.