General Advice, FxCop...

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Well,

I've seen other people's posts tagged with FxCop all around the place, and well I knew of it, and figured I'd give it a try. Some of its more helpful aspects were readily apparent, catching unused locals and private fields, etc, but it seems a bit ... picky about things that well, I don't think are worth the effort. For example, I tend to create a lot of constants for things, and like a good C/C++ programmer, i usually All caps my consts to signify visual right off the bat that this identifier is representing a constant, and FxCop complains about appropriate cases and such. Whatever.

A few warnings came up that seem to befuddle me, and in the end I'm not sure how to resolve the issue. Case and point: Array Constants. I like arrays. Simple, easy, and pretty straightforward, and used to be (though i'm not sure with regards to .Net) parallel to simple types in that they didn't have all that pesky object overhead.

The problem is that with VB it has always complained about declaring Constant arrays, as if there would never be any purpose to having a list of items that will never change and their inter-relationships would make sense to refer to them in an array, especially when utilizing Enumerated types as integral indicies:
VB.NET:
  Public Shared ReadOnly SQL_CMDS() As String = {SQLSELECT, SQLINSERT, SQLUPDATE, SQLDELETE, 
         SQLCREATE.Substring(0, SQLCREATE.Length - 6), SQLDROP, SQLTRUNCATE, SQLSELECT & " " & SQLINTO}

Now, All of those SQL* are declared public constants in the same class, as the class was intended to be 100% static, or shared as it is called in VB. My idea is simply to have a repository of all the commands and strings etc related to SQL statements. (or at least a fair number of them for basic SQL syntax).
however, FxCop has indicated that this does not protect the individual elements of the array, and thus I cannot do this:
VB.NET:
SQL_CMDS = New String {"",""}
But I could do this:
VB.NET:
SQL_CMDS(2) = "Hello"

If that is the case, what would be a good recommendation for designing this with the intended effect. I want a "readonly constant array". A static element that has x number of elements, and cannot be changed nor can its elements be changes. In C/Delphi these are utilized to reduce overhead of the application, as they are delegated to a reserved memory space of constant values, but so far in VB it has not seemed to react in a similar manner.

As a continuation, I will be continually posing other FxCop type issues that the online help is not too fully versed in explaining, as tragically, I come at all programming languages from the computer side not the human side, so I typically (as I'm sure has been evident) don't like a language telling me how to program the computer or forcing me into a narrow view of what these systems can do. However, I am aware that many language based optimizations do occur and perhaps the effect i want is just not evident in the traditional style of application development.

Thanks (one of these days I might actually call myself a VB.net programmer)
 
Perhaps a Collections.ObjectModel.ReadOnlyCollection ?
 
Perhaps a Collections.ObjectModel.ReadOnlyCollection ?

Well, I could do that. I guess i'm more interested in finding out about the overhead. Like, in C/Pascal, we can have three different types: (i'll use pascal as the reference cause the syntax is more legible)
VB.NET:
var
  X: integer;
  Y: Real;
  Z: Array[0..3] of Integer

Now we'll use the Stack for this reference because there's no need to go further into memory management, but if the given stack segment for that method is say, $A000000 so, the address map goes as follows:
VB.NET:
  X: $A000000-$A000003
  Y: $A000004-$A000009
  Z: $A00000A-$A000019

Thus:
Z[0] -> $A00000A
Z[1] -> $A00000E
Z[2] -> $A000012
Z[3] -> $A000016
So the creation of an array is just a larger memory block on the heap (or stack) that takes no overhead to manipulate. So in many circumstances it is faster, easier, and less burden on the system to utilize an Array for the list of items.

Now in the same situation, if In Pascal or C++ i were to create a "ReadOnlyCollection" that would then create a full object on the stack/heap that would not only contain the primary address space of the object instance, but the vtable, the data member addresses, method addresses, property addresses, etc...so for something that would be a grand total of say 20 bytes of space in memory in array format, is now some 200+ bytes of memory, not to mention the overhead of calling methods to retrieve the data instead of direct addressing.

Now I gather that VB does things quite a bit differently, which is where my curiosity comes into play regarding how VB treats arrays versus how it treats objects. Is the nature of a VB.Net ReadOnlyCollection so integrated into the system that it is indeed less of a burden on the system than creating an array? I only ask because I've written a vast number of methods that utilize arrays to handle things. In Pascal/C i would have declared a contanst String and used things that way, but VB's string/char handling is so backwards that it was easier to use an Array of Char.
Example:
VB.NET:
SINGLEQTE = new Char() {"'", "'"}
ANGLE = New Char() {"<",">"}
BRACKET = New Char() {"[","]"}

With those I use them to Do Automatic grouping of strings via extensions:
VB.NET:
MyString = "tbl_Customers"
MyColumn = "Column 1"
Mystring.Bracket & "." & MyColumn.Bracket
---------------------------
Result: "[tbl_Customers].[Column 1]"

And many others, like a split() function that allows for grouped elements, so it doesn't split on commas in the middle of a Parenthetical group, etc.
All of these are based on "Constant Arrays" or there abouts. I never assign values to those arrays, but seeing what FxCop said, it appears that there is a hole for some impropriety....not that anyone else will use my code here, I just thought I'd try to learn something more language specific in the programming realm.

Thanks
 
Back
Top