Contents tagged with .NET
-
Unexpected behaviour: Defining values in enums
I don't normally regurgitate other blogger's posts like a headless chicken, but in this case it's worth mentioning.
Stephen, a colleague of mine, and a picky sod (who puts their enums in alphabetical order? - no serious comments on that remark, pulllleazee!), ran into some unexpected behaviour with regards to enums. Worth a read.
-
Dump the Module keyword in VB.NET!
Though I'm mainly a C# developer, I now and then get exposed to some VB.NET stuff. No, this is not going to be a C# vs. VB.NET debate. We've seen enough heated arguments and flame wars on that topic over the years.
Something about VB.NET Console applications created in Visual Studio.NET (all versions), bugs me though: the dreaded Module keyword. The default skeleton for a Console app in VB.NET looks like this:
Module Module1
Sub Main()
End Sub
End Module
Whilst under the hood, a module is simply a class with static members and a private default constructor to prevent instantiation, I don't think its use should be promoted like that. And I really wonder why MS hasn't changed the default Console app skeleton to look as follows:
Class Program
Shared Sub Main()
End Sub
End Class
In my opinion, the Module keyword shouldn't have even existed in VB.NET. It's one of the reasons why a lot of VB.NET code I've seen simply gets dumped in a Module, and Object Oriented Programming goes out the window. Of course, there's nothing stopping you coding like that in VB.NET without using the Module keyword, or even in C# for that matter. But it is a step in the right direction in trying to get developers to think about object oriented class design first (static/shared vs. instance members etc), before shoving anything and everything in a Module. -
ViewState, CheckBox, ControlState...errr...
J. Michael Palermo writes about the CheckBox in ASP.NET maintaining its state regardless of the EnableViewState attribute to False in ASP.NET 2.0.
Hang on - isn't that the same in ASP.NET 1.0 and 1.1? Oh yes.
All web controls that implement the IPostBackDataHandler maintain their basic 'value-state' (for lack of a better description) using the HTTP POST form collection. Irrespective of setting the EnableViewState property for the web control to False.
Nothing has changed in ASP.NET 2.0 as far as that is concerned. The CheckBox control along with TextBox and other controls that implement the IPostBackDataHandler interface, still maintain their basic value-state using their respective HTTP POST values. ControlState has nothing to do with this.
ControlState is a way to allow more fine-grained control over individual portions or behavioural elements of a web control, which under the bonnet actually still uses the ViewState statebag. Fritz Onion's article outlines this nicely and in more detail. -
ArraySegment Structure - what were they thinking?
From the MSDN docs:
ArraySegment is a wrapper around an array that delimits a range of elements in that array. Multiple ArraySegment instances can refer to the same original array and can overlap.
Turns out this structure doesn't even deserve the definition 'wrapper'. It simply takes the array, offset and number of elements in your segment, and sets a few properties accordingly.
Subsequently when you want to iterate over the items in your ArraySegment, you still need to use a combination of Offset and Count to achieve this. How is this different from not creating an ArraySegment and define your Offset in-situ as well as the number of elements?
I was expecting to be able to do something like this:
ArraySegment<string> seg = new ArraySegment<string>(new string[] { "John","Jack","Jill","Joe"},1,2);
// Access first item in segment
string first = seg[0];
// Iterate through ArraySegment
foreach (string s in seg)
{
Console.WriteLine(s);
}
Turns out you can't. There's no indexer for ArraySegment and no enumerator. You have to access the .Array property and use .Count and .Offset as passed to the constructor. What is the point of that!?
So I rolled my own generic DelimitedArray class which does exactly that. See the inline code below.
public class DelimitedArray<T>
{
public DelimitedArray(T[] array, int offset, int count)
{
this._array = array;
this._offset = offset;
this._count = count;
}
private int _offset;
private T[] _array;
private int _count;
public int Count
{
get { return this._count; }
}
public T this[int index]
{
get
{
int idx = this._offset + index;
if (idx > this.Count - 1 || idx<0)
{
throw new IndexOutOfRangeException("Index '" + idx + "' was outside the bounds of the array.");
}
return this._array[idx];
}
}
public IEnumerator<T> GetEnumerator()
{
for (int i = this._offset; i < this._offset + this.Count; i++)
{
yield return this._array[i];
}
}
}
Hope this is of use to someone.