Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

let in TypeScript

TypeScript (and ES6) introduces the let keyword to allow you to define variables with true block scope. That is if you use let instead of var you get a true unique element disconnected from what you might have defined outside the scope.

Following example code example demonstrates use of “let” which is truly block scoped.

Function getUserNameWithLet() returns username "nmk" which is block scoped.

Typescript Code:

function getUserNameWithVar()
{
    var userName = "ken";
    if (true) {
       var userName = "kendre";
    }
    return userName;
}
 
function getUserNameWithLet() {
    let userName = "nmk";
    if (true) {
        let userName = "kendre";
    }
    return userName;
}
 
window.onload = () => {
    var elNameWithVar = document.getElementById('lblFirstNameWithVar');
    var elNameWithLet = document.getElementById('lblFirstNameWithVar');
    elNameWithVar.innerHTML =getUserNameWithVar();
     elNameWithLet.innerHTML =getUserNameWithLet();
};

HTML code:

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>TypeScript HTML App</title>
    <link rel="stylesheet" href="app.css" type="text/css" />
    <script src="LetExample.js"></script>
</head>
<body>
    <h1>TypeScript HTML App</h1>
 
<label id="lblFirstNameWithVar"></label>
<label id="lblFirstNameWithLet"></label>
</body>

Enums in TypeScript

Enums 

In computer programming, an enumerated type (also called enumeration, enum, or factor in the R programming language, and a categorical variable in statistics) is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type(reference wikipedia.org).

Enum allows us to create set of named constants, common use is we can create distinct cases. TypeScript provides both numeric and string-based enums.

We can create three types of enum  in typescript following examples will explain in details , how to create enums.


1. Numeric enums

We will start with numeric enums, an enum can be defined using the enum keyword.

Enum Visibility
    {
        None=1,
        Hidden,
        Visible
    };

Above, we have a numeric enum where Up is initialized with 1. All the following members are auto-incremented from that point on. In other words, Visibility.None has the value 1, Hidden has 2, Visible has 3.

enum Visibility
{
    None,
    Hidden,
    Visible
};

Here, None would have the value 0Hidden would have 1, etc. This auto-incrementing behavior is useful for cases where we might not care about the member values themselves, but do care that each value is distinct from other values in the same enum.

How to use it:

Using an enum is simple: just access any member as a property off the enum itself, and declare types using the name of the enum, check following example where we used enum to set text display mode.

Example:

function
setVisibiity(recipient: string, message: Visibility): void {
    // ...
}

setVisibiity("Aditya Balaji Kendre", Visibility.Visible)


2. String enums

In a string enum, each member must be constant-initialized with a string literal, or with another string enum member.

How to use it:

function
SetUserFevColor(recipient: string, userColorChoice: Colors): void {
    // ...some typescript custom code
}
 
SetUserFevColor("Ajay Balaji Kendre", Colors.Green);
 


3. Heterogeneous enums

Technically enums can be mixed with string and numeric members,but its recommended to avoid Heterogeneous enums except must require case.

Example:

enum
UpdatedData  {
    No = 0,
    Yes="YES"
}

Creating Dynamic HTML table using C#

Creating Dynamic HTML table using C# in Windows Form/Windows Service/Library Project

In this code example post I will explain how to create Dynamic HTML table using C# in Windows Form/Console Application/Windows Service/Windows Library project.

   For example, I want to send products detail email in proper html table format using existing C# generic Product list data. In following code example I will share code only to create dynamic html table and in next code example post I will share html table with email code.

C# Code

   public class DynamicHtmlTable
    {
        //table tag constants
 
        private const string HtmlTableStart = "<table cellspacing=0 cellpadding=0 style=\"border-collapse:collapse; text-align:center;\">";
 
        private const string HtmlTableEnd = "</table>";
 
        private const string HtmlTrStart = "<tr>";
 
        private const string HtmlTrEnd = "</tr>";
 
        private const string HtmlThStart = "<th style=\" border-color:#5c87b2; border-style:solid;text-align:center;border-width:thin;\">";
 
        private const string HtmlThEnd = "</th>";
 
        private const string HtmlTdStart = "<td style=\" border-color:#5c87b2; border-style:solid;border-width:thin;\">";
 
        private const string HtmlTdEnd = "</td>";
 
 
        public void GetTableData()
        {
            var Products=Dal.DAL.GetProducts();
            var EmailData = BuildDynamicTable(Products, "Dynmaic Html Table").ToString();
        }
 
        //This method creates dynamic table
        private StringBuilder BuildDynamicTable(List<Product> Products,string appName)
        {
            var DynamicTable = new StringBuilder();
 
            DynamicTable.AppendFormat("<h2>CodeChef4u Example To Create{0}</h2>", appName);
 
            //dynamic table
 
            DynamicTable.AppendLine(HtmlTableStart);
 
            //header row
            CreateTableHeaderRow(DynamicTable);
 
            //table row
            foreach (var product in Products)
            {
                CreateTableRow(product, DynamicTable);
            }
            //Table ends
            DynamicTable.AppendLine(HtmlTableEnd);
 
            return DynamicTable;
        }
 
        //This method create table header row with columns names
        private static void CreateTableHeaderRow(StringBuilder DynamicTable)
        {
 
            DynamicTable.AppendLine(HtmlTrStart);
 
            DynamicTable.AppendLine(HtmlThStart + "Name" + HtmlThEnd);
 
            DynamicTable.AppendLine(HtmlThStart + "Category" + HtmlThEnd);
 
            DynamicTable.AppendLine(HtmlThStart + "Price" + HtmlThEnd);
 
            DynamicTable.AppendLine(HtmlThStart + "Image URL" + HtmlThEnd);
 
            DynamicTable.AppendLine(HtmlTrEnd);
 
        }
 
 
         //This method create new table row with data
        private static void CreateTableRow(Product Product,
 
           StringBuilder emailDynamicTable)
        {
 
            emailDynamicTable.AppendLine(HtmlTrStart);
 
            emailDynamicTable.AppendLine(HtmlTdStart + Product.ProductName + HtmlTdEnd);
 
            emailDynamicTable.AppendLine(HtmlTdStart + Product.CategoryId + HtmlTdEnd);
 
            emailDynamicTable.AppendLine(HtmlTdStart + Product.Price + HtmlTdEnd);
 
            emailDynamicTable.AppendLine(HtmlTdStart + Product.ImageUrl + HtmlTdEnd);
 
            emailDynamicTable.AppendLine(HtmlTrEnd);
 
        }
 
 
    }