Back to description
Welcome to the world of object-oriented programming and C#! The primary goal of this book is to use the C... more
Welcome to the world of object-oriented programming and C#! The primary goal of this book is to use the C# programming language from Microsoft to teach you object-oriented programming, or OOP . This book assumes that you have no prior programming experience in any language and that you know nothing about OOP.
If you do have programming experience and some familiarity with OOP, that’s fine. Having that experience makes things easier for you. However, I still encourage you to read the book from start to finish for a number of reasons. First, this book represents the distillation of 25 years of programming and teaching experience. I have a good idea of what works and what doesn’t work when it comes to explaining complex topics so that they’re easy to understand. Reading each chapter gives you the tools to understand the next chapter. Second, I may introduce topics in one chapter and then rely heavily on those topics in a much later chapter. In other words, the process used to learn OOP and C# is one that introduces new topics based upon ones that were introduced earlier. Obviously, it’s important to master the earlier content before tackling the later content. Finally, the programming examples I use also build on concepts presented in earlier program examples. It will be easier for you to understand the later program examples if you’ve experimented with those programs introduced earlier in the book.
One more thing: You cannot learn programming by just reading about it. You have to dig in and start programming yourself. For that reason there are exercises at the end of each chapter designed to help you hone your programming skills. The learning process is even more interesting if you try to create your own programs based upon some real problem you’d like to solve. Don’t worry if things don’t fall together instantly on the first try. You should plan to make a ton of “flat-forehead” mistakes…you know, the kind of mistake where, upon discovering it, you pound the heel of your hand into your forehead and say: “How could I make such a stupid mistake!” Not to worry…we’ve all been there. Such mistakes are just part of the process of becoming a programmer and you should expect to make your fair share. However, stick with it, read the book, and you’ll be surprised at how fast things will come together. Indeed, I think you’ll find programming to be a truly enjoyable pastime.
In this chapter, you will learn about
Downloading Visual Studio .NET’s C# Express
Installing C# Express
Testing C# Express to ensure it was installed correctly
With that in mind, let’s get started.
... less
As you learned in Chapter 1, programming with objects has been around for over four decades. However, it’s only in the last... more
As you learned in Chapter 1, programming with objects has been around for over four decades. However, it’s only in the last 15 years or so that object-oriented programming has become the norm rather than the exception. In this chapter I present a simple example of how objects might be used in an everyday situation. You then expand on the concepts presented in that example to an actual program you build using Visual Studio .NET and the objects it provides for you.
In this chapter, you will learn:
What an object is
What the term state means with respect to objects
What a class is
What it means to instantiate an object
What properties are
What methods are
How to use some of the objects provided by Visual Studio .NET
As you learned in Chapter 2, computer programs often input data in one form, process it, and then show the new data on the... more
As you learned in Chapter 2, computer programs often input data in one form, process it, and then show the new data on the computer screen. This kind of invites the question: What is data? Simply stated, data is information. Computer data is information that is stored in a variable for use in a program. In a broad sense, there are two basic kinds of computer data: 1) numeric data and 2) textual data.
Numeric data is any kind of data that can have arithmetic operations performed on it. You can add, subtract, multiply, and divide numeric data. You can perform other operations on numeric data, like finding a square root, a sine, or a cosine of a numeric quantity, and so on. You will also hear numeric data types referred to as value types. The precise meaning of this term is discussed in Chapter 5. For now, however, you can think of them as those data types whose variables can have data stored directly into them.
Textual data are character representations of data. Your name is a piece of data that can be represented in a program in a textual fashion. Some students get confused when you ask them whether a zip code is numeric or textual information. Because zip codes are usually not manipulated in mathematical expressions (for example, you wouldn’t need to multiply or divide by one), they are usually treated as textual information.
This chapter discusses the different data types C# provides to you. In this chapter, you will learn about the following:
Integer data types
The range of values for different data types
Floating-point data types
Which data type should be used to represent financial data
The Boolean data type
When to use one data type versus another
Intellisense
When you finish this chapter you will have a good understanding of why C# offers you such flexibility when it comes to selecting a data type.
This chapter concentrates on the basic building blocks C# provides you for writing programs. In this chapter, you will learn... more
This chapter concentrates on the basic building blocks C# provides you for writing programs. In this chapter, you will learn about the following:
Operators and operands
Expressions
Statements
lvalues and rvalues
What a symbol table is and how Visual Studio uses it
How Visual Studio interacts with Windows
The Bucket Analogy
Using the Visual Studio debugger
Magic numbers and why to avoid them
Most beginning books do not discuss many of the topics presented in this chapter. This may cause you to wonder why you need to understand them. There are several reasons. First, once you have mastered these topics, many of the other elements of C# (and programming in general) will become clearer and more intuitive. Memorizing certain rules is one approach to learning a language: once you’ve memorized (and obeyed) a rule, you can avoid some of the error messages that Visual Studio might throw at you. However, truly understanding why a rule exists in the first place is a vastly superior way to learn. It is this higher level of understanding that we seek. If you understand the material presented in this chapter, finding and correcting program errors becomes easier. Error detection becomes easier because you will have a deeper understanding of what C# is doing “under the hood.” Finally, your knowledge of the material presented here will be portable. That is, the concepts and techniques discussed in this chapter apply to all programming languages, not just C#. If you decide to learn another programming language later, these concepts will help you learn the new language.
In Chapter 3 you learned about the different value data types that C# makes available for use in your programs. This chapter... more
In Chapter 3 you learned about the different value data types that C# makes available for use in your programs. This chapter concentrates on the other major type of data in C#: reference data types. In this chapter, you will learn
What a reference data type is
How reference data types differ from value data types
How the Bucket Analogy can be applied to reference data types
You will also learn about the following:
String variables
Verbatim string literals
DateTime variables
DateTime
Constructor methods
Overloaded methods
Method signatures
In a very real sense, object-oriented programming owes much of its popularity to the power that reference data types bring to the programming table. Reference types are fundamental to object-oriented programming and you will use them a lot.
If computers could not use data to make decisions, they would be little more than very expensive boat anchors. Because it... more
If computers could not use data to make decisions, they would be little more than very expensive boat anchors. Because it is hard to write a nontrivial program without some form of decision-making ability in it, I have used some decision-making keywords in previous programs. However, I never really explained what those keywords actually do. That changes by the end of this chapter. In this chapter, you will learn about:
Relational operators
Comparing value types
Comparing reference types
The if and else keywords
if
else
Cascading if statements
Logical operators
The switch, case, break, and default keywords
switch
case
break
default
Data validation
This chapter also presents some thoughts on coding style. Coding style simply refers to the way you write program code. There are myriad coding styles. Because C# is not form-specific, you are free to use just about any coding style you wish. The author has known brilliant programmers whose code is almost impossible to read or decipher. On the other hand, I’ve had mediocre students who write code that is a joy to read. True, beauty is in the eye of the beholder. However, experience has taught me that 80 percent of a program’s development time is spent in testing, debugging, and maintaining the code, and only 20 percent in writing it. For that reason, anything you can do to make your code clearer and more easily understood is a good idea.
Computer programs are very good at doing repetitive tasks…much better than humans, because computers don’t get bored. In... more
Computer programs are very good at doing repetitive tasks…much better than humans, because computers don’t get bored. In this chapter I examine program loops. These are simply a means by which a program can repeat the execution of a given set of program statements. In this chapter you will learn the following:
What a loop is and when it should be used
What constitutes a well-behaved program loop
What happens when loops are ill-behaved
What a for loop is
for
What a while loop is
while
What a do-while loop is
do-while
When to use the break and continue statements
continue
Most nontrivial programs use some form of program loop. Loops are so useful that C# makes several different types available. Let’s see what loops can do for you.
In this chapter you will learn about arrays, array lists, and collections. As you will learn, these data structures are useful... more
In this chapter you will learn about arrays, array lists, and collections. As you will learn, these data structures are useful in solving many types of programming problems. In this chapter you will learn about:
Arrays
Array indexes and elements
How to set the size of an array
Some of the Array class methods that are commonly used
Multi-dimensional arrays
Array lists
Collections
Jagged arrays
When you finish this chapter, you’ll have an appreciation of how arrays can make certain programming problems much easier to solve.
In Chapter 2 I stated that classes are like cookie cutters. I went on to point out that Visual Studio provides you with a... more
In Chapter 2 I stated that classes are like cookie cutters. I went on to point out that Visual Studio provides you with a room full of cookie cutters hanging on the wall that you can use. You have used many of those cookie cutters in the sample programs, including labels, textboxes, buttons, listboxes, and listview objects, all of which are extensions of the basic Windows Forms class. Up to this point, you’ve been writing code that uses those objects in a single class that I have consistently named frmMain. I called it that because all the programs used a single class containing the Main() method that marks the starting point for all C# programs.
frmMain
Main()
Well, it’s time to cut the apron strings.
In this chapter you will learn how to make your own cookie cutters to hang on the wall. This chapter concentrates on the design considerations you need to think about in order to write “good” code for your own classes. A well-designed class becomes another cookie cutter that you can hang on the wall and use over and over in other programs. Poorly designed classes tend to become use-once-and-throw-away cookie cutters. Given that one of the major advantages of OOP is code reuse, you need to think about class design anytime the opportunity presents itself.
In this chapter, you will learn about:
What elements constitute good class design
Scope
The static storage class
static
Access specifiers
Class properties and methods
Class components and writing style
UML Light
General versus helper methods
User interfaces
This chapter creates a clsDates class as a reference point for learning about class design. You will add two methods to this class: a leap year method that’s a little different from the method offered by the DateTime class, and a getEaster() method for determining the date on which Easter falls. (Easter falls on a Sunday in either March or April, depending on the state of the lunar calendar.)
clsDates
getEaster()
The next two chapters are probably the two most important chapters in the book. I feel this way because these two chapters reveal two of the major benefits to be derived from OOP: data encapsulation and code reuse.
In Chapter 9 I initiated a discussion about designing your own classes. I ended that chapter by designing and writing a simple... more
In Chapter 9 I initiated a discussion about designing your own classes. I ended that chapter by designing and writing a simple class named clsDates that added two methods: getLeapYear() and getEaster(). This chapter continues the theme of class design, but gets into additional details about writing classes. In this chapter you will learn about:
getLeapYear()
Constructor details
Method overloading
Cohesion
Coupling
Property methods
Getters and setters
Changing the state of an object
Sensing an error in a class method
When you finish this chapter, you will have a solid foundation upon which to start writing your own classes. With a little effort, I think you’ll find that it is both worthwhile and kind of fun.
It would be wonderful if we didn’t even have to write a chapter like this one, but the truth is that problems do arise when... more
It would be wonderful if we didn’t even have to write a chapter like this one, but the truth is that problems do arise when programs are developed and run. In this chapter you will learn about
The differences among bugs, errors, and exceptions
Syntax, semantic, and logic bugs
Exception handling
Throwing exceptions
Using the debugger
Debugging windows
Bug detection, isolation, and correction
Most experienced programmers agree that approximately 20 percent of program development time consists of writing the program code. The remaining 80 percent is used for testing, debugging, and maintenance of that code. Clearly, anything you can do to reduce that 80 percent figure is going to help you keep on time and on budget.
This chapter discusses generics, a feature that has been a part of C# since its 2005 release. While Microsoft still has a... more
This chapter discusses generics, a feature that has been a part of C# since its 2005 release. While Microsoft still has a few hiccups to remove, generics have the potential to save you a ton of programming effort. In this chapter, you will learn about the following:
1. Generics
2. The advantages generics offer
3. Boxing and unboxing
4. Runtime type checking
5. Interfaces
6. Recursion
Generics offer you a way to stretch your programming time in ways that just make sense. Let’s examine what this relatively new OOP feature has to offer.
This chapter shows you how to use disk data files. If you think about it, computers would lose much of their functionality... more
This chapter shows you how to use disk data files. If you think about it, computers would lose much of their functionality if there wasn’t a way to permanently store, or persist, the data generated by a computer. Things that we take for granted today, such as making an airline reservation, must have been a nightmare before computers were in widespread use. In this chapter, you will learn about the following:
Two basic file types: text and binary
The advantages and disadvantages of each file type
Computer streams
Sequential files
Random access files
Serialization and deserialization
Multiple document interface (MDI) programs
Writing program menus
Up to this point you have written programs whose usefulness ended when the program ended. Once you’ve mastered the contents of this chapter, you’ll be able to write programs that can store data, enabling you to recall that data at some later time. Programs take on a whole new light when you know how to persist data.
Let’s get started.
In Chapter 13 you learned how to persist data using several different types of data files. In this chapter I expand on that... more
In Chapter 13 you learned how to persist data using several different types of data files. In this chapter I expand on that topic, but persist the data using database techniques. In this chapter you will learn:
What a relational database is
What tables, fields, records, and keys are
What normalization is
How to use the database Structured Query Language
What DDL is
How to use ADO.NET with databases
How to use bound controls
What LINQ is
It’s been said that of all the programs that are under development, over 80 percent of them use a database in one form or another. Clearly, this is one tool that must hang from your tool belt.
In this chapter I expand on the concept of inheritance. Although you have been using inheritance since the first chapter... more
In this chapter I expand on the concept of inheritance. Although you have been using inheritance since the first chapter of this book, I haven’t said much about it. In this chapter you learn the details of inheritance that are necessary to use it properly in your own programs. In this chapter you will learn the following:
What inheritance is
How inheritance simplifies program code
What base classes and derived classes are
What the protected access specifier is
protected
What polymorphism is
How polymorphism can simplify your programs
As you read this chapter, keep in mind that inheritance was added to OOP languages to make the programmer’s life easier. While the end user may not see any advantages to inheritance and polymorphism, you will have a greater appreciation of how much simpler they can make program code.
This appendix provides solutions to the chapter exercises found throughout the book.... more
This appendix provides solutions to the chapter exercises found throughout the book.
Purchase Before purchasing this product, please be sure you have met all software and system requirements, and that you understand any limits placed upon its use.
Return Policy Wrox Chapters on Demand are non-returnable and non-refundable.
Reader Software Wrox Chapters on Demand are offered as PDFs, and they must be viewed using the Adobe Reader. If you do not have the Reader installed, it can be downloaded for free at Adobe.com.
Test Download As Wrox Chapters on Demand purchases are non-returnable, it is advisable that you test your system and software configurations with a free sample download before you place an order.
Usage Rights for a Wrox Chapter on Demand File Any Wrox Chapter on Demand product you purchase from this site will come with certain restrictions that allow Wiley to protect the copyrights of its products. After you purchase and download this title, you:
If you have any questions about these restrictions, you may contact Customer Care at (877) 762-2974 (8 a.m. - 5 p.m. EST, Monday - Friday). If you have any issues related to Technical Support, please contact us at 800-762-2974 (United States only) or 317-572-3994 (International) 8 a.m. - 8 p.m. EST, Monday - Friday).
Related Books
C#/C++ Resources