Wednesday, 28 September 2011

Difference between string and String

Answer simply nothing.....   String=System.String  dot net provide another name string for the convenience of the programmers who are basically from c and c++.   short= System.Int16 , int=System.Int32.

Diffference between ViewBag and ViewData in MVC

                 I am proud to write the first article in MVC.   I am brand new to MVC at this date.  I saw the ways to convey the message between the control to the view,  the easiest ways are viewbag and viewdata both are nearly do the same in the difference way.
ex:
    public ActionResult Index()
        {
              ViewBag.Greeting="Hello World";
              ViewData["Greeting"]="Hello World";
          }

in the view

<body>
  <h1><%=ViewBag.Greeting%></h1>
   <h2.<%=ViewData["Greeting"]%></h1>
</body>

1.  The simple difference is we use a string in the ViewData for indexing purpose that is called magic string but in the ViewBag method view simply use the property similar to the class.

2.  For the both cases the intelligence is not provided.  So there may be chances to make a manual mistake to indicating the variables.
            * If you made mistake in the ViewData  ex ViewData["Greting"] instead of Greeting at the time the error is ignored and HTML is executed without any error.
             * But it is strictly notified in the ViewBag method.

3. In the ViewBag the data conversion is handled by itself no need to convert the data from object to the data type. But it must be defined in the ViewData.
              ex:
                  public ActionResult Index()
             {
                    List<string> namelist = new List<string>{"sugunthan","shamnugam","sundaram"};
                    ViewBag.list = namelist;
                    ViewData["list"] = namelist;
                    return View();
              }

In the View
               <ul>
                 <%foreach (var name in (List<int>) ViewData["list"]){ %>
                   <li><%=name %></li>
                   <%} %>
                   </ul>
                   <ul>
                   <%foreach (var name in ViewBag.list)
                    { %>
                    <li> <%=name %></li>
                    <% }%>
                   </ul>

If you made any mistake in the data conversion it make an error in the runtime.






Saturday, 17 September 2011

Cookies

     Cookies are created when a user's browser loads a particular website. The website sends information to the browser which then creates a text file. Every time the user goes back to the same website, the browser retrieves and sends this file to the website's server. Computer Cookies are created not just by the website the user is browsing but also by other websites that run ads, widgets, or other elements on the page being loaded. These cookies regulate how the ads appear or how the widgets and other elements function on the page.

Session Cookies:

     
Webpages have no memories. A user going from page to page will be treated by the website as a completely new visitor. Session cookies enable the website you are visiting to keep track of your movement from page to page so you don't get asked for the same information you've already given to the site. Cookies allow you to proceed through many pages of a site quickly and easily without having to authenticate or reprocess each new area you visit.
Session cookies allow users to be recognized within a website so any page changes or item or data selection you do is remembered from page to page. The most common example of this functionality is the shopping cart feature of any e-commerce site. When you visit one page of a catalog and select some items, the session cookie remembers your selection so your shopping cart will have the items you selected when you are ready to check out. Without session cookies, if you click CHECKOUT, the new page does not recognize your past activities on prior pages and your shopping cart will always be empty.



Tuesday, 13 September 2011

Trim Functions in JavaScript

Three type of trims are available
1. Trim (Left and Right)
2. Right Trim
3. Left Trim

The function for these trims are
Trim
String.prototype.trim = function()
{
return this.replace(/^\s+|\s+$/g,"");
}
Left Trim
String.prototype.ltrim = function()
 {
return this.replace(/^\s+/,"");
}

Right Trim
String.prototype.rtrim = function()
 {
return this.replace(/\s+$/,"");
}


Simply places these function into your page or external javascript file but it must be availble where you will use these functions.

We see how to use these functions

var sampleString="  this is my blog   ";
alert("$"+sampleString.trim()+"$");
alert("$"+sampleString.ltrim()+"$");
alert("$"+sampleString.rtrim()+"$");

                                             

Friday, 2 September 2011

Concrete class and Abstract class

Concrete class is nothing but normal class, we can use as a base class or may not.Not compulsory, it can't contain abstract methods.we can create object and work with this class.

class Sample
{
public void MyMethod()
{..............}
}


Abstract class:- Abstract class is class which declared with a keyword Abstract,
Must be used as a base class.
Only intension to declare a abstract class is to use as a base class,that is we can't create object of this class like concrete class.
It can contain abstract methods as well as concrete(normal) methods.

public abstract class MyClass
{
public void MyMethod()
{..............}

public abstract TestMethod()
{
.........
}
}