now if I could get this book autographed Jgot some reading to do J
My thoughts, articles, findings, etc. mainly about programming and computers in general.
I tried posting more stuff yesterday but blogger.com was down, I couldn't even leave comments to other blogger blogs =o(Here's a list of tips and tricks that I have compiled from my reading list, I've seen a tip here and a trick there, but not a full list, so here it is (for me to remember J)
shutdown or restart a workstation
Note: These tips work on Windows XP, but there is no guarantee that they will work in future versions of Windows.
One way to do this is to run Task Manager and select your shutdown option from the "Shut Down" menu.
Another trick is to click on the desktop and type Alt+F4. This will call up the shutdown dialog, where you get the usual shutdown options like "Shut down", "Shut down without installing updates", "Restart", "Stand by", and "Hibernate".
These next two tricks are documented and will continue to work in future versions of Windows:
If you're a command line person, you can run
shutdown.exe, but that program supports only shutdown and restart; it doesn't do stand-by or hibernate. But theshutdown.exeprogram has a serious flaw: It requires you to have administrator privileges. If you are a limited user with shutdown privileges, theshutdown.exeprogram will complain. (Which means that I don't use it.)Finally, if your computer isn't using Fast User Switching, you can type the Ctrl+Alt+End hotkey, which is the Remote Desktop version of Ctrl+Alt+Del and consequently takes you to a dialog where you can do various system-type things, among them logging off and shutting down.
Connecting to terminal services when all active sessions are used:
mstsc /console
Mapping drives via Remote Desktop
Open the remote desktop application. Click on the Options button and go to the Local Resources tab. The bottom section of the tab is titled Local Devices, and there you will see a check box labeled Disk drives. Checking this box will automagically connect all disk drives on you local machine to the remote machine when you open the RDC.
Remote Desktop on a non-standard port
Modify this registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TerminalServer\WinStations\RDP-Tcp
Connecting via remote desktop to the non-standard port
Suppose you change the port for the virtual machine to listen in on port 3900. You simply append 3900 to the server name (or IP) when connecting via Remote Desktop.
Important shortcut keys:
Alt + Page Up
Switches between programs from left to right.
Alt + Page Down
Switches between programs from right to left.
Alt + Insert
Cycles through the programs in the order they were started.
Alt + Home
Displays the Start menu.
Ctrl + Alt + Break
Switches the client between a window and full screen.
Ctrl + Alt + End
Brings up the Windows Security dialog box.
Ctrl + Alt + Pause
Toggles between fullscreen and windowed mode
(note that this does not set the client desktop to the correct size)
Alt + Del
Displays the Windows menu
Ctrl + Alt + Num -
Places a snapshot of the client's active window on the clipboard
Ctrl + Alt + Num +
References
http://blogs.msdn.com/oldnewthing/archive/2006/10/20/849575.aspx
http://haacked.com/archive/2005/10/13/Remote_Desktop_To_Console_Session.aspx
http://stevenharman.net/blog/archive/2006/10/22/Mapping_Drives_via_Remote_Desktop.aspx
http://haacked.com/archive/2006/10/17/Remote_Desktop_On_A_NonStandard_Port.aspx
The code for this article applies to C#
Reading blogs, I found this post (in italian), where Marco wants to write a function to generate random numbers with the following rules:
The function he came up with was the following:
private string GetRandom()
{
Random rn = new Random();
string resultnum=string.Empty;
do
{
string a = rn.Next(0, 9).ToString();
if (resultnum.Contains(a)!=true)
resultnum = resultnum + a;
}
while (resultnum.Length<5);
return resultnum;
}
I felt curious to see what areas I could improve, and I started writing a function to get the same result, but in a more optimized way
A couple things that jump out to my mind are:
So I wanted to write a function that: would avoid concatenation and would execute the loop exactly 5 times, this is the result:
static char[] allNumbers = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
Random r = new Random();
public string GetRandom2() {
char[] result = new char[5];
int max = 9, pos=0;
for (int i=1; i<6; i++) {
pos = r.Next(0, max);
result[i - 1] = allNumbers[pos];
//*** swap positions
allNumbers[pos] ^= allNumbers[max];
allNumbers[max] ^= allNumbers[pos];
allNumbers[pos] ^= allNumbers[max--];
}
return new string(result);
}
The technique I used was:
The performance gain from these changes was minimal (3 ms per 1000 calls), then I thought about moving the Random variable declaration outside the function, so it could be reused between calls, this gave me the performance gain I was looking for, calling the function 10,000 times takes ~114ms using the old method and only ~6ms using the new one, that's where the problem was, the rest was pretty much insignificant =o(
I'm sure someone else can write a faster version, but I accomplished my goal =o)
The code for this articles applies to C# 2.0
A more or less common task on a generic list is to remove items from it, lets see what approaches we can take:
#1 The definitive wrong way to do it:
List<Person> l1 = GetList();
//*** The wrong way
foreach (Person p in l1) {
if (p.Age > 30)
l1.Remove(p);
}
This code will throw an InvalidOperationException: "Collection was modified; enumeration operation may not execute."
#2 Kinda works (and allows you to perform an action on the item being deleted)
List<int> ints = new List<int>();
ints.Add(1);
ints.Add(2);
ints.Add(3);
ints.Add(4);
ints.Add(5);
ints.Add(6);
ints.ForEach(delegate(int i) {
if ((i % 2) == 0) {
Console.WriteLine("removing"+i.ToString());
ints.Remove(i);
}
});
If you run that code it will work just as we expect it, but there is a problem with it, if you add all the pair numbers first you would see that it doesn't delete all of them, it doesn't throw an exception, it just breaks out of the loop and keeps going, so this method works but on very specific conditions
#3 The right way: we traverse the collection backwards and delete items
int x = ints2.Count;
while (--x>=0) {
if (ints2[x] < 4)
ints2.RemoveAt(x);
}
#4 A better way: the list has a method for this purpose
ints2.RemoveAll(delegate(int i) {
return i <4;
});
#5 But what if I need to perform an action on the item being deleted?
ints2.RemoveAll(delegate(int i) {
if (i < 4) {
//*** Perform action here
Console.WriteLine("removing :" + i.ToString());
return true;
}
else
return false;
});
if the item was a class we could call methods on it before deleting it from the list (perhaps a call to a DB)
On future posts I'll talk on detail about why exactly #1 and #2 don't work
About 2 months ago I wrote a couple articles on reflection, specifically about executing another's class private methods, someone left a question on how you can get the private fields, just now I found the time to answer to that; first of all let's create a test class:
class TestClass {
private int prop1;
public int Prop1 {
get { return prop1; }
set { prop1 = value; }
}
private string prop2;
public string Prop2 {
get { return prop2; }
set { prop2 = value; }
}
private bool prop3;
public bool Prop3 {
get { return prop3; }
set { prop3 = value; }
}
public bool field4;
public TestClass() {
prop1 = 10;
prop2 = "20";
prop3 = false;
field4 = true;
}
}
Then we create an instance of it, and use the method GetFields to (you guessed) get the list of fields, we just have to specify on the parameters that we want private members and they have to be instance members (non-static), once we get the list we can do *whatever* we want with it, in this case the code prints the field type and it's value
TestClass test = new TestClass();
FieldInfo[] fields = test.GetType().GetFields(BindingFlags.NonPublic BindingFlags.Instance);
foreach (FieldInfo f in fields)
Console.WriteLine(string.Format("type:{0} \t value:{1}", f.FieldType.ToString(), f.GetValue(test)));
That's about it. Now, just to make it a little bit more interesting I created a small generic function to accomplish the previous task and now you can reuse that with any other class:
static void PrintPrivateFields<T>(T theInstance) where T:class {
Type t = theInstance.GetType();
FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic BindingFlags.Instance);
foreach (FieldInfo f in fields)
Console.WriteLine(string.Format("type:{0} \t value:{1}", f.FieldType.ToString(), f.GetValue(theInstance)));
}
To use it, we just create an instance of that class and pass the instance to the function:
TestClass test = new TestClass();
PrintPrivateFields(test);
Note that I don't need to specify the type when calling the function
PrintPrivateFields<TestClass>(test);
The compiler is smart enough to figure it out (since the only parameter is of that type).
Hope the code is useful, you can find the full code here
![]() |
| From tecblog |
Is this time of the year where I get to back to my home town to visit family and friends, I will be in Juarez & Chihuahua from Dec. 23 to the end of the year, if you're interested in meeting, have some dinner, geek talk or whatever, drop me a line and we'll arrange something
I "grew up" using Novell networks and DOS, I used to be really good at managing Novell servers but I haven't touched a Novell server in quite a while; yesterday they were having problems with an old Novell server and they asked me for help, I couldn't remember some of the basic commands, so this post is for me to remember those commands later, and maybe you are so lucky that you may need these ones sometime
Server commands
Workstation commands
Divide your program into methods that perform one identifiable task. Keep all of the operations in a method at the same level of abstraction. This will naturally result in programs with many small methods, each a few lines long.That's as simple as breaking a method into smaller methods, something you do in every day programming, there are many simple patterns like this one, all they do is describe the tasks you do on a daily basis.
Back in the days, I used to start a page with a html table with 3 rows, one for the header, one for the middle area at 100%, and a footer
Nowadays this approach doesn't work any more and we're told that we have to use pure CSS, not tables, blabla... so, how do we accomplish this with pure CSS?
We are going to need some css and a bit of javascript, first of all, lets see the css
html,body,form
{
margin:0;
padding:0;
}//prevent some default spacing on some browsers
.header
{
text-align:center;
background-color:Navy;
color:White;
width:100%;
}
.footer
{
background-color:Orange;
color:Navy;
position:absolute;
bottom:0;
border:0;
}
div.scrollable{
overflow:auto;
border-left-width:0px;
border-bottom-width:0px;
border-top-width:0px;
border-right-width:0px;
background-color:Gray;
color:White;
position:absolute;
}
the most interesting is the last class div.scrollable, overflow:auto makes it scrollable, and the rest is to eliminate the borders and set the color.
Now lets see the html:
<html>
<head>
</head>
<link rel="stylesheet" type="text/css" href="test.css" />
<body>
<div>
<div id="header" class="header" height="20px">
This is the header
</div>
<div id="mainDiv" class="scrollable">
This is the main body<br>
There is a lot of stuff in the main
<br><br><br><br><br><br><br><br><br><br>
There is a lot of stuff in the main
<br><br><br><br><br><br><br><br><br><br>
There is a lot of stuff in the main
<br><br><br><br><br><br><br><br><br><br>
There is a lot of stuff in the main
<br><br><br><br><br><br><br><br><br><br>
There is a lot of stuff in the main
<br>
<br><br><br>
This is the last line
</div>
<div id="footer" class="footer" height="20px">
<div style="float:left">This is a div on the left</div>
<div style="float:right">This is a div on the right</div>
</div>
</div>
</body>
</html>
I put a lot of lines in the middle, just so you can see the document scrolling. As a "bonus" I put two divs on the footer, one aligned to the left and one to the right, both in the same "row", this is one of those things that people have a hard time finding. We're almost there, we just need some javascript now:
var mainDivHeight;
var mainDivWidth;
function AdjustFullSize() {
var screenHeight = 450;
var screenWidth = 610;
if (window.innerHeight) {
screenHeight = window.innerHeight;
screenWidth = window.innerWidth;
}
else if (document.body) {
screenHeight = document.body.clientHeight;
screenWidth = document.body.clientWidth;
}
mainDivHeight = screenHeight - 20 - 20; //20+20 for header and footer
mainDivWidth = screenWidth - 0;
document.getElementById('mainDiv').style.height = mainDivHeight + 'px';
document.getElementById('mainDiv').style.width = mainDivWidth + 'px';
}
The different browsers have different properties to access the document height and width, we account for that and then assign the size to the main div, call this function on the body onload (and resize!) and we are done, the end result looks like this, you can get the full source from there, hope you find it useful.