Thursday, March 12, 2015

AngularJs set limit to decimal places

app.filter('setDecimal', function ($filter) {
    return function (input, places) {
        if (isNaN(input)) return input;
        // If we want 1 decimal place, we want to mult/div by 10
        // If we want 2 decimal places, we want to mult/div by 100, etc
        // So use the following to create that factor
        var factor = "1" + Array(+(places > 0 && places + 1)).join("0");
        return Math.round(input * factor) / factor;
    };
});

Wednesday, March 11, 2015

C# program that uses abstract class

using System;

abstract class Test
{
    public int _a;
    public abstract void A();
}

class Example1 : Test
{
    public override void A()
    {
 Console.WriteLine("Example1.A");
 base._a++;
    }
}

class Example2 : Test
{
    public override void A()
    {
 Console.WriteLine("Example2.A");
 base._a--;
    }
}

class Program
{
    static void Main()
    {
 // Reference Example1 through Test type.
 Test test1 = new Example1();
 test1.A();

 // Reference Example2 through Test type.
 Test test2 = new Example2();
 test2.A();
    }
}

Output

Example1.A
Example2.A

C# Abstract

An abstract method has no implementation. Its implementation logic is provided instead by classes that derive from it. We use an abstract class to create a base template for derived classes.

Tuesday, March 10, 2015

C# program that uses InsertRange

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
 List<int> a = new List<int>();
 a.Add(1);
 a.Add(2);
 a.Add(5);
 a.Add(6);

 // Contains:
 // 1
 // 2
 // 5
 // 6

 int[] b = new int[3];
 b[0] = 7;
 b[1] = 6;
 b[2] = 7;

 a.InsertRange(1, b);

 // Contains:
 // 1
 // 7 [inserted]
 // 6 [inserted]
 // 7 [inserted]
 // 2
 // 5
 // 6
 foreach (int i in a)
 {
     Console.WriteLine(i);
 }
    }
}

Output

1
7
6
7
2
5
6

C# list AddRange Method & C# program that uses AddRange

AddRange adds an entire collection of elements. It can replace tedious foreach-loops that repeatedly call Add on List. We can pass any IEnumerable collection to AddRange, not just an array or another List.

The term "range" simply means an IEnumerable collection.


using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
 List<int> a = new List<int>();
 a.Add(1);
 a.Add(2);
 a.Add(5);
 a.Add(6);

 // Contains:
 // 1
 // 2
 // 5
 // 6

 int[] b = new int[3];
 b[0] = 7;
 b[1] = 6;
 b[2] = 7;

 a.AddRange(b);

 // Contains:
 // 1
 // 2
 // 5
 // 6
 // 7 [added]
 // 6 [added]
 // 7 [added]
 foreach (int i in a)
 {
     Console.WriteLine(i);
 }
    }
}

Output

1
2
5
6
7
6
7




C# program that adds objects to List

using System.Collections.Generic;

class Program
{
    static void Main()
    {
 // Add three objects to a List.
 List<Test> list = new List<Test>();
 list.Add(new Test(1, 2));
 list.Add(new Test(3, 4));
 list.Add(new Test(5, 6));
    }

    class Test
    {
 int _a;
 int _b;
 public Test(int a, int b)
 {
     _a = a;
     _b = b;
 }
    };
}

C# List Add Method

Add places an element at the end of a List. It handles both value types and reference types. The Add instance method on the List type has internal logic that allows you to quickly add elements to the end of the collection.


using System.Collections.Generic;

class Program
{
    static void Main()
    {
 // Add first four numbers to the List.
 List<int> primes = new List<int>();
 primes.Add(2);
 primes.Add(3);
 primes.Add(5);
 primes.Add(7);
    }
}

C# program that adds elements to List

using System.Collections.Generic;

class Program
{
    static void Main()
    {
 List<int> list = new List<int>();
 list.Add(2);
 list.Add(3);
 list.Add(5);
 list.Add(7);
    }
}

C# List

List. An array does not dynamically resize. A List does. With it, we do not need to manage the size on our own. This type is ideal for linear collections not accessed by keys

Dynamic in size, with many methods, List makes life easier. List is a generic (constructed) type. We need to use < and > in the List declaration. Lists handle any element type.