November 2006 - Posts
The problem: I have a two-dimensional object array. Each first level element of the array contains five object elements--except for the last one, which only has three. However, I have a procedure that needs to concatenate another array at the end of the original one. Since I need to put the unique three-element object array at the end of the list, I had to find a solution to sort it via the elements' length.
The immediate solution was to use ArrayList.Sort(IComparer). Okay I created a class that looked similar to this:
class MyComparer : IComparable
{
private object _data;
public object Data
{
get { return _data; }
}
public MyComparer(object data)
{
_data = data;
}
int IComparable.CompareTo(object obj)
{
MyComparer mc = (MyComparer)obj;
object[] myData = this._data as object[];
object[] thatData = mc.Data as object[];
return (myData.Length
}
To use it in one of my methods:
private static object[] _sortChartData(object[] oData)
{
ArrayList arrData = new ArrayList();
for (int i = 0; i < oData.Length; i++) {
arrData.Add(new MyComparer(oData
));
}
arrData.Sort();
for (int j = 0; j < arrData.Count; j++) {
arrData[j] = (arrData[j] as MyComparer).Data;
}
return arrData.ToArray();
}
With this I can sort the contents of the original object[] to the one desired.
I've come across Predicates in dotNet 2.0. I felt so disgusted with too many foreach loops when looping through list collections. So, here was my problem: I have three string Lists, say, A, B and C. List string A is the reference list; while B and C contain elements found in A. By brute force method (and practically the only way we can do it in dotNet 1.x), we do something like:
foreach (string strA in A)
{
foreach (string strB in B)
{
if (strB.Contains(strB)) { /* do something... */ }
}
foreach (string strC in C)
{
if (strC.StartsWith(strC)) { /* do something... */ }
}
}
In dotNet 2.0, Predicates are provided for the generic features such as the System.Array and System.Collections.Generic.List classes. These classes provide methods like Find, FindAll, FindLast, etc. that use predicates to help developers search for certain elements in collections via delegates rather than looping at each element. Developers get the ability to "walk" an entire data structure, determining whether each item meets a set of criteria, without having to write the boilerplate code to loop through each row manually. In addition, it is more efficient to go.
Okay, back to our example. By employing predicates, I created a new class called StringFilter:
public class StringFilter
{
private string _strReference;
public bool ContainsString(string s)
{
return s.Contains(_strReference);
}
public bool StartsWithString(string s)
{
return s.StartsWith(_strReference, StringComparison.InvariantCultureIgnoreCase);
}
public StringFilter(string strReference)
{
_strReference = strReference;
}
}
Now, rewriting our example using this new class as predicate:
foreach (string strA in A)
{
StringFilter sf = new StringFilter(strA);
if (B.Exists(sf.ContainsString)) { /* do something... */ }
if (C.Exists(sf.StartsWithString)) { /* do something... */ }
}
The new class provides flexibility in searching elements of the collection.
This code returns the volume serial for a hard drive (VB6).
CodePublic Declare Function GetVolumeSerialNumber Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As Long, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, ByVal lpMaximumComponentLength As Long, ByVal lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As Long, ByVal nFileSystemNameSize As Long) As Long
Public Function VolumeSerial(DriveLetter) As Long
Dim Serial As Long
Call GetVolumeSerialNumber(UCase(DriveLetter) & ":\", 0&, 0&, Serial, 0&amp;, 0&, 0&, 0&)
VolumeSerial = Serial
End Function
Example UsageMsgBox VolumeSerial("C")
It is officially released.
The Microsoft .NET Framework 3.0 is the new managed code programming model for Windows®. It combines the power of the .NET Framework version 2.0 with new technologies for building applications that have visually compelling user experiences, seamless communication across technology boundaries, and the ability to support a wide range of business processes. These new technologies are Windows Presentation Foundation, Windows Communication Foundation, Windows Workflow Foundation, and Windows CardSpace. The .NET Framework 3.0 is included as part of the Windows Vista™ operating system; you can install it or uninstall it using Windows Features Control Panel. This redistributable package is for Windows XP and Windows Server 2003.
Downloads:
The recent Office 2007 RTM presents a bug when opening buildingblocks.dotx. The solution for this is to delete the file and let Office recreate it. It can be located in:
%drive%:\Documents and Settings\%username%\Application Data\Microsoft\Document Building Blocks\1033
Apparently, you need to set the SelectionLanguage property of a JavaScript XML Document object to XPath so you can select nodes via XPath.
var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
// or var xmldoc = new ActiveXObject('Msxml2.DOMDocument.3.0');
xmldoc.async = "false";
xmldoc.loadXML(xmlData);
xmldoc.setProperty("SelectionLanguage", "XPath");
After which you can use XPath to select nodes. This is inevitable for MSXML 3.
I needed a hashtable implementation in JavaScript and here's what I’ve come up. It is used like this:
var ht = new Hashtable();
ht.put(“key”, “value”);
var val = ht.get(“key”); // returns null if not found
The implementation is like this:
function Hashtable(){
this.hash = new Array();
this.keys = new Array();
this.location = 0;
}
Hashtable.prototype.hash = null;
Hashtable.prototype.keys = null;
Hashtable.prototype.location = null;
Hashtable.prototype.get = function (key) {
return this.hash[key];
}
Hashtable.prototype.put = function (key, value) {
if (value == null)
return null;
if (this.hash[key] == null)
this.keys[this.keys.length] = key;
this.hash[key] = value;
}
Songbird™ is a desktop Web player, a digital jukebox and Web browser mash-up. Like Winamp, it supports extensions and skins feathers. Like Firefox®, it is built from Mozilla®, cross-platform and open source.
I stumbled upon this
site that provides quick reference to Web development.
In VB6, in order for you to make the Printer object in VB6 recognize the return of the CommonDialog.ShowPrinter method, you have to set something like:
Printer.TrackDefault = True
' then show the printer dialog
commonDialog1.ShowPrinter
The only problem here is that the commonDialog.DefaultPrinter is set to True by default. Thus, it practically sets the default printer to the selected. Any subsequent calls to the Printer object follow the printer selected in the Printer Dialog Box. Essentially you are setting a default printer via the dialog.
I have yet to find a way to retrieve the hDC (printer device handle) from the CommonDialog Print dialog such that it can be passed to the Printer object--if it indeed is possible. Because right now, I don't think this is the most efficient way to do it.
When using Ajax Pro, you have to add the runat="server" tag on the form to be used. In other words, you have to have at least one server-side form in the class whose type is to be registered as an Ajax class. Otherwise, you'll end up with a Compiler Error CS0030 Error Message Cannot convert type 'type' to 'type'. It took me almost an hour troubleshooting this.
A student/colleague during the recent Ajax Lecture at Mapua brought up
Anthem.NET. It is an Ajax "framework" for .Net web applications. Since it's the first time I heard about it, I took some time learning it and how it works.
From what I've found so far, it makes it easier for developers to build Ajax-based dotNet web application. For instance, with the widespread AjaxPro library, you have to manually implement Ajax on each method to be invoked. Ajax classes can be attributed as such, yes, but if you want to make custom controls with Ajax implementation, you have to make it for yourself. In a nutshell, since AjaxPro is an Ajax "wrapper" that you can just reference to, you still have to get your hands dirty in creating the actual rich controls Ajax is famous for.
Anthem.NET has made it easier by packaging custom controls wherein developers can use the familiar "runat=server" tag and attributes such as PreCallBackFunction, PostCallBackFunction and CallBackCancelledFunction (among many others). These attributes are valid server-side control attributes that can call client-side JavaScript functions. This is interesting because, you have more control on your Ajax invocations.
I believe the backbone of Anthem.NET is very much like AjaxPro. Only Anthem.NET pushed it further with pre-built classes and server-side controls. It's actually more like Atlas.
... there is always a well-known solution to every human problem -- neat, plausible, and wrong. -- H. L. Mencken (1880-1956), "Prejudices"
This quote made me rethink my primary notions on my previous blog: Yes perhaps, the simplest solution is not that simple at all! It could be complex enough to be wrong.
OptimizationIn general, there are two kinds of code optimization: one for speed and another one for space. By speed I mean the code executes in the least amount of time. By space I refer to code that executes in the least number of lines. It is oftentimes difficult to balance the two. But when you do, you get code that is atomic and easy to maintain.
Yesterday I presented
Asynchronous JavaScript and XML (Ajax) to a bunch of MSCS graduate students at the Mapua Institute of Technology. I needed to adjust my lecture to a level where most students (even those without or with minimal webapps experience) will be able to relate to.
It was a great opportunity and privilege for me to be able to present my understanding of Ajax. I was also a learning experience for me.
My Powerpoint presentation (with notes) can be downloaded here. The sample codes I used can also be downloaded and reviewed using this link.
I remember doing my first few C/C++ programs in college using the Turbo IDE , which was still run in DOS. Now Borland is reviving it and packaging it with much more. Check out this
link for details.
On Aug. 8, the company's Developer Tools Group, which is up for sale, is scheduled to announce single-language versions of the components of Borland Developer Studio, the company's IDE (integrated development environment) for Microsoft Windows and .Net applications.
More Posts
Next page »