Posts

Showing posts from February, 2010

How to hide generated javascript file in visual studio code

In this post I am going to show you a cool trick in visual studio code. Let's suppose you are writing typescript code in visual studio code and you want to hide generated javascript code.

Create .vscode folder in your folder and then create settings.json file and paste following code

{ "files.exclude": { "node_modules":true, "**/.git": true, "**/.DS_Store": true, "**/*.js.map": true, "**/*.js": {"when": "$(basename).ts"}     } }

How To create your own DynamicObject

In this post i will show how to create your own dyanamic object in c# 4.0.Create a class and Inherit it from DyanicObject (Provides a base class for specifying dynamic behavior at run time. This class must be inherited from; you cannot instantiate it directly).It has following virtual method that you must override to add dynamic behavior to your class. // Summary:// Returns the enumeration of all dynamic member names.//// Returns:// The list of dynamic member names.publicvirtual IEnumerable<string> GetDynamicMemberNames(); //// Summary:// The provided MetaObject will dispatch to the Dynamic virtual methods. The// object can be encapsulated inside of another MetaObject to provide custom// behavior for individual actions.//// Parameters:// parameter:// The expression representing the MetaObject to dispatch to the Dynamic virtual// methods.//// Returns:// The object of the System.Dynamic.DynamicMetaObject type.publicvirtual DynamicMetaObje…

ExpandoObject and Dynamic Programming in c# 4.0

ExpandoObject is a new Object in the .NET Framework that allows you to add and remove properties, methods, and events at runtime just like JavaScript Object. The following example demonstrates how to add two new values and a method in c# and Javascript. C# Code JavaScript Code using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Dynamic; namespace CSharpDemo { class Program { staticvoid Main(string[] args) { dynamic customer = new ExpandoObject(); customer.City = "Gurgaon"; customer.Country = "India"; customer.FullAddress = new Action(() => Console.WriteLine( customer.City + " " + customer.Country )); customer.FullAddress(); Console.ReadKey(); } } } function GetCusomer() { var customer = new Object(); …

Named and Optional Parameters

Named parameters allow you to pass parameters into a function in any order. To use a named parameter, simply specify the parameter name followed by a colon and then the value you are passing into a function. The following code illustrates passing the named parameter. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CSharpDemo { class Program { staticvoid Main(string[] args) { //Calling mehtod without named parameter AddNumbers(1, 2, 3); //Calling method with named parameter AddNumbers(c: 3, b: 2, a: 4,d:10); Console.ReadLine(); } publicstaticvoid AddNumbers(int a, int b, int c, int d = 0) { int sum = a + b + c + d; Console.WriteLine("Sum={0}", sum); } } } There some rules you need to be aware of when working with named parameters. Non-optional parameters must be declared …

How to create xml from Collection using Linq

Image
In this post i am going to show you how to create xml from database(for demo purpose i have created a List of person). using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; namespace Linq2XML { publicclass Program { staticvoid Main(string[] args) { var xml = new XDocument(new XComment("Your Comment"), new XElement("Persons", from c innew Person().GetList() where c.FirstName.StartsWith("B") select new XElement("Person", new XAttribute("FirstName", c.FirstName), new XAttribute("LastName", c.LastName)))); Console.WriteLine(xml); Console.ReadLine(); } } publicclass Person { publicint ID { get; set; } publicstring FirstName { get; set; } publicstring LastName { get; set; } public List<P…

Autocomplete using Jquery

Image
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><scriptsrc="Scripts/jquery-1.3.2.js"type="text/javascript"></script> <title></title> <style type="text/css"> .listbox { position: relative; left: 10px; margin: 10px; width: 200px; background-color: #000; color: #fff; border: 2px solid #000; } .nameslist { margin: 0px; padding: 0px; list-style: none; } .hover { background-color: Teal; color: b…

LINQ2SQL

Image
In this post i will show mapping of Linq query with SQL query <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title></title><styletype="text/css"> .style1 { width: 100%; } </style></head><body><formid="form1"runat="server"><div></div><tableclass="style1"><tr><td><asp:ButtonID="btnSelect"runat="server"Text="Select Statement"OnClick="btnSelect_Click"/></td><tdrowspan="9"><asp:TextBoxID="txtOutput"runat="server"TextMode="…

Difference between "throw" and "throw ex" in .NET

Diffrence between throw and throw ex In this post i will show what is diffrence between throw and throw ex. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ExceptionGotcha { publicclass Foo { /// <summary>/// Method that throw the exception./// </summary>publicstaticvoid MethodWithAttitude() { thrownew ApplicationException(); } } class Program { /// <summary>/// /// </summary>staticvoid Method() { try { Foo.MethodWithAttitude(); } catch (Exception ex) { throw ex; } } staticvoid Main(string[] args) { Method(); } } } From the above code it is clear that exception is in the Foo class but if you execute this code then it will show exception in Program(Method) class.Where is the problem?.…

How To Create Assembly with more than One Module

Image
In this post i will show how to create assembly which contains more than one module. Create below files in the same folder Bar.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Module { public class Bar { public override string ToString() { return "Hi from Bar"; } } } Foo.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Module { class Foo { static void Main(string[] args) { Console.WriteLine("Hello from Foo"); Bar obj = new Bar(); Console.WriteLine(obj.ToString()); Console.ReadLine(); } } } Now go to visual studio command prompt and change to the source file and execute the following command