jump to navigation

Refactoring tool of Visual Studio 2005 September 7, 2007

Posted by addisu in Software - .NET 2.0, Software - .NET General, Software - ASP .NET 2.0, Software - C#.
1 comment so far

How much do u use refactoring tool of Visual Studio 2005?

  • Rename: i probably use this tool more often. Once you declare a class or a variable and used it everywhere, “rename” tool is the best if you are not happy with the name. Naming is one of best practice and the name of a class or a variable should be descriptive. You might not come up with the best name at the beginning.
  • Extract Method: This is another tool i use. Every time there is a reusable portion of code that exist in my method, i used “Extract Method” to have another smaller method. … for two purposes sometimes,..one to reuse the code and two, to make smaller size and readable method
  • Generate Method Stub: even though it is not part of the Refactoring tool part, i like to use this tool. I write a method call to an object that doesn’t exist. I use this tool to generate the method stub. The best part of this tool is, it knows if private or internal or public should be used. … the name and type of parameter, return value…

‘ref’ and ‘out’ in C# is Bizarre and confusing… September 4, 2007

Posted by addisu in Software - .NET 2.0, Software - .NET General, Software - C#.
add a comment

i tried to really see the difference of these key words in C# and i still get it weired.

http://msdn2.microsoft.com/en-us/vcsharp/aa336814.aspx

  • for ‘out’, we can pass the variable unassigned but we have to make sure we assign it before use in the callee method
  • for ‘ref’, we have to assign the variable before we pass it…

i still see both as pass by reference kind of thing…

any suggestions….?

.NET 2.0 Generics, … i am in love with it…. August 24, 2007

Posted by addisu in Software - .NET 2.0, Software - .NET General, Software - C#.
add a comment

I had a number of collection classes with similar functionalities like this…

public class PermissionCollectionEntity : IEntity
{
List<PermissionEntity> list;

public PermissionCollectionEntity()
{
this.list = new List<PermissionEntity>();
}

public List<PermissionEntity> Items
{
get
{
return this.list;
}
}

public void Add(PermissionEntity entity)
{
if (entity != null)
{
this.list.Add(entity);
}
}

public Boolean Remove(PermissionEntity entity)
{
if (entity != null && this.list.Contains(entity))
{
return this.list.Remove(entity);
}
else
{
return false;
}
}

public void RemoveAt(int index)
{
if (this.list.Count > index && index >= 0)
{
this.list.RemoveAt(index);
}
}

public void Clear()
{
if (this.list.Count > 0)
{
this.list.Clear();
}
}

public int Count
{
get
{
return this.list.Count;
}
}
}

Now i am replacing those Collection classes with one Generics class like this…

public class TCollectionEntity<T> : IEntity
where T : IEntity
{
List<T> list;

public TCollectionEntity()
{
this.list = new List<T>();
}

public List<T> Items
{
get
{
return this.list;
}
}

public void Add(T entity)
{
if (entity != null)
{
this.list.Add(entity);
}
}

public Boolean Remove(T entity)
{
if (entity != null && this.list.Contains(entity))
{
return this.list.Remove(entity);
}
else
{
return false;
}
}

public void RemoveAt(int index)
{
if (this.list.Count > index && index >= 0)
{
this.list.RemoveAt(index);
}
}

public void Clear()
{
if (this.list.Count > 0)
{
this.list.Clear();
}
}

public int Count
{
get
{
return this.list.Count;
}
}
}

I can re-use this collection for all my Entity classes…I can also enforce the Entity Classes shouldimpliment certain Interfaces…

Cool stuff…

Using keyword “using” in code block March 3, 2007

Posted by addisu in Software - .NET 2.0, Software - .NET General, Software - ASP .NET 2.0, Software - C#.
2 comments

Recently, I have been asked about the alternative way to Dispose objects as we often do in the “finally” block in “Try .. Catch .. Finally” block for connection object. I have seen “using” keyword in some sample codes but I never aware of it does dispose the objects declared as “using

Example:

using (IDataReader dataReader = _db.ExecuteReader(“GetCategories”))
{
// Processing code
while (dataReader.Read())
{
Category item = new Category(
dataReader.GetInt32(0),
dataReader.GetString(1),
dataReader.GetString(2));
this.cmbCategory.Items.Add(item);
}
}

  1. One thing to note is the objects inside the “using” block should implement “IDisposable” interface.
  2. Still if we want to cache Exception, we need to inside and for me it seems a little bit redundant if we do the dispose inside the “finally” block as we often do for the connection object

Configure settings for a ASP .NET 2.0 Web application December 4, 2006

Posted by addisu in MCPD Web Developer, Software - .NET 2.0, Software - .NET General, Software - ASP .NET 2.0, Software - C#, Software - Certifications.
add a comment
  • Configure system-wide settings in the Machine.config file.
  • Configure settings for a Web application in the Web.config file.
  • Manage a Web application’s configuration by using the Web Site Administration Tool.

Summary

As was the case in previous versions of ASP.Net, there are many settings that can be defined in both Machine.config and Applications Web.Config. With ASP.Net there are many more settings that can be controlled in both files. Lucky for us, in ASP.Net we also have access to a Web Site Administration tool and mmc tools to make it easier to make changes using a GUI interface that will modify the Xml stored in the configuration files without having to manually modify them.

At the very least be familiar with the Web Site Administration Tool which can be accessed by navigating to webadmin.axd under your applications root. The tool has four tabs: Security, Profile Application and provider. Most of these are discussed in detail in later sections of this document.

Other Resources & Links:

Configuration Management in ASP.Net 2.0
http://www.extremeexperts.com/Net/Articles/ConfigurationManagementinASPNET.aspx

Configuration Management in ASP.Net 2.0 Part 2
http://www.extremeexperts.com/Net/Articles/ConfigurationManagementinASPNETPart2.aspx

 

Program a Web application.

  • Redirect users to another Web page by using a server-side method.
  • Detect browser types in Web Forms.
  • Ascertain the cause of an unhandled exception at the page level.
  • Programmatically access the header of a Web page.
  • Implement cross-page postbacks.
  • Assign focus to a control on a page when the page is displayed.
  • Avoid performing unnecessary processing on a round trip by using a page’s IsPostBack property.
  • Access encapsulated page and application context.
  • Avoid unnecessary client-side redirection by using the HttpServerUtility.Transfer method.
  • Avoid round trips by using client-side scripts.
  • Use a page’s Async attribute to create a page that has built-in asynchronous capabilities.
  • Convert HTML server controls to HTML elements.

Summary

Redirecting users to other pages through Server Side Methods is accomplished through Response.Redirect, HttpServerUtility.Transfer and Server.Transfer.

The Request object now exposes a Browser Object which can be used to determine the Browser type and capabilities.

At the page level you can subscribe to the Page OnError event which will allow you to get the last error by using Server.GetLastError to get the last exception that occurred.

You can access the header of a web page through the HTMLHead Control as long as you make the Head tag have the RunAt=Server Attribute. You can use it to modify the title, links, scripts, etc.

To Implement Cross Page Postbacks in ASP.Net 2.0 by setting the PostBackUrl property of the button control to the page you want your web form to post to. In the Posted Page you can access the calling page through the PreviousPage property which would be null if it wasn’t a cross page postback. The one tricky situation that could occur is that Validation on the Server side will not occur unless you call the PreviousPage.IsValid method to make sure that the post passed validation.

There are three ways to set focus programmatically using ASP.Net 2.0. You can use the Page.SetFocus Method with the ControlID of the Control you want to set the focus on, You can call the Focus method of the control itself, or you can set the DefaultFocus property of the Form element.

The IsPostBack Property lets you know programmatically if the page was posted back and you can avoid refreshing data that was persisted to Viewstate.

The HttpServerUtility.Transfer method works like the Response.Redirect method but without the extra client side trip.

In ASP.Net you can now set a PageDirective Attribute of Async to true to use the IHttpAsyncHandler. You then can use the AddOnPreRenderComplete method  to set a method to be called on the Begin and End of the PreRender event.

You can convert HTML Server controls to html elements by unchecking the runat server property or removing the runtat=”server” attribute from the control in html view.

Other Resources & Links:

Design Considerations for Cross Page Post Backs in ASP.NET 2.0
http://odetocode.com/Articles/421.aspx

How to: Detect Browser Types in ASP.NET Web Pages
http://msdn2.microsoft.com/en-us/library/3yekbd5b.aspx

Web Application Error Handling in ASP.Net
http://www.15seconds.com/issue/030102.htm

HTMLHead Class
http://msdn2.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlhead.aspx

Focus in ASP.Net Controls
http://www.beansoftware.com/ASP.NET-Tutorials/Focus-ASP.NET.aspx

HttpServerUtility.Transfer Method
http://msdn2.microsoft.com/en-us/library/8z9e2zxx.aspx

Wicked Code: Asynchronous Pages in ASP.Net 2.0
http://msdn.microsoft.com/msdnmag/issues/05/10/WickedCode/

How to: Convert HTML Server Controls to HTML Elements
http://msdn2.microsoft.com/en-us/library/yhwa4c06.aspx