| |||||||||||
Imports System | using System; | ||||||||||
| |||||||||||
' Single line only | // Single line | ||||||||||
| |||||||||||
Value Types Reference Types Initializing Type Information Type Conversion | Value Types Reference Types Initializing Type Information Type Conversion | ||||||||||
| |||||||||||
Const MAX_STUDENTS As Integer = 25 ' Can set to a const or var; may be initialized in a constructor | const int MAX_STUDENTS = 25; // Can set to a const or var; may be initialized in a constructor | ||||||||||
| |||||||||||
Enum Action | enum Action {Start, Stop, Rewind, Forward}; | ||||||||||
| |||||||||||
Comparison Arithmetic Assignment Bitwise Logical Note: AndAlso and OrElse perform short-circuit logical evaluations String Concatenation | Comparison Arithmetic Assignment Bitwise Logical Note: && and || perform short-circuit logical evaluations String Concatenation | ||||||||||
| |||||||||||
greeting = IIf(age < 20, "What's up?", "Hello") ' One line doesn't require "End If" ' Use : to put two commands on same line ' Preferred ' To break up any long single line use _ 'If x > 5 Then Select Case color ' Must be a primitive data type | greeting = age < 20 ? "What's up?" : "Hello"; if (age < 20) // Multiple statements must be enclosed in {}
No need for _ or : since ; is used to terminate each statement.
| ||||||||||
| |||||||||||
' Array or collection looping ' Breaking out of loops ' Continue to next iteration | Pre-test Loops: // no "until" keyword
// Array or collection looping
// Continue to next iteration | ||||||||||
| |||||||||||
Dim nums() As Integer = {1, 2, 3}
| int[] nums = {1, 2, 3}; float[,] twoD = new float[rows, cols]; int[][] jagged = new int[3][] { | ||||||||||
| |||||||||||
' Pass by value (in, default), reference (in/out), and reference (out) Dim a = 1, b = 1, c As Integer ' c set to zero by default ' Accept variable number of arguments ' Optional parameters must be listed last and must have a default value | // Pass by value (in, default), reference (in/out), and reference (out) int a = 1, b = 1, c; // c doesn't need initializing // Accept variable number of arguments int total = Sum(4, 3, 2, 1); // returns 10 /* C# doesn't support optional arguments/parameters. Just create two different versions of the same function. */ | ||||||||||
| |||||||||||
Special character constants (all also accessible from ControlChars class) ' String concatenation (use & or +) ' Chars ' No string literal operator ' String comparison ' String matching with Like - Regex is more powerful ' Substring ' Replacement ' Split ' Date to string ' Integer to String ' String to Integer ' Mutable string | Escape sequences // String concatenation // Chars // String literal // String comparison // String matching - No Like equivalent, use Regex
// Replacement // Split // Date to string // int to string // string to int // Mutable string | ||||||||||
| |||||||||||
Imports System.Text.RegularExpressions ' Match a string pattern ' Find and remember all matching patterns ' Remeber multiple parts of matched pattern ' Replace all occurrances of a pattern ' Replace matched patterns ' Split a string based on a pattern | using System.Text.RegularExpressions; // Match a string pattern
// Replace all occurrances of a pattern // Replace matched patterns // Split a string based on a pattern | ||||||||||
| |||||||||||
' Throw an exception ' Catch an exception ' Deprecated unstructured error handling | // Throw an exception // Catch an exception | ||||||||||
| |||||||||||
Namespace Harding.Compsci.Graphics ' or Namespace Harding Imports Harding.Compsci.Graphics | namespace Harding.Compsci.Graphics { // or namespace Harding { using Harding.Compsci.Graphics; | ||||||||||
| |||||||||||
Accessibility keywords ' Inheritance ' Interface definition // Extending an interface // Interface implementation | Accessibility keywords // Inheritance
// Extending an interface
| ||||||||||
| |||||||||||
Class SuperHero | class SuperHero { | ||||||||||
| |||||||||||
Dim hero As SuperHero = New SuperHero With hero hero.Defend("Laura Jones") Dim hero2 As SuperHero = hero ' Both reference the same object hero = Nothing ' Free the object If hero Is Nothing Then _ Dim obj As Object = New SuperHero ' Mark object for quick disposal | SuperHero hero = new SuperHero(); // No "With" construct hero.Defend("Laura Jones");
hero = null ; // Free the object if (hero == null) Object obj = new SuperHero(); // Mark object for quick disposal | ||||||||||
| |||||||||||
Structure StudentRecord Dim stu As StudentRecord = New StudentRecord("Bob", 3.5) | struct StudentRecord { StudentRecord stu = new StudentRecord("Bob", 3.5f); | ||||||||||
| |||||||||||
Private _size As Integer foo.Size += 1 | private int _size;
| ||||||||||
| |||||||||||
Delegate Sub MsgArrivedEventHandler(ByVal message As String) Event MsgArrivedEvent As MsgArrivedEventHandler ' or to define an event which declares a delegate implicitly AddHandler MsgArrivedEvent, AddressOf My_MsgArrivedCallback Imports System.Windows.Forms Dim WithEvents MyButton As Button ' WithEvents can't be used on local variable Private Sub MyButton_Click(ByVal sender As System.Object, _ | delegate void MsgArrivedEventHandler(string message); event MsgArrivedEventHandler MsgArrivedEvent; // Delegates must be used with events in C#
Button MyButton = new Button(); private void MyButton_Click(object sender, System.EventArgs e) { | ||||||||||
| |||||||||||
Console.Write("What's your name? ") | Console.Write("What's your name? ");
| ||||||||||
| |||||||||||
Imports System.IO ' Write out to text file ' Read all lines from text file ' Write out to binary file ' Read from binary file | using System.IO; // Write out to text file // Read all lines from text file // Write out to binary file // Read from binary file | ||||||||||
Wednesday, December 31, 2008
Microsoft .Net Programming
Manage View State In Session
'Manage View state in Session
Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object)
Dim VSKey As String
'String that will hold the Unique Key
'used to reference this ViewState data
'Create the key based on the SessionID, on the Request.RawUrl
'and on the Ticks representated by the exact time
'while the page is being saved
If Request.Form("__VIEWSTATE_KEY") Is Nothing Then
VSKey = "VIEWSTATE_" & MyBase.Session.SessionID & "_" & _
Request.RawUrl & "_" & Date.Now.Ticks.ToString
Else
VSKey = Request.Form("__VIEWSTATE_KEY")
End If
'Store the viewstate in the Session.
Session(VSKey) = viewState
'Register a HiddenField on the Page,
'that contains ONLY the UniqueKey generated.
'With this, we'll be able to find with ViewState
'is from this page, by retrieving these value.
ClientScript.RegisterHiddenField("__VIEWSTATE_KEY", VSKey)
End Sub
Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object
Dim VSKey As String 'ViewState UniqueKey
VSKey = Request.Form("__VIEWSTATE_KEY")
'Request the Key from the page and validade it.
If Not VSKey.StartsWith("VIEWSTATE_") Then
Throw New Exception("Invalid VIEWSTATE Key: " & VSKey)
End If
Return Session(VSKey)
End Function
Monday, December 29, 2008
Window And Element Viewport Size (Width and Height) With Java-script
var viewportwidth;
var viewportheight;
/*the more standards compliant browsers (mozilla/netscape/opera/IE7)
use window.innerWidth and window.innerHeight*/
if (typeof window.innerWidth != 'undefined')
{
viewportwidth = window.innerWidth,
viewportheight = window.innerHeight
}
/*IE6 in standards compliant mode
(i.e. with a valid doctype as the first line in the document)*/
else if (typeof document.documentElement != 'undefined'
&& typeof document.documentElement.clientWidth !=
'undefined' && document.documentElement.clientWidth != 0)
{
viewportwidth = document.documentElement.clientWidth,
viewportheight = document.documentElement.clientHeight
}
// older versions of IE
else
{
viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
viewportheight = document.getElementsByTagName('body')[0].clientHeight
}
function getElementWith(el)
{
if (el){
/*the more standards compliant browsers (mozilla/netscape/opera/IE7)
use window.innerWidth and window.innerHeight*/
if (typeof el.innerWidth != 'undefined')
{
return el.innerWidth
}
/*IE6 in standards compliant mode
(i.e. with a valid doctype as the first line in the document)*/
else if (typeof el != 'undefined'
&& typeof el.clientWidth !=
'undefined' && el.clientWidth != 0)
{
return el.clientWidth
}
// older versions of IE
else
{
return el.clientWidth
}
}
}
</script>
Sunday, December 28, 2008
Fibonacci Numbers
In mathematics, the Fibonacci numbers are a sequence of numbers named after Leonardo of Pisa, known as Fibonacci (a contraction of filius Bonaccio, "son of Bonaccio"). Fibonacci's 1202 book Liber Abaci introduced the sequence to Western European mathematics, although the sequence had been previously described in Indian mathematics.
The first number of the That is, after two starting values, each number is the sum of the two preceding numbers. The first Fibonacci numbers (sequence A000045 in OEIS), also denoted as Fn, for n = 0, 1, 2, … ,20 are:
F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
Every 3rd number of the sequence is even and more generally, every kth number of the sequence is a multiple of Fk.
The sequence extended to negative index n satisfies Fn = Fn−1 + Fn−2 for all integers n, and F−n = (−1)n+1Fn:
sequence is 0, the second number is 1, and each subsequent number is equal to the sum of the previous two numbers of the sequence itself, yielding the sequence 0, 1, 1, 2, 3, 5, 8, etc. In mathematical terms, it is defined by the following recurrence relation
:

