Tuesday, January 31, 2006

on stupid little tricks: how to sort a table by multiple columns in MSAccess

it's easy to sort by one column, you just right click on it and click sort ascending or sort descending, but to sort by more than one column (without creating a query) is a little tricky, here's what you have to do (Windows keyboard required)

you have to put the columns that you want to sort for, all together in order from left to right, you do this by clicking on the column header, and dragging it over

so if you have columns
A B C D E

and you want to sort by E, then by C you have to drag the E column so you have

A B E C D

next
- click on the first column you want to sort for,
- press and hold the [shift] key
- and click on the last column you want to sort

in the example, we click on column "E", press shift and click on column "C"

A B [E C] D

you then press the menu key menu key and select sort ascending or sort descending

you can now move the columns back to their original position or wherever you want to have those columns

if you close the table it will ask you to save (the layout, position of columns, columns sorted by, etc) your changes, if you do save the changes, next time you open the table it will be sorted by the columns you specified and *everything* will be the way it was when you closed it

I often create programs with complicated configurations, and I store the configuration in access table(s), yes I could use XML, but MSAccess is so much easier to program against from different languages and you have a whole GUI at your disposal to design and enter data, and tricks like this just make life easier

Monday, January 30, 2006

on stupid little tricks: how to bring up the system properties

(having a Windows keyboard (the ones with the Windows key)) simply press:
Windows Key+Pause/Break

I'm surprised how many people doesn't know this, but I am a keyboard guy, for me it is painful to look at others going through the menus to find these kind of windows, which in case you are wondering is
- Start button
- Settings
- Control Panel
- System

Monday, January 23, 2006

Convert.ToInt32('0') not the same as Convert.ToInt32("0") (C#)

depending on your mileage and prior programming languages experience you might already be well aware of this issue.

...this wasn't my case, the other day we hit a bug with an algorithm that calculates a check digit, my colleage had translated the code from vbscript and made sure that it matched the VBScript version, but we were still getting incorrect check digits so I gave it a try debugging side by side with another version in Delphi

I found the bug in the first iteration

the code was something like:
char c=someString[someCounter];
int i = Convert.ToInt32(c);
total+=i;
...

turns out Convert.ToInt32(c) returns 48 and not 0 (for c='0') as we were expecting, this is really easy to fix once you know but can cause some really odd bugs

leason learned, next time I need to convert a char (that I know is a digit) to an int, I'll just use

char c = '0';
int i = c-48;

=o)
FireFox - The hungry-hungry hippo

this post is in reference to Raymond Lewallen's post about FireFox consuming way too much memory, I have been experiencing this myself for a while, but hadn't seen anyone else complain, I don't use any extension which is what some people are saying would cause the problem

at home (I have WXP) I use Internet Explorer 7 and it works great, there are some issues with some sites that just won't work with IE7 so I end up using FF from time to time
at work however I use W2000 so I use FireFox

but after seeing all the responses to Raymond's post, that's it, I'm trying Opera, I have heard many good things about this browser for a long time, I think I tried it once and didn't like it, we'll see this time

Wednesday, January 18, 2006

Tetris 1D, for the tetris fans out there... not reallly

just found this link while backreading blogs
http://www.tetris1d.org

funny...

Tuesday, January 17, 2006

exposing a .NET assembly to be used as a COM object (from any language like Delphi, VB, etc)

this is really simple but many programmers don't know just how simple it is, the steps are:


- create a class library
- add using System.Runtime.InteropServices;
- add these attributes to the class you want to expose
[ComVisible(true),
Guid("YOUR GUID HERE"),
ClassInterface(ClassInterfaceType.AutoDispatch)]
- each method that you want to expose needs to have the [ComVisible(true)] attribute specified
- the methods you expose CANNOT be static

that's it!

here's a complete class library that provides a method to encrypt a string:

using System;
using System.Security.Cryptography;
using System.Text;
using System.Runtime.InteropServices;
//[assembly: System.Runtime.InteropServices.ComVisible(true)]
namespace LoginHash
{
///
/// Provides a method to hash strings using MD5
///

///
[ComVisible(true),
Guid("A8E2505C-8A1B-49F9-AFD1-54B79B26040C"),
ClassInterface(ClassInterfaceType.AutoDispatch)]
public class Login
{
[ComVisible(true)]
public string Encrypt(string cleanString)
{
Byte[] clearBytes = new UnicodeEncoding().GetBytes( cleanString );
Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
return BitConverter.ToString(hashedBytes);
}
public Login()
{
}
}
}


so you see, it's really easy to export all the functionality that you have in .NET to other applications / languages using COM. Next I'll post examples on how to use this COM object

Monday, January 16, 2006


you begin to wonder if you have too many programs installed when you have a 21" monitor with a 1280x1024 resolution and you are going for the third column in your main menu...
(Visual Studio) Linked files gotchas, why do I get the same error/warning twice (more than once)

when you link to another file in the same solution (different project) you end up with a file in one project and the same file (virtually) in another project and so when you build the solution, which is what one usually does, you get duplicated errors (same error, same file, same line number) because all of the projects get compiled and if there is an error in the file that is linked, you get
- one error for the project the file belongs to,
- and another error (duplicated) for the project where the file is linked

there are different workarounds, most important is the fact that you should simply be aware of this behaviour, to avoid this there are different workarounds
- only build the solution if you have to, remember that the solution builds all of the projects in it, so if you are working in one of the projects of the solution, instead of building the solution, build only the project you are working on, to do this:

- from solution explorer, click on the project you want to build
- from the Build menu click "Build [your project here]"

an easier way to do this is to customize your tool bar, to do this
- right click anywhere in your tool bar (where all the little buttons are)
- click customize
- click on the commands tab
- select the "Build" category
- find the "Build Selection" option, drag that over to your toolbar, and drop it where you want it

you can even go further and assign a shortcut to it
- while having the "Build Selection" option highlighted click the "Keyboard" button (at the bottom)
- in the "show commands containing" box, type "Build" (without the quotes)
- highlight the Build.BuildSelection
- go to the Press shortcut key and press the key combination that you want to use for that, if there are any conflicts it will tell you where that key combination is being used in the "shortcut currently used by", in most cases you should use one that is rarely or not used, once you have the key combination, click assign, it might ask you to save a copy of your current default settings, you can just assign the name of the backup copy and click yes

now you can use that combination to quickly build only the project you are working with and not have all those duplicated errors

Saturday, January 14, 2006

hook up a Delegate to a method obtained through reflection

I've been looking for this and finding people with the same problem, and the answers I found all were

"use Delegate.CreateDelegate"

sure, that's the answer, but what parameters or what do I pass to it?

the closest thing I found to a complete answer was this article
http://msdn2.microsoft.com/en-us/library/ms228976.aspx

but the answer was not quite clear, so here it is, hopefully simple enough to be of help for you

//*** these are required to load the assembly
object instance;
Type type;
//*** declare the Delegate (the pointer to the method obtained through reflection)
Delegate GenericMethod;
//*** declare the delegate signature
delegate string StringMethod(string name);


//*** load the assembly (you need include System.Reflection)
Assembly asm = Assembly.LoadFile(@"f:\ScriptLibrary.dll");
type = asm.GetType("ScriptLibrary.ScriptClass", true);
instance = Activator.CreateInstance(type);
//*** hook up the delegate to the method

GenericMethod = Delegate.CreateDelegate(typeof(StringMethod), instance, "method1");

//*** calling the method through the delegate
string s;
s = (string)GenericMethod.Method.Invoke(instance, new object[] {(string)"Eber"});


why would you want to load the method in a delegate instead of using InvokeMember?
according to Eric Gunnerson
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp02172004.asp

is much faster to use a delegate, than to use InvokeMember

by the way, on his article he didn't use a MethodInfo, which is close in speed to the delegate

so...
delegate - fastest
MethodInfo - close to the delegate speed
InvokeMember - slowest

hope this helps

Friday, January 13, 2006

Petals around the rose
I got this puzzle from Geoff Appleby blog

pretty cool, I'm sending it to my friends
http://crux.baker.edu/cdavis09/roses.html

There are five dices

the rules are simple:
- you roll the dices
- and then enter how many petals you think there are around the rose
- press "check" to verify your answer
- repeat the steps until you get consistent correct answers
- Once you have figured it out, don't spoil the fun for others

just for the record, it took me 3 attemps to make a correct guess, then I confirmed it in the fourth and fifth, which according to "Dr Duke", I'm not very smart =o(

Thursday, January 12, 2006

two chinese with a blog

a few weeks (months?) back this video became pretty popular, it is hilarious
http://video.google.com/videoplay?docid=-6739710473912337648

turns out, those two guys have a blog! in there you can find more videos just like that one
you can visit it here
http://twochineseboys.blogspot.com/

and have a good laugh
(Visual Studio) linked files gotchas: how do I link to a file?

I'll start a series of gotchas on linked files, but first of all, how do I link a file, and why would I want to link a file?

to link a file:
from solution explorer
- right click on your project
- Add
- Add existing item
- find the file you want to add
- instead of clicking on the Open button, click on the little arrow on the open button, then select "Link file"

a linked file can be very useful for different things, it doesn't copy the file to your project folder, a linked file is just a reference to a file somewhere, the file exists only "virtually" on your project, but it behaves just like if the file was part of the project

I use linked files for my tests, for almost every project now I create another project "TestCases" and I link to the source files of the main project to be able to test individual functions of the classes of the main project, it makes life a lot easier (especially when making changes)

however there are some gotchas about linked files, I will post the ones I have been faced with so far

Tuesday, January 10, 2006

the dependency 'file' in project 'project' cannot be copied to the run directoy because it would conflict with the dependency 'file'

This error message is saying,
- there are two versions of the same DLL in your project,
- somewhere you have a reference to the old one (most likely this is the problem)
- but VS can't copy it to the run directory, because that would overwrite the new version that you have,
...I hope I didn't make matters worse, but you have to understand the problem so you know what to look for

When you are writing a project that uses an assembly (class library) that you are still working on, you are likely to find this problem when you make changes to the class library and copy the new DLL to the project that uses it, the error message doesn't always point you in the right direction, here's a list of things to look for

1) the first thing you would try is simply copy the new version of the DLL to the project path (or wherever you keep your DLLs for the project), but most likely you already tried that and that's why you're looking for some other solution

2) the Microsoft recommendation to fix this problem:
http://msdn2.microsoft.com/en-us/library/3b12we19.aspx
- Make one of the assemblies a direct reference of your project. A possible downside to this approach is that the assembly you choose is not guaranteed to work with assemblies that require some other version of the referenced assembly.
- or -
- Make sure that both copies of the assembly are strong-named and in the global assembly cache. This eliminates the need to copy the assemblies to the bin directory.

there are different reasons why you wouldn't/couldn't/wont do that, so if your problem is still not fixed, keep reading

3) you can also open in notepad the project files .csproj (or .vbproj for VB) and look for the name of the offending dependency, (you have to open a folder the directory where your project is located and find those files, i.e. if your project is called MyProject, you look for MyProject.csproj)

assemblyname="SomeAssembly"
hintpath="DLLs\SomeAssembly.dll">

that gives a hint to the IDE on where to find the reference, (just a hint), make sure that path is not the problem, if you don't see anything wrong there, keep going

4) open in Notepad the project file .csproj.user (or vbproj.user for VB) and look for these lines

(VisualStudioProject)
(CSHARP LastOpenVersion = "7.10.3077" )
(Build)
(Settings ReferencePath...
- note: the parenthesis should be <> instead but freaking blogger doesn't work right
make sure you don't have copies of the DLL in question in those folders that would cause the conflict.
If you still don't see anything wrong with the paths (you know that in those paths you have the right version of the DLL)


5) then perhaps the problem is not in the current project; another cause of this problem is when your main project uses a DLL that references the same DLL that is causing the problem, to illustrate look at this:

main project
- reference DLL-A
- reference DLL-B

DLL-A
- references...
DLL-B
- reference DLL-A <<<=== the problem is here! your project indirectly has a reference to the old DLL here

in this case, to solve the problem you have to copy the right version of DLL-A to the references of DLL-B (basically perform the steps above but for the DLL-B project), so that when your project uses DLL-B, it will not have the reference to the old DLL-A

uufff... I hope I made some sense and that this helps someone