This sample application will teach how to backup a SQL database to a BAK file using C#. I hope someone will benefit from this.
ScreenShot
Saturday, August 28, 2010
Thursday, August 12, 2010
c# Array Part 2 (Using reference types)
Using arrays in c#, you can also declare arrays of custom types. In our example let’s start with a Customer class, having two constructors, 3 properties (FirstName, LastName, ContactNumber), and an override of ToString() method of the Object class.
Inside our main method, let’s try to use our custom type with arrays.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArraysPart2cs
{
class Customer
{
#region -[Private Member Fields]-
private string FirstName_;
private string LastName_;
private string ContactNumber_;
#endregion
//our first constructor
public Customer()
{
}
//our second constructor
public Customer(string _FirstName, string _LastName, string _ContactNumber)
{
this.FirstName_ = _FirstName;
this.LastName_ = _LastName;
this.ContactNumber_ = _ContactNumber;
}
#region -[Properties]-
public string FirstName
{
get { return this.FirstName_; }
set { this.FirstName_ = value; }
}
public string LastName
{
get { return this.LastName_; }
set { this.LastName_ = value; }
}
public string ContactNumber
{
get { return this.ContactNumber_; }
set { this.ContactNumber_ = value; }
}
#endregion
public override string ToString()
{
string FullName = string.Concat(this.LastName_, " ", this.FirstName_);
return string.Format("You are : {0}, your contact number: {1}", FullName, this.ContactNumber_);
}
}
}
Inside our main method, let’s try to use our custom type with arrays.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArraysPart2cs
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Arrays Part 2";
//so lets declare an array of customer having 2 elements
Customer[] customer = new Customer[2];
//lets try a short method
customer[0] = new Customer("John Felix", "Cruz", "+639083069011");
Console.WriteLine(customer[0].ToString());
//lets try a long method
customer[1] = new Customer();
customer[1].FirstName = "Jin";
customer[1].LastName = "Zalzos";
customer[1].ContactNumber = "+639083069011";
Console.WriteLine(customer[1].ToString());
//you may also use array initializer with custom types
Customer[] anotherCustomer = {new Customer("Rhian","Calzado","+63908306911"),
new Customer("B. Togie", "Calzado", "+6301233655")};
Console.WriteLine(anotherCustomer[0]);
Console.ReadKey();
}
}
}
Thursday, August 5, 2010
c# Array Part I
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArraysPartOne
{
class Program
{
static void Main(string[] args)
{
/*
* sometimes programmers needs to use
* multiple objects of the same type(value types or reference types)
* so we use arrays.
* Definition of array -> is a data structure that contains a number of
* elements of the same type. (Professional C# 2008 Wrox)
*/
/*
* how to declare an array?
* so there are many ways on how to declare an array
* just try to look below
*/
int[] _FirstArray = new int[5];
int[] _SecondArray = new int[2] {4,5};
int[] _ThirdArray = new int[] { 4, 5, 6 };
int[] _FourthArray = { 6,7,8};
// end of declaring or initilizing an array
//start of our sample array
try
{
Console.WriteLine("Array Sample Part I");
int intInputNumber =0;
int intCounter =0;
Console.WriteLine("How many number to want to input");
intInputNumber = int.Parse (Console.ReadLine());
int [] _UserArray = new int[intInputNumber];
do
{
Console.WriteLine(
string.Format
("Input numbers at index[{0}]",intCounter ));
_UserArray[intCounter] = int.Parse(Console.ReadLine());
intCounter++;
}while (intCounter < intInputNumber);
//iterate throught the elements in the array ,
//i'll be using for and foreach loop
Console.WriteLine("Iterate elements using for loop");
for (int i = 0;
i < _UserArray.Length; i++)
{
string strMessage =
string.Format
("Array Index [{0}] = [{1}]", i, _UserArray[i]);
Console.WriteLine(strMessage + Environment.NewLine);
}
Console.WriteLine("Iterate elements using foreach loop");
StringBuilder result = new StringBuilder();
foreach (int IndexValue in _UserArray)
{
result.Append(IndexValue);
result.Append(",");
}
Console.WriteLine(result);
Console.WriteLine("Do you want to sort the array?[y/n]");
string ans = Console.ReadLine();
if (ans.ToUpper().Equals("Y"))
{
Array.Sort(_UserArray);
}
//iterate throught the elements
//in the array ,i'll be using for and foreach loop
Console.WriteLine("Iterate elements using for loop");
for (int i = 0; i < _UserArray.Length; i++)
{
string strMessage =
string.Format
("Array Index [{0}] = [{1}]",
i, _UserArray[i]);
Console.WriteLine
(strMessage + Environment.NewLine);
}
Console.ReadKey();
}
//if you are a wrong index value,
//IndexOutOfRangeException will be thrown
catch (IndexOutOfRangeException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Subscribe to:
Posts (Atom)


