Example 2210 Using PInvoke to call a Win32 API method
using System.Runtime.InteropServices declare the WinAPI method you wish to P Invoke DllImport kernel32.dll, EntryPoint MoveFile, ExactSpelling false, CharSet CharSet.Unicode, SetLastError true static extern bool MoveFile string sourceFile, string destinationFile make an instance and run it Tester t new Tester string theDirectory c test media DirectoryInfo dir new DirectoryInfo theDirectory t.ExploreDirectory dir Set it running with a directory name private void ExploreDirectory DirectoryInfo...
State
A web application's State is the current value of all the controls and variables for the current user in the current session. The Web is inherently a stateless environment. This means that every post to the server loses the state from previous posts, unless the developer takes great pains to preserve this session knowledge. ASP.NET, however, provides support for maintaining the state of a user's session. Whenever a page is posted to the server, it is re-created by the server from scratch before...
Example 194 The remoting Calculator client
using System.Runtime.Remoting.Channels using Console.WriteLine Watson, come here I need you create an Http channel and register it uses port 0 to indicate won't be listening HttpChannel chan new HttpChannel 0 get my object from across the http channel MarshalByRefObject obj MarshalByRefObject RemotingServices.Connect typeof Programming CSharp.ICalc , cast the object to our interface Programming CSharp.ICalc calc use the interface to call methods double sum calc.Add 3.0,4.0 double difference...
Example 2211 Declaring Win32 API methods for import into a C program
DllImport kernel32, SetLastError true static extern unsafe int CreateFile string filename, uint desiredAccess, uint shareMode, uint attributes, uint creationDisposition, uint flagsAndAttributes, uint templateFile DllImport kernel32, SetLastError true static extern unsafe bool ReadFile int hFile, void lpBuffer, int nBytesToRead, int nBytesRead, int overlapped You will create a new class APIFileReader whose constructor will invoke the createFile method. The constructor takes a filename as a...
Example 219 Implementing a network streaming client
static public void Main string Args create a TcpClient to talk to the server TcpClient socketForServer new TcpClient localHost, 65000 Failed to connect to server at 0 65000, localhost return create the Network Stream and the Stream Reader object NetworkStream networkStream socketForServer.GetStream System.IO.StreamReader streamReader new read the data from the host and display it outputString streamReader.ReadLine Exception reading from Server To test this, I created a simple test file named...
Dynamic Invocation with InvokeMember
The first approach will be to create a class named BruteForceSums dynamically, at runtime. The BruteForceSums class will contain a method, ComputeSum , that implements the brute-force approach. You'll write that class to disk, compile it, and then use dynamic invocation to invoke its brute-force method by means of the InvokeMember method of the Type class. The key point is that BruteForceSums.cs won't exist until you run the program. You'll create it when you need it and supply its arguments...
Example 919 Using the IDictionaryEnumerator interface
a simple class to store in the array Create and initialize a new Hashtable. Hashtable hashTable new Hashtable hashTable.Add 000440312, George Washington hashTable.Add 000123933, Abraham Lincoln hashTable.Add 000145938, John Galt hashTable.Add 000773394, Ayn Rand Display the properties and values of the Hashtable. Console.WriteLine hashTable Console.WriteLine Count 0 , hashTable.Count Console.WriteLine Keys and Values PrintKeysAndValues hashTable public static void PrintKeysAndValues Hashtable...
Conversion Operators
C will convert int to long implicitly, and allow you to convert long to int explicitly. The conversion from int to long is implicit because you know that any int will fit into the memory representation of a long. The reverse operation, from long to int, must be explicit using a cast because it is possible to lose information in the conversion You want the same functionality for your fractions. Given an int, you can support an implicit conversion to a fraction because any whole value is equal to...
Polymorphism
There are two powerful aspects to inheritance. One is code reuse. When you create a ListBox class, you're able to reuse some of the logic in the base Window class. What is arguably more powerful, however, is the second aspect of inheritance polymorphism. Poly means many and morph means form. Thus, polymorphism refers to being able to use many forms of a type without regard to the details. When the phone company sends your phone a ring signal, it does not know what type of phone is on the other...