Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
180
Добавлен:
24.05.2014
Размер:
26 Мб
Скачать
☆

990

Part XII

APPENDIXES

 

 

 

{

Console.WriteLine (x);

}

Here, in is a keyword for the foreach statement.

 

 

 

 

 

 

 

 

 

 

CAUTION

 

 

 

 

 

 

 

 

 

The value of the variable in the foreach statement cannot change during the execu-

 

 

tion of the foreach loop.

 

 

 

Y

 

 

 

 

 

 

 

 

 

 

 

 

 

L

 

 

Having discussed some of the significant components that make a C# program, I

 

 

 

 

 

F

 

 

will provide a sample program that illustrates the use of these components.

 

 

 

 

M

 

 

 

 

A

 

 

A Sample Program

 

 

 

 

 

using System;

E

 

 

 

 

 

T

 

 

 

 

 

 

 

 

 

 

 

 

 

namespace MyFirstProgram

 

 

 

 

{

/// MyFirstClass declaration begins here. class MyFirstClass

{

static void Main(string[] args)

{

//declare the variable string name=”name”; string guess=”Blue”; string response=”n”;

Console.WriteLine(“Please enter your name:”); name=Console.ReadLine(); Console.WriteLine(“Hello {0}”,name);

Console.WriteLine(“Here are the first ten numbers:”); for(int i=1; i<=10;i++)

{

Console.WriteLine(“{0}”,i);

Team-Fly®

BASICS OF C# Appendix E 991

}

do

{

Console.WriteLine(“Guess what’s my favorite color?”); guess=Console.ReadLine();

if(guess==”Blue”)

{

Console.WriteLine(“You win! Guessed right.”); break;

}

else

{

Console.WriteLine(“Keep guessing...”);

Console.WriteLine(“Enter n/N to quit.”);

response= Console.ReadLine();

}

} while( response!=”N” && response!=”n”);

}

}

}

The preceding sample is a Console application written in C#. This program accepts the user name and displays “Hello” followed by the username. Then, it displays the first ten numbers, 1 to 10, using a for loop. Using a do...while loop, it requires the user to enter a color as a string value. The entered color is compared with “Blue”. If it matches, the program jumps out of the do...while loop. Otherwise, after displaying the message, it gives the user the option to quit. If the user opts to quit, the program breaks out of the loop; otherwise, it goes on.

Summary

You have just touched the tip of the C# iceberg! C# is obviously much more than the information presented here. But, the intent of this appendix is to just get you familiar with the new entrant in the .NET languages.

This page intentionally left blank

Index

A

abnormal execution function call results, 265 abstract classes, 307

AccessKey property, WebControl class, 527 ACID properties, 798

activation, in-place, 564

Active Template Library (ATL), 161 ActiveForm property, Form class, 356 ActiveX Data Objects (ADO), 449 Add Member Variable Wizard, 62 Add() method, 456

Add Method Wizard, 620 Add Property Wizard, 619–620

AddDocTemplate member function, CWinApp class, 111

adding

Breakpoints, 285–289

events, data validation mechanism, 66–67 AddNew function, 169

AddRef method, IUnknown interface, 575–576 ADO (ActiveX Data Objects), 449 ADO.NET

benefits, 451–452

DataSet objects

ConstraintCollection object, 459 DataColumnCollection object, 458 DataRelationCollection object, 458–459 DataRowCollection object, 457 DataTableCollection object, 456–457

purpose, 454–455 datasets

defined, 453–454 features, 450–451 filling, 828

namespaces, data-related, 452–453 typed datasets, 460

untyped datasets, 460–461

.NET data providers

components, 461–462 objects, interaction, 463 objects exposed, 784

OLE DB .NET data provider, 463–465 purpose, 453

SQL Server .NET data provider, 465–467 XML support, 467–468

AfxBeginThread function, CWinThread class user-interface threads, creating, 140–141 worker threads, creating, 138–140

afxmt.h header file, 149 algorithms

encryption, 763–764 GUIDs, 578–581

Luhn algorithm, 612, 622–627 alpha blending, 323

apartment threading model, 589–590 AppDomains, 890–891

Application Center 2000, 941

application class component, Documment/View architecture, 102, 103

application framework, MFC Application Wizard creating, 52–56

customizing

button events, handling, 68–69 form controls, adding, 59–61

form controls, variable creation, 61–63 transferring data, adding events mechanism, 66–67 transferring data, DDV mechanism, 65 transferring data, DDX mechanism, 64

application layer, .NET Framework, 920 application programming interface (API), 448 application(s). See also case studies; example code

business services, 563 data services, 563

dialog-based applications, 88 document-centric applications, 100 executing, 58

994 Index

application(s) (continued)

execution events, flowcharted, 32 high-availability, creating, 926–927 initializing, 25

MDI, creating, 114–115 monolithic, 560–561

.NET applications, compiling, 5–6 network, developing

IPAddress class, 848–850 Socket class, 843–844 System.Net namespace, 843

System.Net.Socket namespace, 843 System.Threading namespace, 850

Thread class, 850–851 UdpClient class, 844–846

WebRequest class, 848 SDI, creating, 52–56 services, 563

single-tier, 560–561 three-tier, 563 two-tier, 561–562 types, 52–53

Web application DLL, 640 architecture

ASP.NET, 512

ATL Server, 640 client-server, 561–562

Documment/View architecture classes, CDocument, 103–107 classes, CFrameWnd, 109–110 classes, CView, 107–109 classes, CWinApp, 110–112 components, 101–102 control flows, 128–130

dynamic objects, creating, 112–113 message routing, 130

purpose, 100–101 remoting, 874, 875 single-tier, 560–561 three-tier, 563 two-tier, 561–562 UDA, 447

Web services, 673–674 Windows DNA, 590–591

array elements variables, Visual C# .NET, 982 arrays, Visual C# .NET, 985–986

ASP.NET, 547–548 architecture, 512 benefits, 508–510 history, 508

system requirements, 511 validation controls

CompareValidator validation control, 530, 532–534

CustomValidator validation control, 530, 536–537 multiple, 538–539

process described, 530–531

RangeValidator validation control, 530, 534–535

RegularExpressionValidator validation control, 530, 535–536

RequiredFieldValidator validation control, 530, 532, 539

ValidationSummary validation control, 530, 537–538

in Visual Studio .NET, 13

Web Forms, components, 513–514 Web Forms, creating

files created, 515 folders created, 520–521 page design, 516–519

root directory default, IIS application, creating, 521–522

root directory default, IIS application, tracing, 520 steps, 514

testing, 519–520

welcome message application, 519 Web Forms, features, 513

Web server controls adding, at run time, 525 adding, using code, 525

adding, using Toolbox, 524–525 adding, when, 524

event handling, 527–530

HTML server controls versus, 523–524 properties, setting, at design time, 526 properties, setting, at run time, 526–527

ASPX file, 514, 548

ASPX pages, testing options, 520 assemblers, 294

assemblies

benefits, 758–759 cache, placing in, 767

categories

application access types, 756–757 execution, 755

contents, 760–761 conversion tools, 757 creating, 761–762 described, 11–12, 754 DLL compatibility, 759 GAC, 11, 757–758 keys, public and private

explained, 763–764

shared names, generating, 764–765 location, deployment, 765–766 manifests, purpose, 11, 754, 761 names, 762–763

.NET components usage, 768 role, .Net programming, 755–756 signing, 767

versioning, 759–760 assembly linker utility, 757 AssemblyInfo.cpp file, 702 AssemblyInfo.cs file, 515 ASSERT macro, 268 asynchronous threads, 148

ATL (Active Template Library), 161

ATL OLE DB Consumer Wizard, 738, 741 ATL Project Wizard, 614

ATL Server. See also case study (ATL Server applications, creating)

attributes, 599, 962 components, 639–642 described, 639 features, 644–646 maps, 642–643

request handlers, 642–644 ATL server attribute category, 599

ATL Server Project Wizard, 653–655, 657 ATL Simple Object Wizard, 616–617 atomic transactions, 795

Attribute class, 601 attribute(s)

ATL Server attributes, 962 benefits, 600

categories, 599 COM attributes, 961

compiler attributes, 963

Index 995

custom attributes described, 600–601 designing, 601–603

IDL attributes, 962

OLE DB Consumer attributes, 963 providers, 615

purpose, 598 reflection, 603–606

authentication application-specific, 693–694 ASP.NET, 695–696

Automatic Transactions, 595 Autos window, 289–290

AutoScale property, Form class, 356

AutoScaleBaseSize property, Form class, 356

B

BackColor property, WebControl class, 527 balancing, load, 593

base class library, 9 base properties, 525

BaseValidator class, 530

BEGIN_MESSAGE_MAP() macro, 34, 35

binary formatters, 883 binary standards, 565, 566 binding, v-table, 577

BizTalk Server 2000, 942–943 BlinkStyle property, 499

BorderColor property, WebControl class, 527 BorderStyle property, WebControl class, 527 BorderWidth property, WebControl class, 527 boxing, 310, 985

break mode

debugging windows, 294 defined, 280

breakpoints, defined, 280 Breakpoints window

Breakpoint dialog box, 281–284 inserting Breakpoints, 285–289 purpose, 280

toolbar, 287–288 Brush class, 333–334 brushes

creating, 45–48 example usage, 42–44

996 Index

brushes (continued)

horizontal gradient brushes, 322 linear gradient brushes, 322 styles, table of, 46

bugs, defined, 268

build configurations, defined, 271

business logic layer, .NET Framework, 920 business services, applications, 563 Button class, 357

button events, handling, 68–69 buttons

Button class, 357

button events, handling, 68–69 Cancel button example code, 238–239

Commit button example code, 258–262 Execute Query button, 185

Modify button, 226–227

push button dialog box control, 80 radio button dialog box control, 80

ToolBarButton class, 367–368 Update button example code, 227–235

byte code, Java, 6

C

C#. See Visual C# .NET

C++ exception handling, 265–266

CAboutDig class, 58 cache

assemblies, placing in, 767

global assembly cache (GAC), 11, 757–758 Cache API, 510

caching ASP.NET, 510

ISA Server 2000, 948–950 Call Stack window, 293

Cancel button example code, 238–239

CAOs (client-activated objects), 876–877, 885

CArchiveException class, 267 cardinal splines, 322

CARP (Cache Array Routing Protocol), 948 CAS (code access security), 12

case study (ADO.NET in Managed C++, implementing) Account_Detail table, creating, 471–472 background, 470

BankLogin table, creating, 472

database, creating, 471 database schema, 470 Managed C++ application

Account Details form, BankForm class constructor, 483–484

Account Details form, BankForm class declaration, 480–483

Account Details form, ClearForm method, 498 Account Details form, CreateControls method,

484–492

Account Details form, FetchData method, 492–493

Account Details form, FillFields method, 493–494

Account Details form, OnFirstRecord method, 494–495

Account Details form, OnInsertRecord method, 495–497

Account Details form, OnUpdateRecord method, 497–498

Account Details form, ValidateForm method, 499–501

Login form, creating, 474–480 project, creating, 473–474

case study (ASP.NET with C# and Managed C++) ASP.NET application, creating

ASP.NET page, modifying, 548–553 files created, 547–548

steps, 547 background, 542

class libraries, Managed C++, creating, 544–547 project life cycle, 543–544

case study (ATL Server applications, creating). See also ATL Server

ATL Server project, creating, 652–656 background, 648, 656–658

default handler, adding methods, 659–664 form handling, 659

HTML form-based interface, creating, 651–652 project design, 649–651

SRF, modifying, 658–659 XML file, creating empty, 664

case study (COM using ATL, implementing) background, 608

credit card validation component CoClass class, adding, 615–618 COM component, creating, 613–614

interface, defining, 618–622 Luhn algorithm, described, 612

Luhn algorithm, implementing, 622–627 testing, 627–631

project design, 609–611

case study (database connectivity) background, 174–175 creating steps, overview, 184

data source, configuring, 188–191 database, creating steps, 184–186 MFC-based SDI application

Account_Detail form, Add option, 223–225 Account_Detail form, Cancel button example code,

238–239

Account_Detail form, Commit button example code, 258–262

Account_Detail form, Issue CheckBook option example code, 243–245

Account_Detail form, logic outline, 214 Account_Detail form, Modify button, 226–227 Account_Detail form, navigation functionality,

214–222

Account_Detail form, OnDeActivate method example code, 242–243

Account_Detail form, OnSelChangeAccno method example code, 239–241

Account_Detail form, Update button example code, 227–235

Account_Detail form, VerfiyDate example code, 235–237

CheckForBalance method, example code, 257–258 coding, overview, 205–206

creating steps, 192–196 Deposit/Withdrawal form, coding, 245–252

Deposit/Withdrawal form, designing, 201–203 Login form, coding, explained, 209, 212–213 Login form, design, 199–201

Login form, example code, 207–209, 211–212 Login form, logic outline, 206

Login form, validating, CRecordSet class creating, 209–211

main form design, 196–199 main menu, editing, 205

Transfer Operations form, coding, 252–258 Transfer Operations form, designing, 203–204 VerifyAccountNumber function, example code,

255–256

Index 997

project life cycle construction phase, 183

high-level design phase, 177–180 integration and testing phase, 183

low-level design phase, database schema, 181–183 low-level design phase, described, 180–181 overview, 175–176

requirements analysis phase, 176–177 user acceptance testing phase, 184

tables, code for creating, 186–188

case study (Managed Extensions applications, creating) background, 348–349

code listing, 380–439 development life cycle (DLC)

coding phase, 353

design phase, 349–353, 354 execution phase, 355 requirements analysis phase, 349 testing phase, 353, 355

event handling, 369–374

files and control flow, overview, 379–380 Managed C++ application, creating, 355 menus, creating, 360–365

status bars, creating, 366 toolbars, creating, 366–369

user interface form, creating, 355–360

case study (.NET assemblies using Managed C++ extensions, creating)

background, 778

customer table, creating, 781

data, accessing, SQL Server .NET Data Provider, 784–785

login assembly, creating, 782–784 methods, implementing, 785–791 project design, 779–780

testing, COM component, 791–792

case study (network-based applications, creating) background, 852

coding, 855–868 project design, 853–855

case study (remoting, implementing) applications, creating

client, 901–913 remote object, 891–898 remote server, 898–901

background, 888

998 Index

case study (continued) project design, 889–890 testing, 913

case study (using COM+ services from .NET assemblies) background, 794

database tables, creating, 798–799 ProcessOrder assembly, creating, 799–804 ProcessOrder component, registering, 804–805 project design, 794–796

testing, 805–806

case study (Web services using ATL Server, creating) background, 726–727

C# application, adding, 745–746 project design, 727–728

stored procedures, creating, 728–729 tables, creating, 728–729

testing, 745–748

Web service, creating, 730–745

case study (Web services using Managed C++, creating) accessing data, SQL Server.NET Data Provider,

709–714 background, 698

client, Managed C++, building, 714–722 project design, 699–701

Web service, Managed C++ adding functionality, 704–709 creating basic, 701–704 testing, 704–709

case study (XML with Visual C++ .NET). See also .NET Framework, XML classes

application, coding, 823–833 background, 812

project design, 813–815 catalogs, 796

CClientDC device context class, 40

CCmdTarget class, 23

CCriticalSection class, 150

CCW (COM callable wrapper), 769–770

CDaoException class, 267

CDatabase class, 162

CDatabaseAppSet constructor, 164–165

CDBException class, 267

CDC device context class, 39–40 CDialog class, 84–85

CDocument class described, 103–104

example code, 106–107 member functions, 104–105 tasks to perform, 104

CEditView derived class, CView class, 109 CEvent class, 151–152

CException class, 266

CFameWnd class, 24

CFileException class, 267

CFormView derived class, CView class, 109 CFrameWnd class, 109–110

channel services, remoting, 881–883 check box dialog box control, 80 CHtmlView derived class, CView class, 109 class factories, 587

class libraries

Managed C++, creating, 544–547

.NET Framework, 9–10 class tables, 588

Class View window, Visual Studio .NET IDE, 14 classes. See also classes, listed by name

abstract classes, 307

created, MFC Application Wizard, 56–58 dialog classes, creating, 88–90

managed classes, 306–307

MFC exception handling, 266–267 sealed classes, 308

unmanaged classes, 306–307 Visual C# .NET, 978–979

classes, listed by name. See also classes

Attribute class, 601

BaseValidator class, 530

Brush class, 333–334

Button class, 357

CAboutDig class, 58

CArchiveException class, 267

CClientDC device context class, 40

CCmdTarget class, 23

CCriticalSection class, 150

CDaoException class, 267

CDatabase class, 162

CDBException class, 267

CDC device context class, 39–40 CDialog class, 84–85

CDocument class described, 103–104 example code, 106–107

 

 

Index

 

999

member functions, 104–105

 

 

 

 

described, 107

 

tasks to perform, 104

member functions, 107–108

 

CEditView derived class, CView class, 109

tasks to perform, 108–109

 

CEvent class, 151–152

CWinApp class

 

CException class, 266

described, 23–24, 110

 

CFileException class, 267

example code, 111–112

 

CFormView derived class, CView class, 109

member functions, 25–26, 110–111

 

CFrameWnd class, 24, 109–110

in threads, 137

 

CHtmlView derived class, CView class, 109

CWindowDC device context class, 40

 

CListView derived class, CView class, 109

CWinThread class, 137

 

CMainFrame class, 56

CWnd class, 24

 

CMemoryException class, 267

DataColumn class, 459

 

CMetaFileDC device context class, 40

DataRelation class, 459

 

CMultiLock class, 152

DataRelationCollection class, 458

 

CMutex class, 149–150

DataRow class, 457, 459

 

CMyFirstApplicationApp class, 57

DataRowCollection class, 457

 

CMyFirstApplicationDoc class, 57

DataSet class, 454, 459

 

CMyFirstApplicationView class, 57–58

DataTable class, 459

 

CObject class, 23, 117

Delegate class, 339–340

 

CoClass class, 584, 615–618

EventArgs class, 338–339

 

components classes, 960

FileStream class, 829

 

configured classes, 796

Form class, 356

 

Constraint class, 459

garbage-collected classes, 306

 

CPaintDC device context class, 40

Graphics class

 

CRecordset class

methods, table of, 330–333

 

CDatabaseAppSet constructor, 164–165

properties, table of, 329–330

 

data members, 164–165

purpose, 329

 

DoFieldExchange() function, 166–167

ImageList class, 367

 

GetDefaultConnect() function, 165

IPAddress class, 848–850

 

GetDefaultSQL() function, 166

Label class, 358

 

navigating recordsets, 168

MainMenu class, 361–362

 

purpose, 162–163

MenuItem class, 360–361

 

records, adding, 169

MulticastDelegate class, 341

 

records, deleting, 169–170

OLE DB .NET data provider classes, table of, 464–465

records, modifying, 170

Pen class

 

recordset types, 163

methods, table of, 335–336

 

CRecordSet-based classes, creating, 209–211

properties, table of, 334–335

 

CRecordView class, 169–170

purpose, 334

 

CRequestHandlerT class, 642

Socket class, 843–844

 

CRichEditView derived class, CView class, 109

SQL Server .NET data provider classes, table of, 465

CScrollView derived class, CView class, 109

StatusBar class, 366

 

CSemaphore class, 150–151

StreamWriter class, 829

 

CSingleLock class, 152

String class, 986–987

 

CSyncObject class, 148–149

System.Array class, System namespace, 10

 

CTreeView derived class, CView class, 109

System.Console class, System namespace, 10

 

CView class