BTI420 notes – Thu Jan 15 and Mon Jan 19

Our first session in a computer-lab room.

.

Reminder: What do I expect from you?

Before the Thursday or Monday computer-lab-room class, I expect you to prepare for the class. This means:

  • Read and study the current assignment
  • Practice its contents, and/or get started on its contents

In the Thursday or Monday computer-lab-room class, I expect you to be an engaged and actively-learning participant. This means:

  • Being prepared to split your time between new topic learning, and working hands-on with the topic or the current assignment
  • Asking and answering questions
  • Writing notes
  • Working on the current assignment

.

Textbook coverage

Chapter 2 – Controllers – skim the content

Chapter 3 – Views – skim the content

.

Topic coverage today

Presented in point form:

Modelling data entities with classes

String-to-from-number conversions

Get started with Visual Studio for a Console project

Get started with Visual Studio for an ASP.NET MVC project

Adding a Console project to an existing Visual Studio “solution”

Creating and delivering a data object to a view

Work on the assignment named Lab 1 (which is due at the end of the session)

The following topics will likely be covered next week:

Review of HTTP request-handling concepts

Lifecycle of a request in ASP.NET MVC

.

Modelling data entities with classes

Modelling data entities with classes is a simple task, as it is in other languages and frameworks.

A model class needs properties, maybe one or more methods, and maybe one or more constructors.

.

Getting started with string-number conversions

The String class is often used. It is a reference type, and is immutable. However, the editor, compiler, and runtime make it easy to work with strings. Declaring a string is simple, and does not require the “new” keyword (unlike the declarations for other reference types):

string myName = “Peter”;

And, although a string is immutable after it has been created, the compiler and runtime enable you to mutate (change) an existing string’s value by using a natural and comfortable syntax. Using the myName instance from above, let’s change it:

myName = “Prof. Peter McIntyre”;

Numbers in the .NET Framework are typically value types. Many numeric types are available, but we typically use int and double in C#.

.

Conversions

Why do you care about converting between strings and numbers?

Well, think about it: What data type is HTML, which appears in a browser?

Text (string).

Therefore, if you are “round-tripping” numbers to and from a web page, you must convert them to and from strings.

You can “convert” a number to a string with the number’s “ToString()” method:

string foo = 123.ToString();

Some number types have ToString() overloads, which permit you to specify a preset (common) or custom number format string.

If a string contains or holds a numeric value, there is a System.Convert class, which has methods that help. For example:

// The “Convert” class is in the local scope, because “using System;”
// is always present at the top of the source code file
int bar = Convert.ToInt32(“123″);

This is an unsafe conversion, because it’s possible that the string cannot be converted. If that’s the situation, this statement will cause an exception (a runtime error).

A better approach is to use the Int32.TryParse() method:

// Create a temporary variable
int foo;
// Attempt to convert…
bool isNumber = Int32.TryParse(“123″, out foo);

The return result of TryParse() is a bool.

If the conversion is successful:

  1. The return result is “true”, and
  2. The variable “foo” holds the converted string-to-number

If it’s not successful, the return result is false, and foo is zero.

There is a Double.TryParse() method that you can use for double values.

Back to strings for a moment… How can you determine whether a string variable is null or empty? Use this test:

// Assume that “stringVariable” may or may not hold some content…
bool isProblem = string.IsNullOrEmpty(stringVariable);

.

.

.

.

.

.

.

.

.

.

.

.

.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: