vb and c# programming basics. overview basic operations string processing date processing control...

38
VB and C# Programming Basics

Upload: amberly-parsons

Post on 03-Jan-2016

221 views

Category:

Documents


1 download

TRANSCRIPT

VB and C# Programming Basics

Overview

• Basic operations

• String processing

• Date processing

• Control structures

• Functions and subroutines

Additional Documentation

• For extensive documentation in Microsoft .NET Development using VB or C#:– http://msdn.microsoft.com/en-us/library/dd642420.aspx

The System Namespace

• Contains fundamental classes, including String and DateTime

• System Namespace Documentation– http://msdn.microsoft.com/en-us/library/

system.aspx

String Processing

• Concatenation

• String length

• Substrings

• Changing case

• Replacing text

• Hyperlink to String class documentation

String concatenation

Note: C# is case sensitive. Length property begins with upper-case “L”

Returning string lengths

Substring method of String class.

1st argument = start position

2nd (optional) argument = length of substring

Indexof method of String class

Returns the 0-based index position of the substring in the searched string.

Syntax:

searchedString.Indexof(stringToFind)

searchedString.Indexof (stringToFind, startPos)

Changing case

Replacing text in a string

The DateTime Class

• Provides many useful properties and methods related to dates and times

• Hyperlink to DateTime documentation

Date Manipulation

Lots of methods and properties for the DateTime class!

Additional Notes

• In C#, use DateTime.Parse static method to convert a string to date. Not necessary in VB.– Try changing from DateTime.Now to a string

representing a date and time

• To make the view source more readable, you can insert line breaks in your output string:– “\n” for C#

– ControlChars.CrLf for VB

Control Structures

• Selection– if – else– select case ----- switch

• Looping– for loops– while and do while loops

Syntax for if statements in C# is identical to what you know from Java

VB Select-Case like before

Note: C# switch statement allows case to include strings…different from Java, which requires integral values

for loop syntax similar to what you know of Java

Note: unlike Java, C# includes a foreach keyword in the core language.

while in C# is similar to what you know already in Java

Functions and Subroutines

• Signatures – return type, name, parameter list

• Calling functions and subroutines

• Making use of return values

Note: with VB .NET, explicit type declarations not necessary

Note the use of ToString method…converts numeric values to strings for text output.

Note that the int data type (and other primitive data types) provides the possibility of converting data to string via ToString(). In this sense it operates like a Java wrapper class.

Array Processing• Topics:

– Array declaration, instantiation, initialization– Single-dimensional arrays– Multi-dimensional arrays– Jagged arrays

Single Dimension Arrays C#Declaration in C# is strict, brackets must follow data type: int[] array1; NOT int array1[];

Looping through arrays can be done using Length property or via for each loop

Single Dimension Arrays VB

Multi Dimension Arrays C#

Multi-dimension array declaration and use in C# is unlike Java. With Java you are always declaring jagged arrays, but C# has a feature for multi-dimension arrays

Multi Dimension Arrays VB

Length vs. GetLength():

Length returns full array sizeGetLength(x) returns nbr elements in row x

Jagged Arrays C#

Jagged arrays are familiar to Java programmers

Jagged Arrays VB

Web Server Controls for Assignment #1

• TextBox – Two Useful Properties:

• Text – contains the string for user input

• Columns – specifies visible number of characters

• Button– Event processing via ONCLICK attribute in

ASP:Button tag

• Label– Static text display

Note initialization of server control properties in the Page_Load procedure

Association of click event with event-handling routine

Resulting HTML code

Resulting browser render

The web.config file

• Useful for deployment and configuration• Primarily for web administration• Can also be used for creating global application variables• XML file• If used, should be placed in application’s root folder• For debugging purposes, you should do the following……• In your web.config file, change the tag that says:

– <customErrors mode="RemoteOnly" /> • To

– <customErrors mode="Off"/>• This will enable compile errors in your code to be identified when

attempting to view the page in a browser