Programming Index Cards

August 6, 2008

Passing reference types by ref and by value

Filed under: programming — Tags: , , , — apcig @ 2:30 am

Original Article1

…Whenever an instance of reference type is created the pointer is returned back… Now when this pointer is passed by val, all we are doing is duplicating the pointer but it still points to the same memory on the heap and hence any manipulation done to the object in the called method will manipulate the same data to which original hashtable pointer was pointing. In case of passing by ref, the original pointer itself is passed to the called function.

Shared Sub Main()
Dim hst As Hashtable = New Hashtable(3)
hst.Add(1, 1)
‘by default.net parameters are passed by val
PassHashTableByVal(hst)
‘will print 2
Console.WriteLine("Count after passing byval: {0}", hst.Count)
PassHashTableByRef(hst)
‘will throw a null reference exception.
Console.WriteLine("Count after passing byref: {0}", hst.Count)
Console.Read()
End Sub
Shared Sub
PassHashTableByVal(ByVal h1 As Hashtable)
h1.Add(2, 2)
h1 = Nothing
End Sub
Shared Sub PassHashTableByRef(ByRef h2 As Hashtable)
h2.Add(3, 3)
h2 = Nothing
End Sub

A null reference exception in case of passing hashtable as by ref is thrown because in the PassHashTableByRef() we are setting h2 to null which actually is same as hst, whereas in case of passing it by val we are only setting the copy of the hst pointer to null.

1Difference between passing reference types by ref and by value
By Sachin Nigam November 07, 2005

Parameter passing in C#
Honorable Mention

August 5, 2008

Powershell’s popularity is rocketing skywards

Filed under: career, programming — apcig @ 2:42 am

TIOBE Programming Community Index for July 2008

July Headline: Powershell’s popularity is rocketing skywards

The TIOBE Programming Community index gives an indication of the popularity of programming languages. The index is updated once a month. The ratings are based on the number of skilled engineers world-wide, courses and third party vendors. The popular search engines Google, MSN, Yahoo!, and YouTube are used to calculate the ratings. Observe that the TIOBE index is not about the best programming language or the language in which most lines of code have been written.

The index can be used to check whether your programming skills are still up to date or to make a strategic decision about what programming language should be adopted when starting to build a new software system. The definition of the TIOBE index can be found here.

Pos.
Jul 2008
Pos.
Jul 2007
Delta in Position Progr. Language Ratings
Jul 2008
Delta
Jul 2007
Status
1 1 Java 21.345% +0.33% A
2 2 C 15.945% -0.42% A
3 3 C++ 10.693% +0.19% A
4 4 (Visual) Basic 10.447% +0.72% A
5 5 PHP 9.525% +0.87% A
6 6 Perl 5.131% -0.20% A
7 8 Python 4.973% +1.95% A
8 7 C# 4.000% +0.29% A
9 9 JavaScript 2.757% +0.24% A
10 10 Ruby 2.735% +0.64% A

August 3, 2008

Test Driven Development Using NUnit in C#

Filed under: Unit Testing, c#, programming — apcig @ 3:56 pm

Read the full article

Test Driven Development emphasizes the … need to prepare test scenarios or test cases before writing the code itself… It requires that the programmer be very clear about what tests the program should pass and what test it should fail, bringing such concerns to the forefront of the software design process… An automated job is one that’s always very, very easy to do. These automated tests are meant to be run every time there’s a code change and are referred to as unit tests.

Unit testing with NUnit is useful for testing class libraries that makeup an ASP.NET application, but is not designed to test the UI portions of an ASP.NET application. There is, however, an additional free tool, NUnitAsp, that is designed to provide unit testing for the GUI portion of a Web application.

August 1, 2008

Why Use SQL Over procedural?

Filed under: SQL, database, programming — apcig @ 3:01 am

http://www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=27

Why Use SQL Over procedural?

Structured Query Language (SQL) is a set-based language as opposed to a procedural language. It is the defacto language of relational databases.

The difference between a set-based language vs. a procedural language is that in a set-based language you define what set of data you want or want to operate on and the atomic operation to apply to each element of the set. You leave it up to the Database process to decide how best to collect that data and apply your operations. In a procedural language, you basically map out step by step loop by loop how you collect and update that data.

There are two main reasons why SQL is often better to use than procedural code.

  • It is often much shorter to write – you can do an update or summary procedure in one line of code that would take you several lines of procedural.
  • For set-based problems – SQL is much faster processor-wise and IO wise too because all the underlining looping iteration is delegated to a database server process that does it in a very low level way and uses IO/processor more efficiently and knows the current state of the data – e.g. what other processes are asking for the data.

July 23, 2008

MS SQL Server

Filed under: SQL Server, database, programming — apcig @ 1:31 pm

Temporary tables vs. table variables1

Normal tables = physical tables

Local Temp tables - available to the session that created them. #LOCALTEMP

CREATE TABLE #TibetanYaks(
  YakID int,
  YakName char(30)
)

Global Temp tables - available to all users and sessions. Rarely used.

CREATE TABLE ##TibetanYaks(
  YakID int,
  YakName char(30)
)

Table variables - stored part memory, part disk. Access time faster than a temp table

DECLARE @TibetanYaks TABLE (
  YakID int,
  YakName char(30)
)

Which command using Query Analyzer will give you the version of SQL Server and Operating System?
@@VERSION
Returns version, processor architecture, build date, and operating system for the current installation of SQL Server.3

1 SearchSQLServer.com

2 SqlTeam.com

3 dev.fyicenter.com

DataSet vs. DataReader

Filed under: ADO.Net, database, programming — apcig @ 1:24 pm

DataSet vs. DataReader1

1. DataReaders have less overhead than DataSets and are more efficient.
2. DataReaders can be databound (i.e. to a DataGrid).
3. Iterated once through

but

1. DataSets are disconnected. They are like disconnected models of the database.
2. DataSets can be made into Session objects or files, for persistence
3. DataSets can provide access to more than one table and table relationships
4. DataSets can be bound to multiple controls
5. Reiterated through multiple times.
5. You can jump to a particular record or go backwards through a DataSet.
6. Ability to name your table

TABLE 6.3: COMPARING DATAREADERS AND DATASETS2
DataReader DataSet
A DataReader is specific to a data provider (for example, SqlDataReader, OdbcDataReader, and OleDbDataReader). The DataSet class isn’t a part of any data provider. It’s specific to .NET only. However, the DataAdapter used to fill the DataSet with Fill() is specific to a data provider.
The data retrieved through a DataReader is read-only. The data retrieved through a DataSet is read-write.
The data retrieved through a DataReader is forward-only. Once the data has been cycled through, the DataReader must be closed and re-created in order to reaccess the data. You can work with data in a DataSet in any order you choose as many times as you like.
A DataReader presents data through a direct connection to the data source. Only one row of data is stored in Internet Information Services (IIS) memory at any one time. A DataSet stores all the data from the data source in IIS memory at once.
A DataReader is fast. A DataSet is slower than a DataReader.
A DataReader takes up few IIS and memory resources but annexes the databaseconnection until it’s closed. A DataSet takes up a lot more memory/IIS resources to store all the data, but it doesn’t hold up a database connection until it’s closed. The connection needs to be open only when Fill() is called.
A DataReader lasts as long as the connection to the database is open. It can’t be persisted in a cookie or a session variable. A DataSet lasts only until the page is reloaded (posted back) unless it’s somehow persisted (for example, in a session variable).
Fields in a DataReader are referenced by index number or name. You can reference fields in a DataSet by name, but you must also name the DataTable and identify the row (index) that contains the field.
A DataReader has no concept of primary keys, constraints, views, or any other relational database management system (RDBMS) concept except row and field. A DataSet contains DataTables. A primary key may be set for each DataTable, and relationships and constraints may be established between them. DataViews may be created over the DataSet.
You can’t update a data source through a DataReader. You can make changes to data in a DataSet and then upload those changes back to the data source.
A DataReader connects to only onedata source. A DataSet can be filled with Fill() from multiple data sources.

1 My Library Syndication

2 Beginning ASP.NET 1.1 Databases: From Novice to Professional

ASP.Net

Filed under: ASP.Net, programming — apcig @ 1:12 pm

Describe the role of inetinfo.exe, aspnet_isapi.dll andaspnet_wp.exe in the page loading process.
inetinfo.exe is theMicrosoft IIS server running, handling ASP.NET requests among other things. When an ASP.NET request is received (usually a file with .aspx extension), the ISAPI filter aspnet_isapi.dll takes care of it by passing the request to the actual worker process aspnet_wp.exe.

inetinfo.exe = IIS
aspnet_isapi.dll = ISAPI filter
aspnet_wp.exe = actual worker process

Page Load
Init, Load, PreRender, Unload, Disposed = ILPUD
ViewState is available after Init, OnLoad for a control.

Page Life Cycle Events2

  • Page_Init
    The server controls are loaded and initialized from the Web form’s view state. This is the first step in a Web form’s life cycle.
  • Page_Load
    The server controls are loaded in the page object. View state information is available at this point, so this is where you put code to change control settings or display text on the page.
  • Page_PreRender
    The application is about to render the page object.
  • Page_Unload
    The page is unloaded from memory.
  • Page_Disposed
    The page object is released from memory. This is the last event in the life of a page object.
  • Page_Error
    An unhandled exception occurs.
  • Page_AbortTransaction
    A transaction is aborted.
  • Page_CommitTransaction
    A transaction is accepted.
  • Page_DataBinding
    A server control on the page binds to a data source.
  • Process Request Method finally renders HTML Page

What’s a bubbled event?
When you have a complex control, like DataGrid, writing an event processing routine for each object (cell, button, row, etc.) is quite tedious. The controls can bubble up their eventhandlers, allowing the main DataGrid event handler to take care of its constituents.

When controls send their events to their parent control to be handled.

What are the different types of Session state management options available with ASP.NET?
ASP.NET provides In-Process and Out-of-Process state management. In-Process stores the session in memory on the web server. This requires the a “sticky-server” (or no load-balancing) so that the user is always reconnected to the same web server. Out-of-Process Session state management stores data in an external data source. The external data source may be either a SQL Server or a State Server service. Out-of-Process state management requires that all objects stored in session are serializable.

In-Process stores the session in memory on the web server.
Out-of-Process Session state management stores data in an external data source, requires that all objects stored in session are serializable.

Source: Mark Wagner’s .NET C# Cogitation1

VbDotNetHeaven2

July 14, 2008

Windows Communication Foundation (WCF)

Filed under: SOA, WCF, programming — apcig @ 7:29 pm

Windows Communication Foundation is a comprehensive layered framework for SOA programming.
[WCF = SOA Framework]
… you’re only responsible for modeling the communications of your products with the
outside world, and you don’t have to worry about actually implementing this model. This saves
you from having to deal with dirty little details of the underlying runtime components that implement
your model, and consequently allows you to focus on what matters to your application…

WCF Service
…you must not incorporate the logic that handles
the communications of the bug report manager system with the outside world into the bug report manager system itself. Instead, you must delegate the responsibility of the communications with the outside world to a different component. This component is known as a WCF service…

WCF Endpoint
Binding: The binding of an endpoint specifies how the endpoint communicates with the outside
world…
Address: The address of an endpoint specifies where the endpoint is…
Contract: The contract of an endpoint specifies what operations of the WCF service the clients
can access through that endpoint.

WCF Service Model
…, a WCF service is a wrapper around a piece of software enabling the software to
communicate with the outside world.
… developing a WCF service model boils down to developing its endpoints. Recall that
every endpoint has an address, a binding, and a contract. In general it takes two sets of tasks to develop a WCF service model: imperative (coding or development tasks) and administrative.
anything that can be done declaratively in the configuration files should be done in the configuration files to allow administrators to modify the service without any code changes.

June 26, 2008

Accessibility Modifiers

Filed under: OOP, c#, programming — apcig @ 7:36 pm

public
- Access is not restricted.
- Allows the members to be globally accessible.

protected
- Access is limited to the containing class or types derived from the containing class.

internal
- Access is limited to the current assembly.
- Internal members are accessible only within files in the same assembly.

protected internal
- Access is limited to the current assembly or types derived from the containing class.

private
- Access is limited to the containing type.


sealed
- prevents inheritance

Sources: Accessibility Levels (MSDN), Difference between internal and private

Blog at WordPress.com.