Padding character with string


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

namespace Padding
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "This is a text";
            char padChar = '.';

            Console.WriteLine(str.PadLeft(25, padChar));
            Console.WriteLine(str.PadLeft(2, padChar));

            Console.WriteLine(str.PadRight(25, padChar));
            Console.WriteLine(str.PadRight(2, padChar));

            str = "I am Mahedee hasan. Software Architect, Leadsoft Bangladesh Ltd";
            Console.WriteLine((str.PadRight(25, padChar)));

            Console.WriteLine((str.PadRight(25,padChar)).Substring(0,24)+"...");

            Console.ReadKey();
        }
    }
}


/* Output:

...........This is a text
This is a text
This is a text...........
This is a text
I am Mahedee hasan. Software Architect, Leadsoft Bangladesh Ltd
I am Mahedee hasan. Soft...

*/



Iterator Design Pattern with C#

Iterator pattern is a design pattern which is used to traverse aggregate object often called container and access container’s object without exposing it’s underlying representation. Iterator pattern decoupled algorithm from aggregate object or container. In some cases, algorithms are container specific. We often use collection in C# and then we traverse the collection without knowing it’s internal details. Collection is grouping of some object. Objects can be same type or different type. Collection in fact actively used iterator pattern.

UML_Iterator

The above figure is UML class diagram for Iterator Pattern. The main idea behind the iterator pattern is to take the responsibility of traversing container and put it to the iterator object. The iterator object will maintain the state of the iteration, keeping track of the current item and track for the next item to iterate.

Benefits:

  1. Access element of container without exposing it’s internal details.
  2. Provides a uniform interface for traversing different collection (aggregate object).
  3. Provides multiple simultaneous traversals in a collection.

Implementation:

Let’s come to the point. We are going to implement iterator design pattern. I am using here a collection for fruits item. The main actor in here:

IIterator – Interface to define Iterator (Concrete iterator class)

Iterator – Concrete iterator class which is used to iterate elements.

ICollection – Interface to define aggregate.

Collection – Concrete aggregate class.

I used FruitItem class as item class here.

Step 1: Create Item class.

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

namespace IteratorPattern
{
///
/// Item class
///

public class FruitItem
{
public string Id { get; set; }
public string Name { get; set; }
}

}

Step 2: Create IIterator Interface

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

namespace IteratorPattern
{
///
/// Interface for Iterator
///

public interface IIterator
{
FruitItem First();
FruitItem Next();
FruitItem CurrentItem { get; }
bool IsDone { get; }
}
}

Step 3: Create concrete Iterator class

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

namespace IteratorPattern
{
///
/// Concrete Iterator Class
///

public class Iterator : IIterator
{

private Collection collection;
private int current = 0;
private int step = 1;

public Iterator(Collection vCollection)
{

this.collection = vCollection;

}

public FruitItem First()
{
current = 0;
return (FruitItem)collection[current];
}

public FruitItem Next()
{
current ++;

if (!IsDone)
return (FruitItem)collection[current];
else
return null;
}

public bool IsDone
{
get { return current >= collection.Count; }
}

public FruitItem CurrentItem
{
get { return (FruitItem) collection[current]; }
}

// Gets or sets stepsize
public int Step
{
get { return step; }
set { step = value; }

}

}
}

Step 4: Create collection interface.

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

namespace IteratorPattern
{
///
/// Interface for Aggregate class
///

public interface ICollection
{
Iterator CreateIterator();
}
}

Step 5: Create concrete collection class.

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

namespace IteratorPattern
{
///
/// Concrete aggregate class
///

public class Collection : ICollection
{
private ArrayList lstFoodItem = new ArrayList();

public Iterator CreateIterator()
{
return new Iterator(this);
}

// Get counted items
public int Count
{
get
{
return lstFoodItem.Count;
}

}

// Indexer

public object this[int index]
{

get { return lstFoodItem[index]; }

set { lstFoodItem.Add(value); }

}
}
}

Step 6: Create client class

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

namespace IteratorPattern
{
class Program
{
public static void Main(string[] args)
{
Collection collection = new Collection();

collection[0] = new FruitItem() { Id = "1", Name = "Mango" };
collection[1] = new FruitItem() { Id = "2", Name = "Orange" };
collection[2] = new FruitItem() { Id = "3", Name = "Banana" };
collection[3] = new FruitItem() { Id = "4", Name = "Apple" };
collection[4] = new FruitItem() { Id = "5", Name = "Lichi" };
collection[5] = new FruitItem() { Id = "7", Name = "Tamarind" };

// Create iterator
Iterator iterator = new Iterator(collection);
Console.WriteLine("Items by iterating over collection");

for (FruitItem item = iterator.First(); !iterator.IsDone; item = iterator.Next())
{
Console.WriteLine(item.Name);
}

Console.ReadLine();
}
}
}

Output:

output

How to send email in ASP.net

ou have to configure SMTP server before sending email from your server. Hope you already know how to configure smtp server. Here is the simple code segment for sending email against a button click.


protected void btnSendMail_Click(object sender, EventArgs e)
{
      SmtpClient smtpClient = new SmtpClient();
      MailAddress fromAddress = new MailAddress("mahedee_hasan@leads-bd.com"); //Sender email address
      MailMessage _mail = new MailMessage();

     _mail.Attachments.Add(new Attachment(@"E:\Temp\attachment.pdf")); //Add dirctory of attachment

     //bool IsSuccess = false;
    try
    {
      _mail.From = fromAddress;
      _mail.To.Add("mahedee.hasan@gmail.com"); //Receiver email address
      _mail.CC.Add("mahedee.hasan@yahoo.com"); //Email address for cc
      _mail.Bcc.Add("mahedee.hasan@hotmail.com"); //Email address for bcc

      _mail.Subject = "Test Message"; //Email subject
      _mail.Body = "Dear concern, This is the test message from Mahedee"; //Email body
      _mail.IsBodyHtml = true; //Email body is in html format

       smtpClient.Host = "182.268.30.50"; //Name or IP address of the host used for SMTP transaction
       smtpClient.EnableSsl = false;
       //smtpClient.Port = 25; //25 is default port for smtp. You have to change port no if it is not used defualt         port
      smtpClient.Send(_mail); //Send mail
      //IsSuccess = true;
    }
    catch (Exception ex)
    {
       //IsSuccess = false;
    }
    finally
    {
     //
    }
}

Download sample :