Practical Tips series (1): using the indexer for unified Parameter Parsing

Source: Internet
Author: User

Glossary in msdn:The indexer allows instances of classes or structures to be indexed in the same way as arrays. The indexer is similar to an attribute. The difference is that their accessors use parameters.

Problem scenario:Simulate the drag module of Google, and set different parameters for each module, so that the display mode or data of each module is displayed according to the set parameters. Then the problem arises. Because different modules correspond to different parameter settings and these parameters are not uniform, how can we use a uniform method to process these parameters? Currently, we have set the parameter Resolution Method for sharing:

1. When the parameters of each module are saved, all the module parameters are parsed to a specific string format: parameter 1 name = parameter 1 value @ parameter 2 name = parameter 2 value, the benefit of doing so is that no matter how many modules you have or whether the parameters of each module need to be added in the future, the resolution of these parameters is not affected, making it easy to expand.

2. When the front-end needs to present these modules, upload the parameter strings saved in 1 to their respective modules, and then perform anti-resolution based on the parameter format, to get the original value,CodeAs follows:

Protected void initparams (Dictionary <string, string> param, string paramvalue)
{
String [] aparams = paramvalue. Split ('@');
Int length = aparams. length;

For (Int J = 0; j <length; j ++)
{
Int I = aparams [J]. indexof ('=', 0 );
If (I! =-1)
{
String [] aparam = new string [2];
Aparam [0] = aparams [J]. substring (0, I );
Aparam [1] = aparams [J]. substring (I + 1, aparams [J]. Length-I-1 );
Param [aparam [0] = aparam [1];
}
}
}

In this way, when the module is loaded, we can get the corresponding value through Param ["parameter name"] to perform corresponding operations, which is indeed very convenient. In addition, there was no problem at the beginning, and everything was running normally. As the module is upgraded, the parameter items need to be added continuously in future upgrades. When these parameters are updatedProgramWhen, an exception occurs when parsing the original module parameters. This is because the parameter item is added and the judgment of this parameter item is added to the program, for example, Param ["pagesize"], but it is already in the running module, this parameter does not exist. As a result, Param ["pagesize"] does not exist, and an exception is thrown.

Solution:

1. For this situation, I think many people will think of it at the beginning that an exception capture can be easily solved. For example:

Private int pagesize
{
Get
{
Try
{
Return convert. toint32 (Param ["pagesize"]);
}
Catch
{
Return 10;
}
}
}

This can solve the problem, but I don't think everyone is willing to do this. First, you can easily set the default value through exception capture. Second, you can do this, it means that every time I add a parameter item, I have to set the default value of this parameter for the compatibility of old versions. This is not conducive to maintenance, but also makes the code messy. Obviously, this method is not recommended.

2. Since the exception capture method does not work, how can we directly obtain the parameter value through Param ["pagesize"] without throwing an exception because it is a new parameter. This can be achieved through the indexer.

The indexer is similar to an attribute. You can use the set and get attributes to set the value, so that you can perform certain operations on the value while assigning values or values. In this way, we repackage the param parameter in the initparams function mentioned above and use the indexer to read the data in the dictionary.

Public class parammodel
{
Protected dictionary <string, string> param;

Public parammodel ()
{
This. Param = new dictionary <string, string> ();
}

Public String This [String paramname]
{
Get
{
If (this. Param. containskey (paramname ))
{
Return this. Param [paramname];
}
Else
{
If (paramname = "pagesize ")
{
Return "10 ";
}
Else
{
Return "0 ";//}
}
}
Set
{
This. Param [paramname] = value;
}
}

Public String getparams ()
{
String STR = "";
Foreach (string s In Param. Keys)
{
If (STR = "")
{
STR = S + "=" + Param [s];
}
Else
{
STR + = "@" + S + "=" + Param [s];
}
}
Return STR;
}
}

Solve the problem in this example through the indexer. In the future, reading of module parameters is not affected no matter whether the parameter items are increased or decreased.

This article analyzes the problems encountered in actual work and finds the most appropriate solution for reference only.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.