Click here to Skip to main content
15,881,455 members
Articles / Web Development / ASP.NET
Article

Property Generator Tool

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
12 Apr 2008CPOL2 min read 30.5K   269   13   7
An approach to minimize issues when using properties in ASP.NET.

Introduction

This article describes a small tool for generating code for a property whose values are stored in ‘View State’.

Background

During my earlier development experience with creating custom controls for ASP.NET, most of the bugs that I used to find during the later stages of testing (or) in some customer reports were the view state values of properties messing with each other (because of copy/paste code).

This article will illustrate a technique that can be used to avoid this possible issue of storing properties in view state.

Persisting the property value in the ViewState

Say, if you want to create a property named “ExtendedText” and store its value in the view state, you will end up doing the following:

C#
public int ExtendedText
{
    get
    {
        object extendedText = ViewState ("ExtendedText"];
        return (extendedtext != null) ? (string) extendedtext : "DefaultText";

    }
    set
    {
        ViewState["ExtendedText"] = value;
    }
}

Usual issues with the above code

The mistakes we do in writing/copying the above code will result in some minor issues later. Even if there is only a small change in the key of the view state, it cannot be identified during the compilation of the program. For example, an issue cannot be identified in the above program if you write ViewState[“ExtendText”] when setting the View State property (it was “ExtendedText” and not “ExtendText” originally). Each time, we have to write similar code.

The below are the solutions for this problem:

  1. Simplify the code using a common method for getting and setting properties.
  2. C#
    public static object GetViewState(StateBag ViewState, 
                  string ViewStateName, object DefaultValue)
    {
        object Value = ViewState[ViewStateName];
        return (Value != null) ? Value : DefaultValue;
    }
    
    public static void SetViewState(StateBag ViewState, string ViewStateName, 
                                    object DefaultValue,object Value)
    {
        if (Value!=null)
        {
            bool bEquals = (Value.Equals(DefaultValue));
            ViewState[ViewStateName] = (!bEquals) ? Value : null;
        }
    }
  3. Call the methods inside the properties.
  4. C#
    public string ExtendedText
    {
        get
        {
            object _Text = GetViewState(this.ViewState, " ExtendedText", string.Empty);
            return (string)_Text;
        }
    
        set
        {
            SetViewState(this.ViewState, " ExtendedText ", string.Empty, value);
        }
    }

Create the above code using some templates or tools, like the attached sample tool. The tool does the following.

About PropertyGeneratorTool

The tool has the input controls to:

  1. Provide the name of the property
  2. Provide the type
  3. Provide a default value
  4. Select the default value attribute as needed
  5. Select if only the get method should be generated

Once you specify the above details, clicking on the Generate Code button will provide you with the code for the specified property in the RichTextBox control.

How the tool Works

As mentioned before, this is a simple idea and any beginner programmer can do this in their own way. Currently, the code is generated when we click on the Generate Code button in the tool by calling the “GeneratePropertyText” method, which gets the name, type, and the default value text, and creates the code based on the options selected for generating the get and (or) set methods, and generating the default value attribute. You can modify the GenerateGETText/GenerateSETText methods based on the way you want your code to be.

Example: if you have your SetViewState code in another class called BaseManager, then you can modify the following line in the GenerateSETText method:

C#
stb.Append("BaseManager.SetViewState(this.ViewState," + "\"" + 
   this.PropertyName.ToString() + "\"" + "," + 
   _Value1 + "," + "value" + ") ;" );

Sample output

ToolUI.gif

Runtime1.gif

Runtime2_Get.gif

Runtime3_DefaultAttribute.gif

Conclusion

This is just an idea for creating your own templates for the scenario I have mentioned above. There might be some other ways to achieve this, which I might not be aware of. Feel free to send your feedback. Thanks for reading.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
I am a software developer from Chennai, India. From Novemeber 2004, I am in to the .NET Framework and I enjoy writing codes in C#. My experience in .NET Technology includes designing and creating custom controls and application for Web and windows platform.

Comments and Discussions

 
GeneralWould be better as a Code Snippet rather than a separate tool Pin
Alex Furmanski16-Apr-08 4:53
Alex Furmanski16-Apr-08 4:53 
GeneralVB Version Pin
Dhillon14-Apr-08 20:30
Dhillon14-Apr-08 20:30 
GeneralRe: VB Version Pin
Sivastyle15-Apr-08 3:26
Sivastyle15-Apr-08 3:26 
GeneralRe: VB Version Pin
Sivastyle16-Apr-08 3:05
Sivastyle16-Apr-08 3:05 
GeneralAnother way is... Pin
Rafael Nicoletti14-Apr-08 4:04
Rafael Nicoletti14-Apr-08 4:04 
GeneralRe: Another way is... Pin
Sivastyle15-Apr-08 3:27
Sivastyle15-Apr-08 3:27 
GeneralRe: Another way is... Pin
Alexandru Matei3-May-09 23:00
Alexandru Matei3-May-09 23:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.