Difference between revisions of "OOP Class2"

esse quam videri
Jump to: navigation, search
(Basic Data Types)
(Operators)
Line 128: Line 128:
 
  int myVar = 15; //sets the value of myVar to 15
 
  int myVar = 15; //sets the value of myVar to 15
  
Mathematical Operators
+
===Mathematical Operators===
 
{|-
 
{|-
 
|Operator || Description  
 
|Operator || Description  
Line 143: Line 143:
 
|}
 
|}
  
Increment Decrement Operators
+
===Increment Decrement Operators===
 
{|-
 
{|-
 
|Operator || Description  
 
|Operator || Description  
Line 166: Line 166:
 
|}
 
|}
  
operator Precedence
+
===Operator Precedence===
 
Evaluated First
 
Evaluated First
  
Line 180: Line 180:
 
+ is also used to concatenate strings
 
+ is also used to concatenate strings
  
     //Create a string and set it's value to "cool."
+
      
 +
<csharp>//Create a string and set it's value to "cool."
 
     string coolString = "cool";
 
     string coolString = "cool";
 
     //Do some concatenations and make it super cool
 
     //Do some concatenations and make it super cool
     Console.WriteLine ("Super " + "string " + "theory!!!\n" + "Is really " + coolString + ".");
+
     Console.WriteLine ("Super " + "string " + "theory!!!\n" + "Is really " + coolString + ".");</csharp>
 
      
 
      
  
Line 211: Line 212:
 
implicit conversion
 
implicit conversion
  
int intNumber = 1000;
+
<csharp>int intNumber = 1000;
 
long lngNumber;
 
long lngNumber;
 
lngNumber  = intNumber;
 
lngNumber  = intNumber;
Line 218: Line 219:
 
int Number2= 5;
 
int Number2= 5;
 
int Number3 = Number1 / Number2;
 
int Number3 = Number1 / Number2;
//Number3 == 1
+
//Number3 == 1 </csharp>
  
  
 
explicit conversion
 
explicit conversion
  
long lngNumber = 1000;
+
<csharp>long lngNumber = 1000;
 
int intNumber;
 
int intNumber;
 
intNumber = (int)lngNumber;
 
intNumber = (int)lngNumber;
Line 230: Line 231:
 
int Number2= 5;
 
int Number2= 5;
 
double Number3 = (double)Number1 / (double)Number2;
 
double Number3 = (double)Number1 / (double)Number2;
//Number3 == 1.2
+
//Number3 == 1.2</csharp>
  
 
Constants
 
Constants
Line 237: Line 238:
 
syntax
 
syntax
  
const type identifier = value;
+
<csharp>const type identifier = value;</csharp>
 
 
 
 
  
 
example
 
example
  
const int freezingPoint = 32;
+
<csharp>const int freezingPoint = 32;
 
const int freezingPointMetric = 0;
 
const int freezingPointMetric = 0;
const float pi = 3.141592
+
const float pi = 3.141592</csharp>
  
 
Arrays
 
Arrays
Line 251: Line 252:
 
Syntax
 
Syntax
  
type [] identifier
+
<csharp>type [] identifier</csharp>
  
 
single dimension arrays
 
single dimension arrays
  
   string [] aryNames = new string[3];
+
   <csharp>string [] aryNames = new string[3];
 
  
 
  
 
   aryNames [0] = "Joe";
 
   aryNames [0] = "Joe";
 
   aryNames [1] = "Mike";
 
   aryNames [1] = "Mike";
   aryNames [2] = "Alice";
+
   aryNames [2] = "Alice";</csharp>
  
 
Example single dimensions array singleArrray.aspx  Source multi dimension arrays
 
Example single dimensions array singleArrray.aspx  Source multi dimension arrays
  
  string [,] aryNames = new string[3,3];
+
  <csharp>string [,] aryNames = new string[3,3];
 
 
 
aryNames [0,0] = "Joe";
 
aryNames [0,0] = "Joe";
Line 273: Line 274:
 
aryNames [2,0] = "Mary";
 
aryNames [2,0] = "Mary";
 
aryNames [2,1] = "Alice";
 
aryNames [2,1] = "Alice";
aryNames [2,2] = "333 333-3333";
+
aryNames [2,2] = "333 333-3333";</csharp>
  
 
Example multi dimensions array multiArrray.aspx  Source jagged arrays
 
Example multi dimensions array multiArrray.aspx  Source jagged arrays
  
string [][] aryNames = new string[3][];
+
<csharp>string [][] aryNames = new string[3][];
 
 
 
aryNames[0] = new string[2];
 
aryNames[0] = new string[2];
Line 293: Line 294:
 
aryNames [2][0] = "Mary";
 
aryNames [2][0] = "Mary";
 
aryNames [2][1] = "Alice";
 
aryNames [2][1] = "Alice";
aryNames [2][2] = "Im not a number im a free woman";
+
aryNames [2][2] = "Im not a number im a free woman";</csharp>
  
 
Example jagged array jaggedArrray.aspx  Source
 
Example jagged array jaggedArrray.aspx  Source
 
even more array samples
 
even more array samples
  
using System;
+
<csharp>using System;
  
 
class Array
 
class Array
Line 342: Line 343:
 
Console.WriteLine("myStrings[0]: {0}, myStrings[1]: {1}, myStrings[2]: {2}", myStrings[0], myStrings[1], myStrings[2]);
 
Console.WriteLine("myStrings[0]: {0}, myStrings[1]: {1}, myStrings[2]: {2}", myStrings[0], myStrings[1], myStrings[2]);
 
}
 
}
}
+
}</csharp>
  
Structs
+
==Structs
  
 
Lightweight alternatives to classes. Structs do not support inheritance or destructors. Don't worry if you don't understand structs yet it hard cuz the book teaches them before classes. We will talk more about stucts after we talk about classes
 
Lightweight alternatives to classes. Structs do not support inheritance or destructors. Don't worry if you don't understand structs yet it hard cuz the book teaches them before classes. We will talk more about stucts after we talk about classes
Line 360: Line 361:
 
 
  
Enumerators
+
==Enumerators==
  
 
Enumerators are used to set predefined list of named constants.
 
Enumerators are used to set predefined list of named constants.
Line 386: Line 387:
 
   }
 
   }
  
Web Forms
 
 
Http is a staless protocol. There is mo mechanism built in to the protocol that allows the server to remeber clients or requests. An http simply responds to http verbs GET, POST, PUT, DEL, TRACE etc. contained in RFC 2068 HTTP/1.1
 
Introduction to ASP .NET and Web Forms - uses VB.Net
 
 
Old html forms post information using forms in 2 ways with a get or a post http request.
 
Get
 
 
Get send information to the server using the URI. Limited to 1024 character in some browsers and servers.
 
 
<form action="class2.aspx" method="get">
 
FirstName: <input type="text" name="FirstName"><br />
 
LastName: <input type="text" name="LastName"><br />
 
<input type="submit" value="Submit">
 
</form>
 
 
FirstName:
 
LastName:
 
Post - Post posts the varibles in the HTTP Header.
 
 
<form action="class2.aspx" method="post">
 
FirstName: <input type="text" name="FirstName"><br />
 
LastName: <input type="text" name="LastName"><br />
 
<input type="submit" value="Submit">
 
</form>
 
 
FirstName:
 
LastName:
 
Header Name Value HttpMethod GET
 
Connection keep-alive
 
Keep-Alive 300
 
Accept text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
 
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
 
Accept-Encoding gzip,deflate
 
Accept-Language en-us,en;q=0.5
 
Cookie ASP.NET_SessionId=0cjrrijphnnknkvjth5mdw55
 
Host imdev
 
Referer http://imdev/infod/jeff/
 
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7
 
 
HTML Forms
 
Example HTML Forms
 
htmlForms.html  Source
 
Examples of Web Forms
 
ASP.NET Server Controls
 
 
Example server controls serverControls.aspx  Source
 
HomeWork
 
Learning c#
 
Chapter 6, Chapter 7 47-84
 
 
Read Chapter 03
 
Read Chapter 04 pg 59-190
 
LearnC-Sharp tutorial 1 Data Types - Learnc# is DED
 
 
Build an aspx page that can post data and catch the results like PostandCatch.aspx simple or PostCatch_com .aspx -tougher
 
 
    HTML form page                          1 pt 
 
    HTML (only html no c# or .net) page that demostrates an html form using
 
    at least 3 html form elements.   
 
 
    HTML post to aspx catch                1 pt
 
    Use your previously created html FORM tp post info to an asps page that catches
 
    all the variables and displays them.
 
   
 
    An aspx webform page that posts to itself  2 pts   
 
    An aspx web form that posts information to itself and displays the results.
 
 
  
 +
==HomeWork==
 +
*Learning c#
 +
*Chapter 6, Chapter 7 47-84
  
  
Build a console application
+
*Build a console application
  
Variable Types and Casting 4pts 1 extra credit
+
  Variable Types and Casting 4pts 1 extra credit
Build a console application that declares and initializes three integers with watever values you like
+
  Build a console application that declares and initializes three integers with watever values you like
Display these integers in the console
+
  Display these integers in the console
Cast the intergers into three floats
+
  Cast the intergers into three floats
Display the floats
+
  Display the floats
Cast the integers into three strings
+
  Cast the integers into three strings
Display the strings
+
  Display the strings
Put the strings into an array
+
  Put the strings into an array
Display the array
+
  Display the array
  
extra credit use an enumerator to change word stings into ints ie "one" = 1 "two" = 2
+
  extra credit use an enumerator to change word stings into ints ie "one" = 1 "two" = 2
  
 
 
Line 478: Line 414:
 
Next week Quiz on weeks 1 and 2
 
Next week Quiz on weeks 1 and 2
  
Chapter 3 Source
 
checkpage.aspx View Source
 
listpage.aspx View Source
 
listpage2.aspx View Source
 
listpage2_fixed.aspx -jeff View Source
 
  
labelcontrol.aspx View Source
+
==Links==
listpage2_fixed.aspx View Source
 
radiopage.aspx View Source
 
textboxpage.aspx View Source
 
Chapter 4 Source
 
variable.aspx View Source
 
variable2.aspx View Source
 
tax.aspx View Source
 
textboxarray.aspx View Source
 
conversion.aspx View Source
 
other sources
 
server-controls from professional ASP.NET
 
Links
 
 
A Comparative Overview of C#
 
A Comparative Overview of C#
 
Introduction to ASP .NET and Web Forms - uses VB.Net
 
Introduction to ASP .NET and Web Forms - uses VB.Net

Revision as of 23:44, 10 January 2006

C# fundamentals

Questions from week 1 reading. discussion

Some pages comparing c# to other languages

A Comparative Overview of C# http://genamics.com/developer/csharp_comparative.htm

C# and Java: Comparing Programming Languages http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncenet/html/tchCJavaComparingProgrammingLanguages.asp

Basic Data Types

C# is a strongly typed language. This means every object in C# must be declared to be of a specific type. Variable Types

Type Size in Bytes .Net Type Description
byte 1 Byte Unsigned (0-255)
char 2 Char Unicode Characters ascii unicode and other
bool 1 Boolean True of False (note c# boolean values do not eqaute to interget value True != (read as is not equal to) 1 and False != 0)
sbyte 1 SByte Signed integers(-128 to 127)
short 2 Int16 Signed integers(-32,768 to 32,767)
ushort 2 UInt16 Unsigned integers(0 to 65,535)
int 4 Int32 Signed integers(-2,147,483,648 to 2,147,483,647)
uint 4 UInt32 Unsigned integers(0 to 4,294,967,295)
float 4 Single fixed-precision up to 7 digits. Floating point number ( 1.5 x 10-45 to 3.4 x 1038 )
double 8 Double fixed-precision up to 16 digits. Floating point number ( 5.0 x 10-324 to 1.7 x 10308 )
decimal 12 Decimal fixed-precision up to 28 digits. Typically used for financial calculations. Required the suffix "m" or "M"
long 8 Int64 Signed integer ( -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
ulong 8 UInt64 Unsigned integer (0 to 18,446,744,073,709,551,615 )

Variables must be declared with an identifier and then initialized.

Declaration

Declaration sets aside a named section of memory the is the proper size to hold the declared type. At this point the variable contains nothing.

// declare a variable int firstInt; string myString;

Initialization

Initialization actually sets the variables value

// initialize the variable firstInt = 1; myString = "Hello!";

other ways to do it

// declare some variables int secondInt, thirdInt, fourthInt; secondInt = 2; thirdInt = 3; fourthInt = 4;

//declare and initialize variables in one line int myNegativeInt = -2147483648;

In c# variables cannot be used unil they are initalized. For example

   int UsedBeforeInit;
   Console.WriteLine(UsedBeforeInit);

will produce

helloError4.cs(10,31): error CS0165: Use of unassigned local variable

       'UsedBeforeInit'

UsedBeforeInit.aspx - web example of misused variable source UsedBeforeInit_Fixed.aspx - all beter now source

More examples of built in types 1x1.cs C# intrinsic types from Learn-C-Sharp. variable.aspx - example from book aspx page View Source variable2.aspx = example from book errors View Source

Naming conventions

Name variables intelligently. Name variables with names that have meaning.

Hungarian Notation Hungarian notation id a popular notation system used be many C, C++ and VB programmers. It was originally devised in Charles Simonyi's doctoral thesis, "Meta-Programming: A Software Production Method." Hungarian notation specifies that a prefix be added to each variable that indicated that variables type. It also specifies sometimes adding a suffice to clarify variables meaning. In the early 1980's Microsoft adopted this notation system.


ie... intHitCounter, intHitsPerMonthMax Hungarian Notation - Charles Simonyi Microsoft

PascalNotation Capitalize first Letter and then the first letter on each word. ie... PascalNotation

Use on method names method names.

camelNotation Lower case first letter and then capitalize the first letter of each word ie... camelNotation

use for variable names Other Coding Techniques and practices Microsoft - Coding Techniques and Programming Practices IBM Best Practices for Programming in C GNU Coding Standards GNU Naming conventions More Types

Operators

ECMA-334 Operators and punctuators

C# uses the equals = sign for Assignment

int myVar = 15;			 //sets the value of myVar to 15

Mathematical Operators

Operator Description
addition
* multiplication
/ division
% modulus remainder Ask Dr.Math What is Modulus?

Increment Decrement Operators

Operator Description
+ increment same as foo = foo + 1
-- decrement same as foo = foo - 1
= calculate and reassign addition
-= calculate and reassign subtraction
*= calculate and reassign multiplication
/= calculate and reassign division
%= calculate and reassign modulus
y= x++ assignment prefix y is assigned to x and then x in incremented
y= ++x assignment postfix x is incremented and then assigned to y

Operator Precedence

Evaluated First

   * ++,--,unary-
   * *,/,%
   * +,-

Evaluated Last

   * =,+=,-=,*=,etc


+ is also used to concatenate strings


<csharp>//Create a string and set it's value to "cool."

    string coolString = "cool";
    //Do some concatenations and make it super cool
    Console.WriteLine ("Super " + "string " + "theory!!!\n" + "Is really " + coolString + ".");</csharp>
    

will output (remember \n is a new line)

    Super string theory!!!!
    Is really cool.


Short in class Assignment In class assignment 10-15 mins Build a c# console app (remember to take a takedown aproach start small with somethin you know)

   * Declare and initialize two integers
   * Display their values using Console.WriteLine
   * Declare a third integer and initialize it with the sum of the first two integers
   * Output the value of the third integer

Once the console application is done convert it to work on a web page using Response.Write instead of Console.WriteLine

Top down development with comments /infod/jeff/classsource/class2/topdown.aspx Fully implemeted Console adding program /infod/jeff/classsource/class2/add.cs Conversions and Casting

Casting is the process of converting form one type of object to another, There are two Conversion types in C# implicit and explicit. Implicit conversion is handled by the compiler and requires no extra work. Implicit conversion is only possible if the new data type can hold the old data type with out any data loss. explicit conversion forces on datatype into another even if it doesn't fit. It is up to the programmer to define explicit casts. implicit conversion

<csharp>int intNumber = 1000; long lngNumber; lngNumber = intNumber;

int Number1= 6; int Number2= 5; int Number3 = Number1 / Number2; //Number3 == 1 </csharp>


explicit conversion

<csharp>long lngNumber = 1000; int intNumber; intNumber = (int)lngNumber;

int Number1= 6; int Number2= 5; double Number3 = (double)Number1 / (double)Number2; //Number3 == 1.2</csharp>

Constants

Constants are datatypes that will be assigned a value that will be constant thought the executing of the code. You cannot change constants once they have been assigned a value. syntax

<csharp>const type identifier = value;</csharp>


example

<csharp>const int freezingPoint = 32; const int freezingPointMetric = 0; const float pi = 3.141592</csharp>

Arrays

Arrays are groups of variables of the same type Syntax

<csharp>type [] identifier</csharp>

single dimension arrays

 		<csharp>string [] aryNames = new string[3];
 		
 		aryNames [0] = "Joe";
 		aryNames [1] = "Mike";
 		aryNames [2] = "Alice";</csharp>

Example single dimensions array singleArrray.aspx Source multi dimension arrays

		<csharp>string [,] aryNames = new string[3,3];

aryNames [0,0] = "Joe"; aryNames [0,1] = "Schmoe"; aryNames [0,2] = "111 111-1111"; aryNames [1,0] = "Mike"; aryNames [1,1] = "Orbinawitz"; aryNames [1,2] = "222 222-2222"; aryNames [2,0] = "Mary"; aryNames [2,1] = "Alice"; aryNames [2,2] = "333 333-3333";</csharp>

Example multi dimensions array multiArrray.aspx Source jagged arrays

<csharp>string [][] aryNames = new string[3][];

aryNames[0] = new string[2]; aryNames[1] = new string[4]; aryNames[2] = new string[3];

aryNames [0][0] = "John"; aryNames [0][1] = "Doe";

aryNames [1][0] = "James"; aryNames [1][1] = "Bond"; aryNames [1][2] = "007"; aryNames [1][3] = "License to kill";

aryNames [2][0] = "Mary"; aryNames [2][1] = "Alice"; aryNames [2][2] = "Im not a number im a free woman";</csharp>

Example jagged array jaggedArrray.aspx Source even more array samples

<csharp>using System;

class Array { public static void Main() { //array of ints int[] myInts = {5,10,15}; Console.WriteLine("array of ints:"); Console.WriteLine( "myInts[0]: {0}, myInts[1]: {1}, myInts[2]:{2}" ,myInts[0],myInts[1],myInts[2]);

//jagged array of bools bool[][] myBools = new bool[2][]; myBools[0] = new bool[2]; myBools[1] = new bool[1];

myBools[0][0] = true; myBools[0][1] = false; myBools[1][0] = true; Console.WriteLine("jagged array of bools:"); Console.WriteLine( "myBools[0][0]: {0}, myBools[1][0]: {1}", myBools[0][0], myBools[1][0]);

//multi-dimensional array of doubles double[,] myDoubles = new double[2,2]; myDoubles[0, 0] = 3.147; myDoubles[0, 1] = 7.157; myDoubles[1, 1] = 2.117; myDoubles[1, 0] = 56.00138917; Console.WriteLine("multi-dimensional array of doubles:"); Console.WriteLine("myDoubles[0, 0]: {0}, myDoubles[1, 0]: {1}", myDoubles[0, 0], myDoubles[1, 0]);


//array of strings string[] myStrings = new string[3]; myStrings[0] = "Joe"; myStrings[1] = "Matt"; myStrings[2] = "Robert"; Console.WriteLine("array of strings:"); Console.WriteLine("myStrings[0]: {0}, myStrings[1]: {1}, myStrings[2]: {2}", myStrings[0], myStrings[1], myStrings[2]); } }</csharp>

==Structs

Lightweight alternatives to classes. Structs do not support inheritance or destructors. Don't worry if you don't understand structs yet it hard cuz the book teaches them before classes. We will talk more about stucts after we talk about classes Syntax

[ attributes] [access-modifiers] struct identifier [:interface-list {struct members}

struct Dog {

public string name;
public string weight;
public int age;

}


Enumerators

Enumerators are used to set predefined list of named constants. Syntax

[ attributes] [modifiers] enum identifier [:base-type {enumerator-list};

 //An enumerator for ServingSizes at BK
 enum ServingSizes : uint
 {
   Small = 0,
   Regular = 1,
   Large = 2,
   SuperSize = 3
 }
 
 //another more usefull example
 // forced sequence to start  
 // from 1 instead of 0 (default)
 enum Months 
 {
   January = 1, February , March, April ,
   May , June , July , August , Sept , Oct , Nov , Dec 
 }


HomeWork

  • Learning c#
  • Chapter 6, Chapter 7 47-84


  • Build a console application
  Variable Types and Casting 4pts 1 extra credit
  Build a console application that declares and initializes three integers with watever values you like
  Display these integers in the console
  Cast the intergers into three floats
  Display the floats
  Cast the integers into three strings
  Display the strings
  Put the strings into an array
  Display the array
  extra credit use an enumerator to change word stings into ints ie "one" = 1 "two" = 2



codetoad.com ASP.NET : HTML Server Controls aspalliance.com - ASP.NET Syntax for Web Server Controls Next week Quiz on weeks 1 and 2


Links

A Comparative Overview of C# Introduction to ASP .NET and Web Forms - uses VB.Net Microsoft - Coding Techniques and Programming Practices IBM Best Practices for Programming in C GNU Coding Standards GNU Naming conventions RFC 2068 HTTP/1.1 Review