Click here to Skip to main content
Click here to Skip to main content

Property Generator Tool

, 12 Apr 2008
Rate this:
Please Sign up or sign in to vote.
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:

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. 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. 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:

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)

About the Author

Sivastyle
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 PinmemberAlex Furmanski16-Apr-08 4:53 
GeneralVB Version PinmemberDhillon14-Apr-08 20:30 
GeneralRe: VB Version PinmemberSivastyle15-Apr-08 3:26 
GeneralRe: VB Version PinmemberSivastyle16-Apr-08 3:05 
GeneralAnother way is... Pinmembertkrafael_net14-Apr-08 4:04 
GeneralRe: Another way is... PinmemberSivastyle15-Apr-08 3:27 
GeneralRe: Another way is... PinmemberAlexandru Matei3-May-09 23:00 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 13 Apr 2008
Article Copyright 2008 by Sivastyle
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid