Wednesday, July 20, 2011

The Roles of Classes in Object-oriented programming

It’s important to remember that although all classes are created in more or less the same way

in your code, they can serve different logical roles. Here are the three most common examples:

Classes can model real-world entities. For example, many introductory books teach

object-oriented programming using a Customer object or an Invoice object. These

objects allow you to manipulate data, and they directly correspond to an actual thing in

the real world.

Classes can serve as useful programming abstractions. For example, you might use a

Rectangle class to store width and height information, a FileBuffer class to represent a

segment of binary information from a file, or a WinMessage class to hold information

about a Windows message. These classes don’t need to correspond to tangible objects;

they are just a useful way to shuffle around related bits of information and functionality

in your code. Arguably, this is the most common type of class.

Classes can collect related functions. Some classes are just a collection of static methods

that you can use without needing to create an object instance. These helper classes are the

equivalent of a library of related functions, and might have names like GraphicsManipulator

or FileManagement. In some cases, a helper class is just a sloppy way to organize code

and represents a problem that should really be broken down into related objects. In

other cases, it’s a useful way to create a repository of simple routines that can be used in

a variety of ways.

Understanding the different roles of classes is crucial to being able to master object-oriented

development. When you create a class, you should decide how it fits into your grand development

plan, and make sure that you aren’t giving it more than one type of role. The more vague a

class is, the more it resembles a traditional block of code from a non-object-oriented program.

 

check directory Exists in Ftp Server

 

public bool FtpDirectoryExists(string directoryPath, string ftpUser, string ftpPassword)

        {

            bool IsExists = true;

            try

            {

                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(directoryPath);

                request.Credentials = new NetworkCredential(ftpUser, ftpPassword);

                request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;

 

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            }

            catch (WebException ex)

            {

                IsExists = false;

            }

            return IsExists;

        }

 

I have called this method as:
bool result = FtpActions.Default.FtpDirectoryExists( "ftp://domain.com/test",
                                                    txtUsername.Text, txtPassword.Text);

Sunday, July 17, 2011

check string exists in the enum and convert string to an Enum

/// <summary>

/// File Type Enum - The extensions that our application

/// support to work.. check at the time of work with the

/// file.. e.g. read the file and save to database etc.

/// </summary>

 

enum FileTypes

{

     BMP ,

     JPG ,

     JPEG,

     GIF,

    TIFF

}

 

string fileExtension = Path.GetExtension(filePath).ToUpper();

       

//check string exists in the enum.. it will match the case also..

if (Enum.IsDefined(typeof(FileTypes),fileExtension.Trim('.')))

{

    //convert string to enum

    FileTypes  c = (FileTypes) Enum.Parse(typeof(FileTypes), "MOV", true);

}

 

Note: Enum.IsDefined() doesn't offer the ignoreCase parameter. If you don't

know whether the casing is right, it seems the only way to do the conversion is using

the Parse method and catching the ArgumentException.

Saturday, July 9, 2011

Get textbox server control value using jquery in asp.net

Retrieve the server control id in the java script (jQuery code) and do as usual
jQuery code for getting the value at the ASPx page.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="scripts/jquery-1.6.1.min.js" type="text/javascript"></script>
    <script type = "text/javascript">
        $(document).ready(function () {

            $("#<%= btnSend.ClientID %>").click(function () {
                alert($("#<%= txtInfo.ClientID %>").val());
                return true;
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div style=" margin : 0px auto">
        <asp:TextBox ID="txtInfo" runat="server"></asp:TextBox>
        <
asp:Button ID="btnSend"  runat="server" Text="Send" />
    </div>
    </form>
</body>
</html>


Thursday, July 7, 2011

make a XtraGrid cell read-only on condition

using System;
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing;  
using System.Text; 
using System.Windows.Forms; 
using DevExpress.XtraGrid.Views.Grid; 
namespace ReturnForm
 {
      public partial class Form1 : Form {
          public ReturnForm()   
          {
              InitializeComponent();
          }
          private void Form1_Load(object sender, EventArgs e)
          {
              FillDataSource();
          } 
          private void FillDataSource()
          {
              dtProducts = dsProducts.dtProductsTableAdapter.Fill( 
                                                this.dsProducts.dtProducts);
          }
          private bool IsShipToUSCanada(GridView view, int row)
          {
              try
            {
                    string val = Convert.ToString(
                                view.GetRowCellValue(row, "ShipCountry"));
                  return (val == "US" || val == "Canada");
              }
             catch( )
            {
                  return false;
            }
      }
         private void grvProducts_ShowingEditor(object sender, CancelEventArgs e)
        {
              if(gridView1.FocusedColumn.FieldName == "IsFreeShipping" 
                 && IsShipToUSCanada(grvProducts, grvProducts.FocusedRowHandle))
                  e.Cancel = true;
       }
        private void grvProducts_RowCellStyle(object sender,
                      DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        { 
             if(e.Column.FieldName == "IsFreeShipping" 
                               && IsShipToUSCanada(grvProducts, e.RowHandle))
            {
                  e.Appearance.BackColor = Color.LightGray; 
             } 
         }
      }
  }

Tuesday, July 5, 2011

Extending jQuery functions, creating plugins

The jQuery wrapper function provides a large number of useful methods we’ll use
again and again in these pages. But no library can anticipate everyone’s needs. It
could be argued that no library should even try to anticipate every possible need;
doing so could result in a large, clunky mass of code that contains little-used features
that merely serve to gum up the works!

We could write our own functions to fill in any gaps, but once we’ve been spoiled
by the jQuery way of doing things, we’ll find that doing things the old-fashioned way is
beyond tedious. By extending jQuery, we can use the powerful features it provides,
particularly in the area of element selection.

Moreover, enterprising jQuery users have extended jQuery with sets of useful functions
that are known as plugins.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="scripts/jquery-1.6.1.min.js" type="text/javascript" >
    </script>
    <script src="scripts/jquery.validate.js" type = "text/javascript">
    </
script>
    <script src ="scripts/jquery.validate-vsdoc.js" type ="text/javascript">
    </
script>
    <script type = "text/javascript">
       <!— Extends jQuery with function named makeRed -->
        $.fn.makeRed = function () {
            return this.each(function () {
                $(this).css({ backgroundColor: 'red' });
            });
        };

        $(function () {
            $("<p>Insert Me Somewhere</p>").insertAfter("#followme");

            $(function () {
                $("#ram").makeRed();
            });
        });
    </script>
</head>
<body>
<p id="followme">follow me!</p>
<div id="ram">Make me Red</div>
</body>
</html>

The jQuery wrapper means and does

When CSS was introduced to web technologies in order to separate design from content,

a way was needed to refer to groups of page elements from external style sheets.

The method developed was through the use of selectors, which concisely represent

elements based upon their type, attributes, or position within the HTML document.

Those familiar with XML might be reminded of XPath as a means to select elements

within an XML document. CSS selectors represent an equally powerful concept,

but are tuned for use within HTML pages, are a bit more concise, and are generally

considered easier to understand.

For example, the selector

p a

refers to the group of all links (<a> elements) that are nested inside a <p> element.

jQuery makes use of the same selectors, supporting not only the common selectors

currently used in CSS, but also some that may not yet be fully implemented by all

browsers, including some of the more powerful selectors defined in CSS3.

To collect a group of elements, we pass the selector to the jQuery function using

the simple syntax

$(selector)

or

jQuery(selector)

Although you may find the $() notation strange at first, most jQuery users quickly

become fond of its brevity. For example, to wrap the group of links nested inside any

<p> element, we can use the following:

$("p a")

The $() function (an alias for the jQuery() function) returns a special JavaScript

object containing an array of the DOM elements, in the order in which they are defined

within the document, that match the selector. This object possesses a large number of

useful predefined methods that can act on the collected group of elements.

In programming parlance, this type of construct is termed a wrapper because it

wraps the collected elements with extended functionality. We’ll use the term jQuery

wrapper or wrapped set to refer to this set of matched elements that can be operated on

with the methods defined by jQuery.