Monday, July 25, 2011

Monday 7/25/11


using System;
using System.Collections.Generic;
using System.Text;

namespace UsingParams
{
public class Tester
{
static void Main()
{
Tester t = new Tester();
//passes numbers
t.DisplayVals(5, 6, 7, 8);
int[] explicitArray = new int[6] { 1, 2, 3, 4, 5, 6 };
//passes array
t.DisplayVals(explicitArray);
}

public void DisplayVals(params int[] intVals)
{
foreach (int i in intVals)
{
Console.WriteLine("DisplayVals {0}", i);
}
}
}
}




using System;
using System.Collections.Generic;
using System.Text;

namespace RectangularArray
{
public class Tester
{
static void Main()
{
const int rows = 5;
const int columns = 3;

//declare a 4x3 integer array
int[,] rectangularArray = new int[rows, columns];

//populate the array
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
rectangularArray[i, j] = i + j;
}
} // end populate

// report the contents of the array
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
Console.Write(" rectangularArray[{0},{1}] = {2} ", i, j, rectangularArray[i, j]);
}
Console.WriteLine("");
}
}
} // end class Tester
} //end namespace




using System;
using System.Collections.Generic;
using System.Text;

namespace InitializingMultiDimensionalArray
{
public class Tester
{
static void Main()
{
const int rows = 4;
const int columns = 3;

//imply a 4X3 array
int[,] rectangularArray =
{
{0,1,2}, {3,4,5}, {6,7,8}, {9,10,11}
};

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
Console.WriteLine("rectangularArray[{0},{1}] = {2}", i, j, rectangularArray[i, j]);
}
}
}
}
}




using System;
using System.Collections.Generic;
using System.Text;


//a jagged array is an array of arrays
namespace JaggedArray
{
public class Tester
{
static void Main()
{
const int rows = 4;

//declare the jagged array as 4 rows high
//notice that the second dimension is not specified
int[][] jaggedArray = new int[rows][];

//the first row has five elements
jaggedArray[0] = new int[5];

//a row with 2 elements
jaggedArray[1] = new int[2];

//a row with 3 elements
jaggedArray[2] = new int[3];

//the last row has 5 elements
jaggedArray[3] = new int[5];

//fill some (but not all) elements of the rows
jaggedArray[0][3] = 15;
jaggedArray[1][1] = 12;
jaggedArray[2][1] = 9;
jaggedArray[2][2] = 145;
jaggedArray[3][0] = 10;
jaggedArray[3][1] = 14;
jaggedArray[3][2] = 15;
jaggedArray[3][3] = 15993;
jaggedArray[3][4] = 15;

for (int i = 0; i < 5; i++)
{
Console.WriteLine("jaggedArray[0][{0}] = {1}", i, jaggedArray[0][i]);
}

for (int i = 0; i < 2; i++)
{
Console.WriteLine("jaggedArray[1][{0}] = {1}", i, jaggedArray[1][i]);
}

for (int i = 0; i < 3; i++)
{
Console.WriteLine("jaggedArray[2][{0}] = {1}", i, jaggedArray[2][i]);
}

for (int i = 0; i < 5; i++)
{
Console.WriteLine("jaggedArray[3][{0}] = {1}", i, jaggedArray[3][i]);
}
}
}
}




using System;
using System.Collections.Generic;
using System.Text;

namespace SettingArrayBounds
{
public class SettingArrayBounds
{
public static void CreateArrayWithBounds()
{
//creates and initializes a multidimensional array of type string
int[] lengthsArray = new int[2] {3,5};
int[] boundsArray = new int[2] {2,3};
Array multiDimensionalArray = Array.CreateInstance(typeof(String), lengthsArray, boundsArray);

//displays the lower bounds and the upper bounds of each dimension
Console.WriteLine("Bounds:\tLower\tUpper");
for(int i = 0; i < multiDimensionalArray.Rank; i++)
{
Console.WriteLine("{0}:\t{1}\t{2}", i, multiDimensionalArray.GetLowerBound(i), multiDimensionalArray.GetUpperBound(i));
}
}
static void Main()
{
SettingArrayBounds.CreateArrayWithBounds();
}
}
}




using System;

class MainClass
{
public static void Main() {

int ivar;
double dvar;

ivar = 100;
dvar = 100.0;

Console.WriteLine("original value of ivar: " + ivar);
Console.WriteLine("original value of dvar: " + dvar);

Console.WriteLine();

//now divide both by 3

ivar = ivar / 3;
dvar = dvar / 3.0;

Console.WriteLine("ivar after division: " + ivar);
Console.WriteLine("dvar after division: " + dvar);
}
}




using System;

class MainClass
{
public static void Main()
{
uint value1 = 312;
byte value2 = (byte)value1;
Console.WriteLine("value2: {0}", value2);
}
}




namespace CompanyName
{
namespace UserInterface // nested namespace
{
public class MyClass
{
public void Test()
{
System.Console.WriteLine("userInterface Test()");
}
}
}
}

namespace CompanyName.DBAccess // nested namespace using dot
{
public class MyClass
{
public void Test()
{
System.Console.WriteLine("DatabaseAccess Test()");

}
}
}

class MainClass
{
public static void Main()
{
CompanyName.UserInterface.MyClass myUI = new CompanyName.UserInterface.MyClass();

CompanyName.DBAccess.MyClass myDB = new CompanyName.DBAccess.MyClass();

CompanyName.DBAccess.MyClass myMT = new CompanyName.DBAccess.MyClass();

myUI.Test();
myDB.Test();
myMT.Test();
}
}





using System;
using System.Collections.Generic;
using System.Text;

namespace ConvertingArrays
{
//create an object we can store in the array
public class Employee
{
//a simple class to store in the array
public Employee(int empID)
{
this.empID = empID;
}

public override string ToString()
{
string rs = empID.ToString() + " emp. ID";
return rs;
}
private int empID;
}

//This method take an array of objects. We'll pass in an array of Employees and then an array of strings.
//The conversion is implicit since both Employee and string derive (ultimately) from object.
public class Tester
{

public static void PrintArray(object[] theArray)
{
Console.WriteLine("Contents of the Array {0}", theArray.ToString());
//Walk through the array and print the values
foreach (object obj in theArray)
{
Console.WriteLine("Value: {0}", obj);
}
} //end printArray

static void Main()
{
//make an array of Employee objects
Employee[] myEmployeeArray = new Employee[5];
//initialize each Employee's value
for (int i = 0; i < 5; i++)
{
myEmployeeArray[i] = new Employee(i + 5);
}

//display the values
PrintArray(myEmployeeArray);

//create an array of two strings
string[] array = { "hello", "word" };

//print the value of the strings
PrintArray(array);
}
}
}




using System;
using System.Collections.Generic;
using System.Text;

namespace ArraySortAndRevers
{
public class Tester
{
public static void PrintMyArray(object[] theArray)
{
foreach (object obj in theArray)
{
Console.WriteLine("Value: {0}", obj);
}
Console.WriteLine("\n");
}

static void Main()
{
string[] myArray = { "Who", "is", "Douglas", "Adams" };

PrintMyArray(myArray);
Array.Reverse(myArray);
PrintMyArray(myArray);

String[] myOtherArray =
{
"We", "Hold", "These", "Truths", "To", "Be", "Self", "Evident"
};

PrintMyArray(myOtherArray);
//sorts alphabetically
Array.Sort(myOtherArray);
PrintMyArray(myOtherArray);
}
}
}




using System;
using System.Collections.Generic;
using System.Text;

namespace SimpleIndexer
{
//a simplified ListBox control
public class ListBoxTest
{
private string[] strings;
private int ctr = 0;

//initialize the listbox with strings
public ListBoxTest(params string[] initialStrings)
{
//allocate space for the strings
strings = new String[256];

//copy the strings passed in to the constructor
foreach (string s in initialStrings)
{
strings[ctr++] = s;
}
}

//add a single string ot the end fo the listbox
public void Add(string theString)
{
if (ctr >= strings.Length)
{
//handle bad index
}
else
{
strings[ctr++] = theString;
}
}

//allow array-like access

public string this[int index]
{
get
{
if (index < 0 || index >= strings.Length)
{
//handle bad index
}
return strings[index];
}
set
{
//add only through the add method
if (index >= ctr)
{
//handle errors
}
else
{
strings[index] = value;
}
}
}

//publish how many strings you hold
public int GetNumEntries()
{
return ctr;
}
}

public class Tester
{
static void Main()
{
//create a new listbox and initialize
ListBoxTest lbt = new ListBoxTest("Hello", "World");

//add a few strings
lbt.Add("Who");
lbt.Add("Is");
lbt.Add("Douglas");
lbt.Add("Adams");

//test the access
string subst = "Universe";
lbt[1] = subst;

Console.WriteLine(lbt.GetNumEntries());
//access all the strings
for (int i = 0; i < lbt.GetNumEntries(); i++)
{
Console.WriteLine("lbt[{0}]: {1}", i, lbt[i]);
}
}
}
}




using System;
using System.Collections.Generic;
using System.Text;

namespace OverloadedIndexer
{
//a simplified ListBoxTest
public class ListBoxTest
{
private string[] strings;
private int ctr = 0;

//initialize the listbox with strings
public ListBoxTest(params string[] initialStrings)
{
//allocate space for the strings
strings = new String[256];
//copy the strings passed in to the constructor
foreach (string s in initialStrings)
{
strings[ctr++] = s;
}
}

//add a single string to the end of the listbox
public void Add(string theString)
{
strings[ctr] = theString;
ctr++;
}

//allow array-like access
public string this[int index]
{
get
{
if (index < 0 || index >= strings.Length)
{
//handle bad index
}
return strings[index];
}
set
{
strings[index] = value;
}
}

private int findString(string searchString)
{
for (int i = 0; i < strings.Length; i++)
{
if (strings[i].StartsWith(searchString))
{
return i;
}
}
return -1;
}

//index on string
public string this[string index]
{
get
{
if (index.Length == 0)
{
//handle bad index
}

return this[findString(index)];
}
set
{
strings[findString(index)] = value;
}
}

//publish how many strings you hold
public int GetNumEntries()
{
return ctr;
}
}

public class Tester
{
static void Main()
{
//create a new listbox and initialize
ListBoxTest lbt = new ListBoxTest("Hello", "World");

//add a few strings
lbt.Add("Who");
lbt.Add("Is");
lbt.Add("Douglas");
lbt.Add("Adams");

//test the access
string subst = "Universe";
lbt[1] = subst;
lbt["Hel"] = "GoodBye";
//access al the strings
for (int i = 0; i < lbt.GetNumEntries(); i++)
{
Console.WriteLine("lbt[{0}]:{1}", i, lbt[i]);
}//end for
}//end main
}
}




<?php
$anemail = "lee@babinplanet.ca";
$thetoken = strtok($anemail, "@");
while($thetoken){
echo $thetoken . "
";
$thetoken = strtok("@");
}
?>





<?php
$astring = "Hello World";
echo strrev($astring);
?>


<?php
//the value passed to use by a customer who is signing up
$submittedpass = "myPass";
//before we insert into the dtabase, we simply lowercase teh submittal.
$newpass = strtolower($submittedpass);

echo $newpass;
?>





<?php
$astring = "hello world";
echo ucfirst($astring);
echo "
";
echo ucwords($astring);
?>




<div style = "padding:4px; line-height:1.3; color:blue; font-family: consolas, arial;">
$blankspaceallaround = "somepassword";
//this would result in all blank spaces being removed.
echo trim($blankspaceallaround) . "
";
//this would result in only the blank space at the beginning being trimmed
echo ltrim($blankspaceallaround) . "
";
//and as you can imagine, only the blank space at the end would be trimmed here
echo rtrim($blankspaceallaround) . "
";
?>

No comments: