Monday, July 12, 2010

Input & Output's in .Net

FileStream f = File.Create(@"c:\myDataintoFlatFile.dat");
StreamWriter st = new StreamWriter(f);
st.WriteLine("My name is Mahmoud Sayed ahmed");
st.WriteLine("I'm 24 years OLD");
st.WriteLine("I live in CA-USA :)");
st.Flush();
st.Close();


FileStream s = File.Open(@"c:\myDataintoFlatFile.dat", FileMode.Open);
StreamReader read = new StreamReader(s);
while (read.Peek() != -1)
{
string z = read.ReadLine();
if (z.Contains("CA"))
{
Console.WriteLine("CA Found..............");
break;
}
}

Saturday, July 3, 2010

Using IEnumerable and IEnumerator interfaces.

in this example I demonstrate how to use IEnumerable interface and the GetEnumerator function and how to use IEnumerator with Movenext(), Reset() and the Current property
in order to to loop through a class content using foreach ...

using System;
using System.Collections;

namespace ConsoleApplication5
{


class data
{
string name;
int age;

public data()
{

}
public data(string name, int age)
{

this.name = name;
this.age = age;
}

public string _name
{
get
{
return name;
}
}
public int _age
{
get
{
return age;
}
}

}
class people : IEnumerator, IEnumerable
{

public IEnumerator GetEnumerator()
{
return x.GetEnumerator();
}


internal int pos = 0;
data[] x = new data[4];

public people()
{

x[0] = new data("Ali", 32);
x[1] = new data("mohammed", 22);
x[2] = new data("wael", 24);
x[3] = new data("slah", 34);
}
public object Current
{
get
{
return x[pos];
}
}

public bool MoveNext()
{

if (pos <= x.Length)
{
++pos;
return true;

}
else
return false;
}

public void Reset()
{
Console.WriteLine("Reset...");
pos = 0;
}

}


class Program
{
static void Main(string[] args)
{

people p = new people();
foreach (data d in p)
{
Console.WriteLine(d._name + " " + d._age.ToString());
}
}
}
}

IComparable and IComparer

this example is about how to implement IComparable and IComparer interface,
so you can Sort your class members Ascending or Descending

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

namespace ConsoleApplication5
{


class data : IComparable
{
string name;
int age;

public data()
{

}
public data(string name, int age)
{

this.name = name;
this.age = age;
}

public string _name
{
get
{
return name;
}
}
public int _age
{
get
{
return age;
}
}
///////////////////use IComparable and CompareTo method
///////////////////to sort the members Descending
public int CompareTo(object obj)
{
data m = (data)obj;
if (m._age < this._age)
return 1;
else if (m._age > this._age)
return -1;
else
return 0;
}
//////////////use IComparer and Compare method
//////////////to sort the members Descending
public class sortDes : IComparer
{

int IComparer.Compare(object x, object y)
{
data m = (data)x;
data n = (data)y;
if (m._age < n._age)
return 1;
else if (m._age > n._age)
return -1;
else
return 0;
}

}


}

class Program
{
static void Main(string[] args)
{
data[] x = new data[4];
x[0] = new data("Ali", 10);
x[1] = new data("mohammed", 24);
x[2] = new data("wael", 3);
x[3] = new data("slah", 34);
// Array.Sort(x) //for will sort it Ascending
Array.Sort(x, new data.sortDes());
foreach (data d in x)
{
Console.WriteLine(d._name + " " + d._age.ToString());
}


}
}
}

Casting using TryParse, Convert , int.Parse()

using System.Convert, ToInt32() for example allow you to convert 19 different type to int.
in other hand using int.Parse(), accept string parameters only, and it's useful to be used with Console.readLine, but what if the user enter a textual data for example, "Ahmed" or "123Ali"
here tryParse would solve the issue.
in this example:
I created 2 variables (input) is the value I need to parse and (num) as an int to hold the integral parsed new value.

int num;
if (int.TryParse(input , out num))
{
if (num> 10)
Console.WriteLine(num.ToString() + ">10");
}
else
{
Console.WriteLine(num);
}

Resizing an array using Array.Resize()

in this example I tried to pass an array of nulls (int? [] ) to a function in order to remove all the null values then pass it back to the nullable array.

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int?[] x = new int?[] { 5, null, 3, null, 9, null, null, null, null, 4, null, 3, null, 6 };
Console.WriteLine("---------------------------Null holder array ");
foreach (int? element in x)
{
if(element.HasValue)
Console.WriteLine( element);
else
Console.WriteLine("NULL");
}
Console.WriteLine("my array length " +x.Length);
////I can use nullfree with out no more resize or copy in x array.
int?[] nullfree = function(ref x);
Array.Resize(ref x, nullfree.Length);
Array.Copy(nullfree, x, nullfree.Length);
Console.WriteLine("---------------------------New Null free array ");

foreach (int? element in x)
{
Console.WriteLine(element);
}
Console.WriteLine("my array new length " + x.Length);
}
static int?[] function(ref int?[] x)
{
int size = -1;
int num = 0;
int?[] nullfree = new int?[x.Length];
for (int i = 0; i <>(ref nullfree, size + 1);
return nullfree;
}
}
}