Channels

C# Custom Number Formatting


Very often the inbuilt numerical formatting in C# will be insufficent and you will want to apply the custom formatting for your numbers.

The String.Format method is very flexible and can be used to apply custom formatting rules. The # character informs the Format method how to format the numerical value, for example to forma the number 12000.12 as 12,000.12 use can use the format “#,#.##” as in the below code:

double dbl1 = 12000.12;string outputStr;outputStr = string.Format(“This is a custom formatting example {0:#,#.##} “, dbl1);Response.Write(outputStr);

In this example the , character is used to denote the thousand separator and the ## after the decimal place denotes that a maximum of 2 numbers should appear after the decimal place. Therefore the double 12000.1 would be formatted as 12,000.1, if you wish two decimals places always to be shown (ie 12,100.10 in this case) you should use the ’0′ character after the decimal place:

outputStr = string.Format(“This is a custom formatting example {0:#,#.00} “, 12000.1);

You can also add additional characters before and after the formatting string. Thus to format the value as $ 12,000.12 use the format string “$ #,#.##” , or even “#,#.## US Dollars” would output “12,000.12 US Dollars”.

The % character will multiply the value by 100 and then add % character to the end of the string. Thus for the double value 12 the format string “{0:0.00 %}” will output “1200.00 %”. Note that you can use the # character in place of the 0 character as per the formatting rules above and so “#,#.## %” in this case would output “1,200 %”.

View the original article here



Be the first to comment - What do you think?
Posted by Anand Narayanaswamy - September 15, 2011 at 3:54 pm

Categories: C#   Tags: Custom, Formatting, Number

The Gig.U Ultra High Speed Internet Project

The Gig.U project aims to deliver ultra-high speed internet access to 29 universities and surrounding areas in the United States. Many of the universities are hoping that the enhanced access can increase economic growth and facilitate research and development in various communities.

How fast is ultra-high speed? The project looks to bring speeds up to 1Gbps to these areas. At speeds this fast, an entire high-definition movie could be downloaded in mere seconds. With this type of bandwidth, Gig.U has implications for other data intensive applications.

The Gig.U Ultra High Speed Internet Project

Who Does It Benefit?

Not only will universities benefit from internet providers offering ultra-high speed internet, but energy, healthcare and telecommunications sectors may all benefit from increased speeds for research, collaboration and sending high resolution images. Currently, 29 universities have decided to participate in this project.

Universities such as Arizona State University, University of Kentucky, University of Chicago, University of Montana, Indiana University, University of New Mexico and West Virginia University are participating. The results of the project will be used to apply for capital and attract investors. The creators will also use the data gathered to design a better business model.

 

Why Start with Universities?

Universities are the breeding ground for young and sharp minds that are eager to learn new technology. Introducing this type of technology in a university environment will allow the leaders in research to test the network for its viability in the public and private sector. Many of the best companies have their roots from a university research lab or dorm room. University communities are often the building blocks for economic growth in America.

Though many universities also have ultra-high speed access on campus, many faculty and students do not have comparable networks when they need to accomplish tasks at home. This presents some logistical challenges for faculty trying to balance work and home life. Gig.U hopes to examine how to bridge the gap between the community and the university.

Is Gig.U a Valuable Service?

Experts are still researching the implications for Gig U’s high speed internet access in start-ups as well as other locations across the nation. Many argue that the services are redundant and overlap existing networks in more convenient areas. Because researchers still do not know the full extent of the effects of Gig.U, they continue to collaborate to determine how it can affect the workplace, community and university environments.

Medical diagnosing over the internet was one of the major developments made possible by increased bandwidth, and Gig.U hopes to build upon these efforts. Physicians even direct doctors during surgery via conferencing software with the highly reliable internet transfer.

Many students are also pleased to realize the implications of the Gig.U project in the gaming world and in other arenas as well. While the driving force behind the initiative is not to have students become expert gamers, stress relief brought out by smooth, interactive gameplay can be helpful. Gaming may also have rehabilitative notions in terms of improving hand-eye coordination and dexterity.

Students without ultra-high speed internet may find themselves at a disadvantage when compared with their fast-access counterparts. Slower connections mean added time for downloads, web browsing, file sharing, and academic research. Very realistically, assignments may take longer to finish, and fatigue may occur more frequently when the connection is slower and frustration levels rise. The quality of work could diminish as a result.

Conclusion

High speed internet is essential to the success of students and universities. Gig.U is gaining in popularity. With further progress, more universities will participate to improve the workplace and educational environment for more people. The more data points and fresh feedback that Gig.U obtains, the more beneficial the internet project will become for present and future generations.

Note: Photo courtesy of pagedooley via FlickR Creative Commons.

This is a guest post by Blake Sanders, who is a Broadband Expert and frequently writes articles related to Internet Providers.

Submit Guest Article 

Be the first to comment - What do you think?
Posted by Blake Sanders - September 5, 2011 at 12:40 am

Categories: Internet   Tags: Gig.U, Ultra High Speed Internet Project

Measure Memory Usage of .NET Applications

There are primarily two main methods for measuring the memory usage of a .NET Framework application, using the GC class or using System.Diagnostics


The .NET Framework’s GC class contains many useful memory-related methods, including GetTotalMemory(), which returns the amount of memory the garbage collector believes is allocated to your app. The number not be exact right since some objects may not have been garbage collected yet. This, however does have the advantage of being able to inform you of the amount of memory a certain part of your app uses, as opposed to the entire process:


long memAvailable = GC.GetTotalMemory(false);


Console.WriteLine(“Before allocations: {0:N0}”,


memAvailable);


int allocSizeObj = 40000000;


byte[] bigArrayObj = new byte[allocSizeObj];


memAvailable= GC.GetTotalMemory(false);


Console.WriteLine(“After allocations: {0:N0}”, memAvailable);


The above code will output the following:

Before allocations: 651,064After allocations: 40,690,080

View the original article here

Be the first to comment - What do you think?
Posted by Anand Narayanaswamy - August 26, 2011 at 1:55 pm

Categories: C#   Tags: Applications, Measure, Memory, Usage

Passing Parameters in C#

Methods are one of the primary concepts in C#. You can pass parameters to C# methods. There are basically three primary methods of passing parameters to C# methods:

This is passing parameters with no modifying keywords :

void MyMethod(Student studentObj, int aNumber)
{

aNumber += 5;

studentObj.Name = “Jon”;

}

In the above example MyMethod takes two parameters – a Student object and an Integer. Note the difference between the passing a Value Type (such as an Integer) and a Reference Type (such as any class such as a ‘Student’ object in the above example). Any operations performed on a value type will no be reflected in the original variable whereas operations performed on a reference type are reflected in the original variable. For example, assuming calling the above MyMethod method note the below code (note the comment explanation at the end of the code block):

int myInt = 8;Student aStudent = new Student();aStudent.Name = “Mike”;MyMethod(aStudent, myInt);// myInt value is still 8 since it is a Value Type and immutable// aStudent.Name is now Jon since it is a Reference Type// and the variable aStudent just points to a Student object// on the Managed Heap which is then operated upon by the MyMethod(0 method.

By default all paramteres are passed by value – i.e. only the value of the variable is passed to method and the object itself is not passed. Thus operations within the method can only operate on the value of the parameter it receives and cannot make changes to the original variable (do note that this only applies to Value Types as noted above since Reference Types will have their state changes when they are passed to methods).

Alternatively, parameters can be passed by Reference using the ref keyword and their values will be changed by the method:

void MyMethod(ref int aNumber)
{

aNumber += 5;

}

protected void Page_Load(object sender, EventArgs e)
{

int myInt = 8; M

yMethod(ref myInt); //myInt is now 13

}
In the above code the aNumber parameter in MyMethod now has the ref keyword, note that the ref keyword is always required when calling the method. The integer which is passed to the method now has its value changed by the method.

The out keyword has a similar impact to the ref keyword as the parameters that are passed to it can have their values changed. The primary difference is that when using the out keyword the objects do not need to be initialized. In the above example of passing by reference there would be a compile-time error if the integer had simply been declared by not initialized with a value as below:

int myInt;

If the ref keyword was replaced with the out keyword this issue would be resolved:

void MyMethod(out int aNumber)

{

aNumber = 3;

aNumber += 5;

}

protected void Page_Load(object sender, EventArgs e)
{

int myInt; MyMethod(out myInt); //myInt is now 8

}

Note that the integer is now initialized with a value in the method body.

You might wonder what is the point of the out keyword. It can be very useful in situations where you want a method to perform several operations and return multiple values. In this circumstance you can simply declare multiple variables and pass them to void method to have their values assigned.

In this article, you have learned how to pass parameters in C# in various ways.

Read the original article

Be the first to comment - What do you think?
Posted by Anand Narayanaswamy - August 11, 2011 at 1:04 am

Categories: C#   Tags: Passing Parameters in C#

How to Format Numerical Data in C#

You will be required to make use of numerical data while programming with C#. This article examines how to format numerical data in C#.

C# ships with several inbuilt formatting specifies which can be used to quickly format a number, for example the ‘c’ specifier will format the number as a currency:

double dbl1 = 7777777.7777777;
outputStr = string.Format(“This is the currency format {0:c}”, dbl1);

This will output the numerical value as a currency based on the user’s current currency setting. In my case this will output:

This is the currency format $10,000,000.00

Note firstly that String.Format is a static method – ie you can not use it from an instance of a string object so the below code will generate a compile error:

string outputStr;outputStr.Format(“This is the currency format {0:c}”, dbl1);

You must always use it at the class level (ie String.Format)

Below are the inbuilt formatting specifiers:
c – Currency
d – Decimal
e – Exponential notation
f – Fixed point formatting
n – Numerical formatting (with commas)
x – Hexidecimal

The below code shows an implementation of the n formatting speficier:

double dbl1 = 7777777.7777777;

string outputStr;

outputStr = string.Format(“This is the numerical format {0:n}”, dbl1);

Response.Write(outputStr);

Note that there will be compile errors if you attempt to format a double using the decimal format. Also, note that hexidecimal expects an integer type.

View the original article here

Be the first to comment - What do you think?
Posted by Anand Narayanaswamy - August 9, 2011 at 7:16 pm

Categories: C#   Tags: Format Numerical Data in C#, Formatting, Numerical

Introducing C# Strings

Strings are the fundamental concept to be learned in every programming language. Working with strings is a very common task for most C# developers. The .NET Framework offers good variety of tools for working with strings, but care must be taken as there are several gotchas to trip up the beginner.

The first thing to note about strings in .NET is that they are Reference Types. Reference types are live on the managed heap in .NET and so they are managed by the .NET Garbage Collector and unlike Value types they are not automatically destroyed when they are out of scope.

Strings can be easily instantiated using the below syntax:

string s1;

Note that there is no default value for a string, thus you need to assign a value to the string before attempting to access it:

string s1;

Literal1.Text = s1; //Will give run-time error string

s2 = “”; //This is as close as you can get to assigning nothing to a string;

Literal1.Text = s2;

There are numerous inbuilt methods in the .NET framework for dealing with strings. Namely:

Note that strings are immutable and thus the original string can never be changed. Therefore if you need to alter the string you will need to re-assign it a new string value:

string s1 = “all lower case”;s1.ToUpper() ; //s1 is unchanged

Literal1.Text = s1; //outputs ‘all lower case’ since s1 is unchangedstring s2 = “all lower case”;s2 = s2.ToUpper() ; //s2 is now re-assigned the new uppercase stringLiteral2.Text = s2; //outputs ‘ALL LOWER CASE’

For more on string immutability see Strings are Immutable.

C# offers a convenient method for concatenating a string using ‘+’ :

string s1 = “New”;

string s2 = “Delhi”;

Literal1.Text = s1 + s2;

Whilst this method is convenient it is not efficient for performing a large number of concatenations (since strings are immutable and are refenece types created on the managed heap each concatenation results in the creation of an extra string which is not automatically destroyed and stays in memory until the Garbage Collector destroys it).

To get around this limitation the .NET Framework provides the StringBuilder class which can efficiently concatenate strings using its Append() method:

StringBuilder sb = new StringBuilder();

sb.Append(“New”);

sb.Append(“Delhi”);

Literal1.Text = sb.ToString();

This is by way of demonstration since there would not be any memory saving from concatentating two strings (in fact this can consume more memory since the StringBuilder consumes memory). In general concatenating more than four strings is normally when a StringBuilder should be used.

In common with other C-based languages, C# strings may contain escape characters which qualify how the string should be output.
Escape characters begin with a backslash followed by a specific character.

Below are listed the various escape characters and their output:

\’ Inserts a single quote
\” Inserts a double quote
\\ Inserts a backslash
\a Triggers a system alert
\n Inserts a new line (on Windows systems)
\r Inserts a carriage return
\t Inserts a horizontal tab

To preserve a string exactly as it is exactly as it was added simply add the @ character at the start of the string.

For example:

string outputStr = “This site is named \’C# Help\’ “;Response.Write(outputStr);

This outputs

This site is named ‘C# Help’

To output the string verbatim add the @ character at the start of the string.

string outputStr = @”This site is named \’C# Help\’ “;Response.Write(outputStr);

This outputs

This site is named \’C# Help\’

Note that this will also preserve white space:

string outputStr = @”This is a brokenup string”;

View the original article here

Be the first to comment - What do you think?
Posted by Anand Narayanaswamy - August 8, 2011 at 7:47 am

Categories: C#   Tags: c# strings, Strings

Gaia Ajax 3.7 Released

Gaiaware had released Gaia Ajax 3.7, which is a powerful Graphical User Interface (GUI) library for ASP.NET 2.0, 3.5 & 4.0 with sophisticated built-in ajax support. The product provides support for Visual Studio 2005, 2008 and 2010 and includes lots of new and exciting features in controls and aspects.

A core feature of Gaia Ajax 3.7 is that all controls which shops with the product now fully utilize the advanced ajax engine. Gaia Ajax 3.7 now includes 40+ ajax server controls such as Window, Treeview, Tabcontrol and etc. Moreover, it comes with 10 aspects to add behaviours such as drag and drop, modal dialogs and resizable user interfaces. It also includes over 20 effects which will enable you to add animations to all controls. The product will be very useful if you wish to develop a web application completely using Ajax.

Gaia Ajax 3.7 now supports Internet Explorer 6, 7, 8, 9, FireFox, Safari, Opera, Chrome. The vendor offers a fully functional evaluation version which can be downloaded from http://gaiaware.net/ and paid versions with complete source code. I hope the vendor will release controls for ASP.NET MVC platform.

Be the first to comment - What do you think?
Posted by Anand Narayanaswamy - June 18, 2011 at 6:49 am

Categories: ASP.NET, Controls   Tags: Gaia Ajax, Gaiaware

Exploring Kigg – Alternative to Pligg

Pligg is a very powerful application which is used to create Digg or DotNetKicks styled websites. But the software is in PHP version and many .NET developers may not like PHP. Hence, few developers have developed a new application code named KiGG which is a web 2.0 style social news web application developed using Microsoft supported technologies.

The core feature of KiGG is that the application is developed using ASP.NET Modal View Controller (MVC). Moreover, developers have integrated Linq To SQL, Entity Framework, Enterprise Library, Unity and jQuery.

You can either make use of SQL Server 2005, SQL Server 2008 or MySQL 5.x. MySQL works with windows servers. KiGG also provides automatic thumbnail generation using PageGlimpse or WebSnapr and also enables you to protect SPAM using Akismet, TypePad and Defensio.

You can also enable Captcha using reCaptcha service and also display images as Gravatar. Kigg has built-in URL shrinking services and all the submitted URLs will either become http://tinyurl.com or http://is.gd.

Even though I liked the features of KiGG I was unable to install it. I had contacted support but they are not much helpful. There is a discussion forum at the project site where develoeprs can post queries.

I am again going to install KiGG as I am very eager to test drive the product. I know that KiGG involves more work than installing Pligg but it will be a new learning experience for those develoeprs who want to delve deep into ASP.NET MVC.

Be the first to comment - What do you think?
Posted by Anand Narayanaswamy - June 4, 2011 at 7:44 am

Categories: .NET, ASP.NET   Tags: Alternative to Pligg, KiGG

Fix – Could not load file or assembly RadMenu.Net2 or one of its dependencies

Telerik has developed a wide range of controls and RadMenu is one of them. It is used to create menus in an ASP.NET application. Once you deploy the application to a web server then you will sometimes receive error message as displayed below

Server Error in ‘/’ Application.
——————————————————————————–

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Could not load file or assembly ‘RadMenu.Net2′ or one of its dependencies. The system cannot find the file specified.

Source Error:
Line 1:  <%@ Register Assembly=”RadMenu.Net2″ Namespace=”Telerik.WebControls” TagPrefix=”radM” %>
Line 2: 
Line 3:  <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd“>
 

Source File:   Line: 1

Assembly Load Trace: The following information can be helpful to determine why the assembly ‘RadMenu.Net2′ could not be loaded.

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

Resolution

To resolve the above error you should create the corresponding folder an application under Internet Information Services (IIS). This process can be done from within the hosting control panel.

If your control panel interface doesn’t have this functionality then your hosting provider will login to the remote web server and perform the job for you.

Be the first to comment - What do you think?
Posted by Anand Narayanaswamy - May 29, 2011 at 12:52 am

Categories: ASP.NET, Telerik   Tags:

Fix – Assembly binding logging is turned OFF

Assembly binding logging is turned OFF error will be displayed when you attempt to install a ASP.NET application on the server. This error will also occur if you upload files containing third party components such as Telerik ASP.NET Control Suite.

Following error will be displayed on the screen if you upload files containing Telerik RadMenu control.

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Could not load file or assembly ‘RadMenu.Net2′ or one of its dependencies. The system cannot find the file specified.

Source Error

Line 1:  <%@ Register Assembly=”RadMenu.Net2″ Namespace=”Telerik.WebControls” TagPrefix=”radM” %>
Line 2:
Line 3:  <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd“>
Assembly Load Trace: The following information can be helpful to determine why the assembly ‘RadMenu.Net2′ could not be loaded.

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

Resolution

In order to fix the above error, you need to make the domain or relevant folder as an ASP.NET application using the control panel provided by your web hosting provider.

If your control panel doesn’t provide any option to make your site/folder an ASP.NET application then you need to contact your provider and submit a support ticket.

Be the first to comment - What do you think?
Posted by Anand Narayanaswamy - May 10, 2011 at 1:59 am

Categories: ASP.NET   Tags: Assembly binding logging is turned OFF

Next Page »