housekeeping
Here are the books and websites I use most when the answers are not coming to me.C# for Programmers (2nd Edition) (Deitel Developer Series)
Pop open Microsoft Visual C# 2008 Express Edition - I suggest you pin it to your start menu.
Notice the area in the center, called the Start Page.
Now look closer at the "Getting Started" section.
There are actually some good resources there for you to use.
ready, set, go.
Today we create our first application.
Sadly it'll be a console app.Click File - New Project - Console Application. You'll get something that looks like this:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { } } }
The using's are all the .net namespaces or libraries that are included in the project by default.
The ConsoleApplication1
namespace is your own private realm where your code exists. All .net code exists in a namespace.
Variables
As I mentioned, all things in C# are class objects. The difference between a class object and a variable are that class objects can contain methods and they can be extended. An int in Delphi can't have any of its own functions, but in C#, even the simplest data object can and does.
Do this: Add this to the code above.
static void Main(string[] args) { int x; int y = 2; x = 7; int z = x + y; } }
int x; creates an integer class object but does not assign it a default value. Any attempt to read or manipulate it at this point would cause an exception.
The int y = 2; line shows a shorthand way of creating and initializing the value at the same time.
The x = 7; shows how to add or change data in an existing data object.
The int z = x + y; line shows a variable z being created containing the results of a calculation. Note that subsequent changes to x and y will not cause z to recalculate, it just works the math at the time and stores it in z.
Now do this. Type z. and a popup context helper appears. This shows the methods that can be accessed from an int.
if you mouse-hover over the items you get an explanation like this.
Ok, time to move on to...
EMail Demo
Now clear out all the varibables and type in the word MailMessage as shown.
Note the little rectangle under the "e". Hover your mouse over that and you'll see this:
What the IDE is offering to do is add the namespace System.Net.Mail to your using section or, alternately, add it to the declaration itself. You don't have to add a namespace to your project to access it, you can explicitly pick objects from any namespace by using their fully qualified name. Adding them to the using clause just simplifies the typing.
In this case, choose the first one, add System.Net.Mail to the using section.
Now you'll see the using System.Net.Mail;
has been added to the top section of the program and all the objects and methods in that namespace are now native to your program.
The MailMessage class is an object that can be used to send or receive email messages. You can create an empty one and load it later like this...
MailMessage myMailMessage = new MailMessage(); myMailMessage.To = new MailAddress("bryanv@eloan.com", "Bryan Valencia"); myMailMessage.From = new MailAddress("gumby@eloan.com", "Gumby and Pokey"); myMailMessage.Subject = "I am feeling down again."; myMailMessage.Body = "I am all congested with disclosures.";
Or you can create it and initialize it with variables. Note that we had to create 2 MailAddress objects to set the To and From addresses in myMailMessage.
The following example shows how to create a MailMessage object with all its values pre-initialized.
static void Main(string[] args) { MailMessage myMailMessage = new MailMessage( "gumby@eloan.com", "bryanv@eloan.com", "I am feeling down again.", "I am all congested with disclosures." ); }
Ok, now let's send this bad-boy. In order to do that (and please feel free to substitute your own address for the To address), you need an object called the SMTPClient. It's in the same namespace as the MailMessage.
static void Main(string[] args) { MailMessage myMailMessage = new MailMessage( "gumby@eloan.com", "bryanv@eloan.com", "I am feeling down again.", "I am all congested with disclosures." ); SmtpClient mySMPTClient = new SmtpClient("eloan.com"); mySMPTClient.Send(myMailMessage); Console.WriteLine("Message Sent"); }
Now, hit Control-F5, or from the menu, Debug - Start without debugging. You should get this:
Then in about 10 seconds, whoever you decided to bless with your email should see it in their inbox.
Let me diagram what we just did. When we created the MailMessage object, here's what was happening:
what we typed | what happened |
---|---|
MailMessage | We told C# that we are about to create an object of the class MailMessage in the System.Net.Mail namespace. |
myMailMessage | we told C# that we are going to call it myMailMessage. |
= new MailMessage(...) | We told C# to go ahead and create it now - that means to allocate memory and initialize it with either default values or the ones we are passing in. The object is now created and resides somewhere in memory, and can be accessed by the name myMailMessage. |
Constructors
Every class object has a predefined method called a constructor. It tells C# how to create instances of itself. Constructors can be simple, like the constructor for an int, or more complicated like the one for MailMessage. They can even be overloaded.
Note how the context helper pops up a helper for the constructor. There are 4 overloaded constructors for the MailMessage class. Use the down arrow key to see the formats you can choose from. All methods in all objects will pop these helpers while you are editing.
A few little items
Single quotes and double quotes are not interchangeable. Strings use the double quote marks "like this". Chars use the single quote (or tickmark or apostrophe), like this: 'A'.
A class is the organization of an object, an object is an instance of that class. Or, think of classes as cookie cutters (or blueprints) and objects as cookies (or Lear Jets).
Overloading means that there can be many methods with the same name. More on this later.
No comments:
Post a Comment