Wednesday, 25 April 2007

Convert integer array to string array

Today I needed to write some code to convert an integer array to a string array. Somebody told me to iterate over the integer array and fill the string array.

I said to myself NO WAY. .NET has to have some build-in functionality to do this stuff. And it does. Array.Convert Generic Method is the name.

This is the code:

private void TestMethod(int[] intArray)
string[] stringArray =
new Converter<int,string>

string result = string.Join(",", stringArray);

private string ConvertIntToString(int intParameter)
return intParameter.ToString();

Greetz,                                                                              G

Wednesday, 18 April 2007

Serializing viewstate to object

In an earlier article, concerning a refactoring to a State machine, I stated the problem of passing the entire viewstate to the state machine because of the need for some viewstate items.

Actually I needed a total of 5 viewstate items. So passing the entire viewstate was definitely not a good idea. After reading some stuff about serialization I solved the problem.

Let me explain the steps.

  • Create an class which contains the needed properties
  • Override LoadViewState and SaveViewState in the page
  • Load the object from the viewstate in LoadViewState
  • Save the object in SaveViewState
  • Pass the object into the state machine instead of the actual viewstate

This is how the code looks like:

public class MyOwnViewState
private Object _myViewStateField;

public Object MyViewStateProperty
get { return _myViewStateField; }
set { _myViewStateField = value; }

Notice the [Serializable] attribute.

Next we override the Loading and Saving of the viewstate.

protected override void LoadViewState(object savedState)
if (savedState != null)
object[] myState = (object[])savedState;

if (myState[0] != null)

if (myState[1] != null)
= (MyOwnViewState)myState[1];

First we check if the viewstate isn't null. Next we put the it in an object array. Finally we pull the custom object out of the object array and put it in a private field so we can use it.

Saving works in a similar way

protected override object SaveViewState()
object baseState = base.SaveViewState();
object[] allStates = new object[3];
0] = baseState;
1] = _myOwnViewState;
return allStates;

We save the state as a cumulative array of objects. And we add our custom object to the array.

The only thing that's left is use the custom object in the state machine

WizardFlowControl wizardFlowControl =
new WizardFlowControl(_wizardControl, _myOwnViewState);


That's it. Greetz, G


Friday, 13 April 2007

No more custom section handlers in .NET 2.0

With the framework 2.0 we don't need to write custom section handlers for the web.config file anymore.

Now there are property settings we can use.

Choose the project properties. Then select Settings. There we fill in the key/value pairs. Choose Application for the scope and save.

Now VS has created an app.config file in the project. In the app.config file there's a section group with a section defined in it. There's also an application settings section in it.

Copy section to the section group in the web.config file. Copy the application settings section also to the web.config file.

Delete the app.config file.

Now lets call the settings in your code like this

string mySetting =

When you want to get a setting dynamically, use this code

object myDynamicSetting = Properties.Settings.Default.Properties[MyDynamicSettingsKey].DefaultValue;

Greetz,                                                                               G