Saturday, July 17, 2010

How to Find and Land a .NET Development Job

My developer job resource kit is now available. It's called "Shove It, FizzBuzz: How to find and land a .NET development job."



What's included in this kit?

The kit includes a 200+ page eBook (PDF) that teaches you how to get the job you want (and will make you a better developer in the process). It contains tips on improving your overall developer marketability, 110+ technical questions (with answers), plus general interview questions, puzzle questions, and more.

The kit also includes software that simulates an actual interview and can help boost your confidence during the real thing. You could also just use the software to keep yourself on top of the latest technologies.

Last, but not least, the kit includes a customizable resume (in Word format) that has proven to be effective at getting people jobs.

http://shoveitfizzbuzz.com



Friday, May 14, 2010

Repeating and aligning elements using jQuery and CSS

Today I had a need to create a dynamic list of checkboxes, and I wanted them all aligned properly, so the results would look something like this:

image

The layout was simple with CSS, then I added some jQuery to simulate getting the checkbox items dynamically.

Example:


That example produces a list that adjusts appropriately when resized:

image image

Monday, April 26, 2010

ASP.NET Chart Controls Without Web Forms


I started tinkering with the ASP.NET chart controls last week for a project at work, and the samples available from Microsoft are excellent, except they're mostly all using web forms. So I took it upon myself to do a few things:
  • Upgraded the existing samples from .NET 3.5 to .NET 4.0.

  • Added a sample project 'ChartsWithoutWebForms' to show one way to utilize the ASP.NET charts without using web forms at all.

  • Added a sample project 'ChartsWithMVC' to show one way to utilize the ASP.NET charts with the ASP.NET MVC framework.
The basic idea behind the two additional projects is that the chart is rendered as an image, which means you lose some interactive functionality, but my examples show that you can still fake some interactivity.

I also have an example that shows how you might create a dynamic charting system, which is actually what I'm working on now, but the work is venturing into an area where it will be specific to my employer, so I want to release the samples before that happens.

Feel free to take these updated samples and use them as you see fit. Hopefully they will help you out.

You can download the samples here:
http://www.downcastsystems.com/files/ChartSamples.zip

Tuesday, November 4, 2008

C# 4.0 dynamic Intellisense : a letter to the C# designers

I just emailed this to Eric Lippert:

I want to express my concern with the new dynamic language features in C# 4.0.

I think it is important to enable duck-typing along with this feature; possibly by enabling a "fake" cast to an interface. My proposal is below.

 
public class Person
{
public string Name { get; set; }
}

public class Fruit
{
public string Name { get; set; }
}

public interface INamed
{
string Name { get; set; }
}

public static class Program
{
public static void Main()
{
// these types may be defined in any language; possibly from Ruby, etc.
// the cast to INamed is "fake"; used only to enable intellisense since actual member resolution is dynamic
dynamic p = new Person() { Name = "John" } as INamed;
dynamic f = new Fruit() { Name = "Apple" } as INamed;

// would get intellisense after "." provided by the INamed interface
Console.WriteLine(p.Name);
Console.WriteLine(f.Name);
}
}

Monday, September 29, 2008

Balsamiq makes mocking up fun!

Robert sent me a link last week to a new mockup tool called Balsamiq.  At first, I didn't really see the point, but after using it for only a few minutes, I was totally sold.

What is it?

Have you ever tried to use Visio to mockup UI?  It's a cumbersome process, and no matter what I do, the end result just always looks like a bunch of circles and squares and nothing like what I actually envisioned.

Balsamiq has some ready-made controls that you can easily drop onto your canvas.  They look hand drawn, which is absolutely wonderful because then when I show people my mockups, there will be no doubt that they're simply looking at a mockup and not the final version.

image

Why is it useful?

I was just playing around with Balsamiq for a few minutes and made a quick screen showing a new feature we want to implement.  I showed it to Robert, and we found out instantly that I was going about it the wrong way.  Imagine if I'd mocked that up in HTML, or in Photoshop?  It would have taken me a lot longer, and it would have been harder to modify after my initial mistake.  Since I used Balsamiq, i was able to just move the controls around.  So it saved me a lot of time and aggravation.

Another Example

We have to implement a way for color blind users to use our web application, so we've been asked to add "Color Schemes" to the User Settings.  The user can have an underline or a box around particular fields on the form.  The box background color or underline color is affected by changing color schemes.

At first, I thought, the simplest thing to do would be to simply provide a drop-down list of named color schemes with a preview below, so I mocked it up, and it looked like shit.  Then I remembered the way Digsby and now the new Windows Live Messenger 9 Beta showed some of the custom UI settings, so I tried to emulate it a bit.  (The horizontal rule control in Balsamiq isn't colorable yet, but the author says he's added to his upcoming feature list.)

image 

Also, I just want to note that I've emailed the author twice, and he responded back within a few minutes each time.  That's impressive.

Check it out:

http://www.balsamiq.com

Friday, May 2, 2008

Unnecessary Iffage

A friend of mine showed me an example of the kind of bad code he finds every day where he works:

image

There are a lot of things wrong with the example above.  Le'ts pick it apart and simplify.

1. This line is unnecessary:
image 

You could just use SomeBooleanFunction in place of someBooleanValue in the example above, like so:

image

2. " = True" is unnecessary.
The function already returns a boolean value.  Get rid of it:

image

3. anotherBooleanValue is also unnecessary. We could do away with it and return the value immediately from the If statement, like so:
image
 
4. In fact, almost all of it is unnecessary:
image
 

How does this happen?

Why didn't the programmer just use the existing SomeBooleanFunction() instead of creating a new method that does the exact same thing?  I bet I know.

I suspect the original programmer didn't know that SomeBooleanFunction() existed until after he'd already duplicated the same logic in MyBooleanFunction().  Once he found out that SomeBooleanFunction() was there, he wanted to use it, but didn't want to change all the code that was already using his new MyBooleanFunction().

A simple find/replace could have solved his problem, but he may have been afraid to change too much. That's not a good excuse, I know. I'm just sayin'.

Tuesday, April 29, 2008

Programming Books Usually Suck

Jeff Atwood over at Coding Horror is always going on about how programmers don't read programming books because most of them suck, and I agree with that.  I personally have only read about three really good ones.

In Jeff's post, he mentioned (as an example of poor writing) a book called Perl for Dummies. I followed through to the linked review, and it was great. So I wanted to point out a few of the best (of the worst) examples, as I think programmers who are writing should take notice of these kinds of things.

When there are simple and difficult explanations, you can usually count on Hoffman to find the difficult explanation, and then to tell you how difficult is. For example: (page 93)

int can be a bit tricky because it rounds down for positive numbers and rounds up for negative numbers.

He's right; that is tricky. Wait, which was does it round again? Up, down, positive, negative, I can't remember. But if he had said

int is very simple: It throws away the fractional part of a number, leaving the integer part.

then it is a lot easier and you understand the name so that next week you might remember what it does.  Another example of this: In the ill-advised explanation of xor, he says

The operator returns true if the two things have different truth values but returns false if they have the same truth value. (See table 8-2.)

Here's the simple version:

$soup xor $salad is true if either $soup or $salad is true, but not both.

Then you dispense with the table.

Part of the reason Robert and I do Develocity is because we are always finding terrible sample code out in the wilds of the Internet, and it's teaching programmers the wrong ways to do things. It's scary to think people are filling books with the same sort of nonsense.

I'm not a programming expert, and I make my share of mistakes too, but before I post sample code that other people are going to look at and possibly use, I will go through it a few times, seeing if it can be made simpler and easier to understand.

Most examples have too much noise.  A "Hello, World" example should do nothing but print "Hello, World" to the screen. Don't add any for loops or random number generators to it.

Sunday, April 13, 2008

Learn and Practice LINQ With LINQPad

LINQPad is a free tool that lets you query your databases using LINQ syntax in an IDE similar to a poor man's SQL Management Studio. It comes packed with 200+ examples.

image

You can also use it as a code snippet compiler, but I think Snippet Compiler is better suited for that, as LINQPad doesn't have Intellisense at the moment.

http://www.linqpad.net/

Friday, April 11, 2008

Development-Related Uses For Screencasts

I’ve started using a program called Jing recently. It can record a video of what's currently on your screen.

Possible uses:

  1. Show managers or project sponsors the current state of your in-progress application without giving them access to the version on your machine (that you might accidentally break since you're still working on it).
  2. When a new feature is created, developers could create a video that shows the QA team what the new feature is supposed to do.
  3. When the new feature doesn't work, the QA team can show the developers the steps necessary to reproduce the problem. (Most of the time, a simple text description isn't enough.)
  4. I have a friend who wants to show his users in Asia how to use certain features of his application.
  5. I have another friend who wants to record the repetitive (yet somewhat complicated) tasks he’s currently doing, so he can show someone else how to do those tasks.
  6. Project sponsors could keep customers (internal and external) abreast of new changes to an application via a blog and show off those changes with an embedded screencast.

If you want to get really fancy, you could even create tutorial videos like the kind you see on www.asp.net, but I think a program like Camtasia Studio would probably be better suited to that, as Jing has a 5 minute limit on any videos created, and there is no way to edit the videos it creates.

Also, because I don't want to divulge any company secrets, I'm not sharing my videos to Screencast.com, Jing's default video destination. I set it up to save a file on my hard drive, so that I can attach it to an email or TFS work item.

Jing is free and extremely easy to use.

http://www.jingproject.com/

How To: Hide a Button When Clicked on an ASP.NET Page Containing Validation Controls

Suppose you have a form with some ASP.NET validation controls on it, and you want to prevent users from clicking the form's submit button more than once.

image

It might sound easy at first. Just add some javascript to the asp:Button's OnClientClick to hide the button and show your custom message:

<script language="javascript" type="text/javascript">
function DisableButton(button)
{
button.style.display
= 'none';
document.getElementById(
'loading').style.display = 'inline';
}
</script>
<asp:Button ID="LoginButton" runat="server" Text="Login" OnClientClick="DisableButton(this)" />
<span id="loading" style="display: none;">Logging in...</span>

Except if you do that, and you're using ASP.NET validation controls on the page, your user could end up in a situation like this:

image

Now your user is stuck because the Login button is gone!

So, this is how to get around that:

<script language="javascript" type="text/javascript">
function DisableButton(button)
{
Page_ClientValidate();
if (Page_IsValid)
{
button.style.display
= 'none';
document.getElementById(
'loading').style.display = 'inline';
}
}
</script>

In the script above, you'll notice I'm using a Page_ClientValidate() method and a Page_IsValid boolean variable. Those are exposed as part of ASP.NET's Client-Side Validation API.

What's happening:

  1. Page_ClientValidate() forces the validation to happen immediately.
  2. If the validation tests pass (Page_IsValid), hide the button, show the message, and submit the page.
    image
  3. If the validation tests fail, ASP.NET will handle showing the validation errors for you, and the page will not be submitted.
    image

For more information on ASP.NET Validation, see this article on MSDN:
http://msdn2.microsoft.com/en-us/library/aa479045.aspx

Friday, April 4, 2008

Enhancing C#'s switch statement with a fluent API

A colleague pointed me to this article today.  It shows a very clever set of utility classes that can dramatically improve the usefulness of the "switch..case" concept.  Since the C# language can't be extended, it is implemented as a fluent API that tries to mimic the built-in switch statement.

The most interesting use to me is the generics-based double dispatch using delegates that enables a sort of visitor pattern:

void Do(Control c) 
{
new Switch(c)
.Case<Label>(l =>
{
// ...
})
.Case<Button>(b =>
{
// ...
})
.Default(cc =>
{
// ...
});
}



An interesting oddity of the implementation is that the various Case() overloads are implemented as Extension methods on the Switch types. That is the only thing I don't like in the linked article. It exploits a "feature" of Extension methods that sorta allows you to call methods on null references. The case "statements" (method calls) are short-circuited by returning null and additional calls to Case() check for the null "this" pointer (the fake "this" which is the first parameter to extension methods) and exits quickly. To me, this seems like abuse of extension methods since the same thing can easily be accomplished with a traditional class.



Here is a modified version of the Switch<T> class implemented as a simple class without the extension methods:




public class Switch<T>
{
public T Object { get; private set; }
private bool hasBroken = false;

public Switch(T obj)
{
this.Object = obj;
}

public Switch<T> Case(T other, Action<T> action)
{
return this.Case(other, action, SwitchBehavior.Break);
}

public Switch<T> Case(T other, Action<T> action, SwitchBehavior behavior)
{
return this.Case(x => object.Equals(x, other), action, behavior);
}

public Switch<T> Case(Func<T, bool> condition, Action<T> action)
{
return this.Case(condition, action, SwitchBehavior.Break);
}

public Switch<T> Case(Func<T, bool> condition, Action<T> action, SwitchBehavior behavior)
{
if (!this.hasBroken && condition(this.Object))
{
action(this.Object);
if (behavior == SwitchBehavior.Break)
this.hasBroken = true;
}

return this;
}

public void Default(Action<T> action)
{
this.Case(t => true, action, SwitchBehavior.Break);
}
}

public enum SwitchBehavior { FallThrough, Break }



A functional C# (type)switch - B# .NET Blog

Javascript: More than meets the eye...

I recently ran across a presentation on InfoQ by Glenn Vanderburg called "The Power of Javascript" that was recorded at JAOO.

Summary
Glenn Vanderburg makes the case for Javascript, a language long overlooked. This presentation from JAOO 2007 shows how its OOP model and other language features make it a very powerful tool and how to use these features to get the most out of the language.

The presentation describes how Javascript got its name - starting out as LiveScript developed by Netscape and later rebranded as Javascript to exploit the popularity of the new kid on the block - Java, developed by Sun. He explains why it is commonly considered to be a "toy language" and why it isn't.

A while ago I posted the first in a series of articles about javascript. Since then I've been somewhat stingy in releasing the other proposed articles.  I'll try to get back into the swing of things promptly.

Thursday, November 15, 2007

Sorting a Generic List of Custom Objects Simply

For a defect I was working on today, I just wanted to sort a list of users by username in a very simple fashion.

Google failed me. I wasn't able to find a good example of how to do this easily in VB.NET. As usual, most examples are full of noise, like this, this, this, or that.

So, once I figured out how to do it, I thought I should share it.

First, we create a function to handle our comparison.

Private Function UsernameComparer(ByVal x As User, ByVal y As User) As Integer
Return x.UserName.CompareTo(y.UserName)
End Function

Next, we use it.

myUserList.Sort(AddressOf UsernameComparer)

Now our user list is sorted by username.

In C# 2.0, using anonymous methods, it can even be done all at once:

myUserList.Sort(delegate(User x, User y) {
return x.UserName.CompareTo(y.UserName);
});

If you know of an even simpler way to do this, leave some comments and let us know!

Wednesday, October 17, 2007

MSDN code examples are terrible

This is the page that really got my goat. I'll list the relevant parts here for ease of reading.

Page.RegisterOnSubmitStatement Method

Allows a page to access the client OnSubmit event. The script should be a function call to client code registered elsewhere.

   1: '[Visual Basic] 
   2: Dim scriptString As String = "<script language=JavaScript> function doClick() {"
   3: scriptString += "document.write('<h4>' + myForm.myHiddenField.value+ '</h4>');}<"
   4: scriptString += "/" + "script>"
   5: RegisterHiddenField("myHiddenField", "Welcome to Microsoft .NET!") 
   6:  
   7: RegisterOnSubmitStatement("submit", "document.write('<h4>Submit button clicked.</h4>')") 
   8:  
   9: RegisterStartupScript("startup", scriptString) 


   1: //[C#] 
   2: void Page_Load(Object sender, EventArgs e) 
   3: { 
   4:    String scriptString = "<script language=JavaScript> function doClick() {";
   5:    scriptString += "document.write('<h4>' + myForm.myHiddenField.value+ '</h4>');}<";
   6:    scriptString += "/" + "script>";
   7:    RegisterHiddenField("myHiddenField", "Welcome to Microsoft .NET!"); 
   8:    RegisterOnSubmitStatement("submit", "document.write('<h4>Submit button clicked.</h4>')"); 
   9:    RegisterStartupScript("startup", scriptString);
  10: } 

Ok, how many things can you find wrong with this simple example?


  1. string concatenating to make a script block

  2. a bunch of totally unrelated code that muddies the point

  3. document.write() - who does that?

  4. VB and C# code don't even match; C# includes the method declaration; VB only includes the method body

  5. scriptString += "/" + "script>"

  6. definition says "The script should be a function call to client code registered elsewhere." but that is not what the example code does

Here's my stab:



   1: //[C#] 
   2: void Page_Load(Object sender, EventArgs e) 
   3: { 
   4:   if (!IsClientScriptBlockRegistered("doClickScriptKey"))
   5:   {
   6:     String scriptString = @"
   7:     <script type='text/javascript'>
   8:         function doClick() { alert('the form is about to submit!'); }
   9:     </script>";
  10:     RegisterClientScriptBlock("doClickScriptKey", scriptString);  //"registered elsewhere"
  11:   }
  12:   RegisterOnSubmitStatement("submitScriptKey", "doClick();"); //"function call to client code registered elsewhere"
  13: } 

Friday, September 14, 2007

Conversation about Patterns, Developers, and whatnot

(9:10 PM) Robert:

how's the job going this week?

(9:11 PM) Daniel:

better actually, we're digging into this arcade game
and I'm starting to understand things more

(9:11 PM) Daniel:

but its all-consuming in my head, i can't seem to think about anything else
i.e. dave's game

(9:12 PM) Robert:

that's understandable

(9:12 PM) Daniel:

I really want to help dave though, cause I want him to finish it

(9:13 PM) Daniel:

maybe on the weekends I'll have time

(9:24 PM) Robert:

i hate words that have extraneous letters
such as Lincoln

(9:24 PM) Robert:

what's that second L doing in there?

(9:24 PM) Daniel:

just hangin out man

(9:24 PM) Robert:

nobody says "Leen-koln"

(9:25 PM) Robert:

back to programming a bit...

(9:25 PM) Daniel:

I feel your pain

(9:25 PM) Robert:

have you read about "design patterns"?

(9:26 PM) Robert:

(along with "anti-patterns")

(9:26 PM) Daniel:

I'm staring at a book called Design Patterns at this very moment

(9:26 PM) Daniel:

not reading it, its on the table

(9:26 PM) Robert:

"Elements of Reusable Object-Oriented Software"?

(9:26 PM) Daniel:

you're meaning things like the factory method, singletons...etc

(9:27 PM) Robert:

yeah

(9:27 PM) Daniel:

yeah, that's the one

(9:27 PM) Robert:

cool

(9:27 PM) Daniel:

dedrick had it in the office and I'm borrowing it

(9:27 PM) Robert:

you're already light-years ahead of most developers

(9:27 PM) Robert:

of your own accord or upon his suggestion?

(9:28 PM) Daniel:

no, I've been wanting to learn about them

(9:28 PM) Daniel:

and it turns out he had just the book I was going to buy

(9:28 PM) Robert:

i was teaching Greg at work about the Strategy pattern today

(9:29 PM) Daniel:

I guess I don't know greg

(9:29 PM) Robert:

you don't

(9:29 PM) Daniel:

but yeah, I've been wanting to read up on the patterns for awhile now
after learning they existed

(9:29 PM) Robert:

do you know if that book also talks about "anti-patterns"?

(9:29 PM) Daniel:

not sure

(9:30 PM) Daniel:

not in the index
anyway

(9:32 PM) Daniel:

I don't understand something

(9:33 PM) Daniel:

you mention a lot that most developers don't do this, or know about that
but jeez, what do they know?

(9:33 PM) Robert:

they know how to write "spaghetti code" until it just barely works

(9:34 PM) Robert:

"spaghetti code" is probably what your early expirements looked like

(9:34 PM) Daniel:

that sucks, they don't even want to make it good?

(9:34 PM) Robert:

nope
they are victims of "good enough"

(9:37 PM) Robert:

http://www.amazon.com/AntiPatterns-Refactoring-Software-Architectures-Projects/dp/0471197130/ref=pd_bbs_1/104-0707808-5144724?ie=UTF8&s=books&qid=1189647010&sr=1-1

(9:37 PM) Daniel:

I guess I don't have enough time working with dedrick and the other guy to see what they're attitude is, they seem to be okay

(9:38 PM) Daniel:

sounds like a funny book
interesting

(9:39 PM) Robert:

this is the exact reason why i think i told you before - i'd rather hire a mediocre-but-passionate dev than a well-trained-but-unmotivated dev

(9:39 PM) Daniel:

I can see that

(9:40 PM) Daniel:

do you ever get input on who your company hires?
give input

(9:41 PM) Robert:

passion begets constant improvement and excellent developers that can encourage other devs

(9:42 PM) Robert:

lack-luster begets wasted time/effort and code that is difficult to maintain
no

(9:43 PM) Robert:

though i was asked to write a small code-snippet that would be presented to interveiwees to see what they understand about it
i'll show you - you won't get it - it's probably too difficult

(9:43 PM) Daniel:

ok

(9:46 PM) Robert:

    static class Foo<T> where T : new()
    {
        private static T bar = new T();
        public static T Bar
        {
            get { return bar; }
        }
    }
that's the whole class, and it's quite useful

(9:47 PM) Daniel:

ok, let me look

(9:47 PM) Robert:

i had to replace real names with Foo and Bar or else you'd know right away

(9:48 PM) Daniel:

don't tell me

(9:49 PM) Robert:

the point of the code in the interview isn't to see if you get it right, it's to see how much of it you can explain

(9:50 PM) Daniel:

it looks like a generic class that just makes things
to me

(9:50 PM) Daniel:

some parts of the code confuse me however

(9:51 PM) Robert:

explain...

(9:51 PM) Daniel:

explain what I don't understand?

(9:51 PM) Robert:

yeah

(9:52 PM) Daniel:

in the class heading, you have "where T : new()
I don't get that

(9:53 PM) Daniel:

maybe I'm just reading it wrong

(9:54 PM) Robert:

that is a "generic type parameter constraint"

(9:55 PM) Robert:

it say's "you can use any T as long as it has a default public constructor"

(9:55 PM) Daniel:

I see

(9:55 PM) Daniel:

is this an object factory?

(9:56 PM) Robert:

if a type only had a private constructor or the only public constructor (abbrv. "ctor") requires any parameters

(9:56 PM) Robert:

that's close - it's a very special type of "factory"

(9:57 PM) Robert:

want me to explain?

(9:57 PM) Daniel:

yeah, don't think I can get any closer

(9:58 PM) Robert:

hint: it's not building very many objects - "new T()" is only called once

(9:59 PM) Daniel:

a singleton?

(9:59 PM) Robert:

yep
it externalizes the concept of "singleton" to a separate generic class

(9:59 PM) Robert:

you would use it like this:

(10:00 PM) Robert:

WizardGame game = Singleton<WizardGame>.Instance();

(10:00 PM) Robert:

the Singleton class (Foo) ensures that there is only one

(10:01 PM) Daniel:

how does it do that?
what prevents you

(10:08 PM) Robert:

brb; putting dishes away

(10:09 PM) Daniel:

ok

(10:36 PM) Robert:

hey
i'm back

(10:37 PM) Daniel:

ok

(10:37 PM) Robert:

every question you ask me tells me more about what you know and don't know about C#/.Net

(10:37 PM) Daniel:

:)

(10:37 PM) Robert:

...as an interviewer

(10:38 PM) Robert:

at first, i knew that you at least understood the C# language at a basic level
and you understood the basic concepts of Generics

(10:38 PM) Robert:

but not some of the finer points (constraints)

(10:39 PM) Robert:

after my hint, i knew that you were familiar with the concept of "design patterns"

(10:39 PM) Daniel:

a lot of information from just a little bit of code

(10:40 PM) Robert:

and now i know that you aren't familiar with the intricacies of C#

(10:40 PM) Robert:

here's the non-obfuscated code:
    static class Singleton<T> where T : new()
    {
        private static T instance = new T();
        public static T Instance
        {
            get { return instance; }
        }
    }

(10:41 PM) Robert:

and now to answer your question...

(10:41 PM) Robert:

first, you have to understand that Singleton<Foo> is a different class than Singleton<Bar>

(10:42 PM) Daniel:

ok

(10:42 PM) Daniel:

seems logical enough

(10:45 PM) Robert:

was looking up some terms i forgot for a second

(10:45 PM) Robert:

Singleton<T> is called an "open" generic type as long as T is undefined

(10:46 PM) Robert:

Singleton<Foo> is a "closed" generic type b/c T has been defined as Foo

(10:46 PM) Daniel:

ok

(10:46 PM) Robert:

Singleton<T> == S<T> from now on

(10:46 PM) Daniel:

ok

(10:47 PM) Robert:

so now that you know S<Foo> is a totally different class than S<Bar>...

(10:47 PM) Robert:

notice that the class is static - both member (field and property) are both static

(10:47 PM) Daniel:

I see

(10:48 PM) Daniel:

so you don't need an instance of the class, but it returns an instance
of T

(10:50 PM) Robert:

    class Foo
    {
        public object bar = new object();
    }
is basically the same as...
    class Foo
    {
        public object bar;
        public Foo()
        {
            bar = new object();
        }
    }

(10:50 PM) Robert:

first initializes bar inline; second initializes bar inside the constructor

(10:51 PM) Daniel:

I'm with you

(10:51 PM) Robert:

first is essentially converted to second when compiled

(10:51 PM) Robert:

C# guarantees that all fields will be initialized before any user-defined ctor code is executed

(10:52 PM) Daniel:

ok

(10:52 PM) Robert:

    class Foo
    {
        public int bar = 3;
        public Foo()
        {
            bar = 2;
        }
    }

(10:52 PM) Robert:

in this case, bar is initialized to 3, then immediatly overwritten with 2

(10:53 PM) Daniel:

I see

(10:53 PM) Robert:

C# also guarantees that a static constructor will be executed exactly one time per class (per AppDomain)

(10:54 PM) Robert:

and that it will be executed immediatly before the first access of any static members of the class

(10:54 PM) Daniel:

does the constructor need an access modifier?

(10:54 PM) Robert:

static constructors can't have access modifiers

(10:55 PM) Robert:

instance constructors follow the same rules as other members - everything without an access modifier is private by default

(10:55 PM) Daniel:

ok
let me soak in what you wrote

(10:57 PM) Daniel:

so its the static keyword in the constructor that makes it the singleton

(10:57 PM) Robert:

the class i sent you doesn't even have an explicit constructor

(10:58 PM) Daniel:

oh, you're right

(10:58 PM) Daniel:

hmm

(10:59 PM) Daniel:

so the fact that its a static class then

(10:59 PM) Robert:

sorta - in C# a static class is just a way to ensure that all members are static
it doesn't mean much

(11:01 PM) Robert:

(in .Net 1.x they shipped with a class that was expected to be "static", but it had a public instance method and the only instance ctor was Private - that meant that there was no way for anyone to instantiate the class and use the public method - in .Net 2.0 they added the concept of a "static class" to prevent things like that in the future)

(11:02 PM) Daniel:

I see

(11:03 PM) Robert:

i have given you all the info you need to answer your orig. question

(11:04 PM) Robert:

(inline initialization == ctor initialization; static ctor is called exactly once and just before first access of any static member)

(11:06 PM) Daniel:

I get it

(11:06 PM) Robert:

you grok it?

(11:06 PM) Daniel:

no, I don't want  to say I grok it until I've used it

(11:07 PM) Daniel:

so pretty much that's how you do a singleton in .net?

(11:08 PM) Daniel:

or is that across other languages as well?

(11:08 PM) Robert:

other languages/platforms have different intricacies
and that's not the normal way to do a singleton
it's my own invention
:)

(11:08 PM) Daniel:

seems elegant

(11:09 PM) Daniel:

although I don't know the other way

(11:09 PM) Daniel:

I have a question for you

(11:10 PM) Daniel:

its C++ but more OOP specific

(11:11 PM) Daniel:

in the main class I'm doing at work, it has a bunch of functions that work with the member  data

(11:12 PM) Daniel:

it basically has one method, and inside that method I've broken tasks down into their own methods

(11:12 PM) Daniel:

but since its all within the class I don't have to pass anything into these methods since I already have access to them

(11:13 PM) Daniel:

but it feels weird or something

(11:13 PM) Robert:

it's a bad habit

(11:13 PM) Daniel:

I thought so, but I wasn't sure

(11:14 PM) Robert:

unless the data (state) needs to be persisted even after the call to the "basically one method" then it should be passed to each method as needed

(11:15 PM) Daniel:

ok, I felt that was the right way
tomorrow I'll fix it

(11:16 PM) Daniel:

I mean I felt that passing parameters was the right way

(11:19 PM) Robert:

this is the conventional singleton pattern
    public class Foo
    {
        // prevents public instantiation
        private Foo() { }
        private static Foo instance;
        public static Foo Instance
        {
            get
            {
                // this is called the "double-checked locking" singleton pattern
                // it attempts to solve a "race condition" that may occur in multi-threaded systems
                if (instance == null)
                {
                    lock (this)
                    {
                        if (instance == null) // another thread could have assigned it since the previous check
                        {
                            instance = new Foo();
                        }
                    }
                }
                return instance;
            }
        }
    }

(11:20 PM) Robert:

http://www.ibm.com/developerworks/java/library/j-dcl.html

(11:21 PM) Robert:

"The bottom line is that double-checked locking, in whatever form, should not be used because you cannot guarantee that it will work on any JVM implementation."
same thing on the .Net platform

(11:23 PM) Robert:

"Listing 10" in that article is "good" but not great in .Net b/c it isn't lazy enough

(11:24 PM) Robert:

access to *any* static member will instantiate the "instance" field even though i may not be using it
anyway, it's bedtime
i think you've got enought for tonight
:)

(11:24 PM) Robert:

i had fun teaching you

(11:24 PM) Daniel:

for sure, thanks

(11:25 PM) Daniel:

interesting how you merged our two topics
did you notice that?

(11:25 PM) Robert:

no?

(11:25 PM) Daniel:

we were talking about design patterns then about programmers you don't like, which lead to my question about how much input you have in the hiring

(11:26 PM) Daniel:

which lead to you giving me the example snippet you wrote for interviewees

(11:26 PM) Robert:

which went back around to patterns

(11:26 PM) Daniel:

which was about a singleton
:0-)
uh, :)

(11:26 PM) Robert:

i'm just good like that :)
goodnight

(11:26 PM) Daniel:

guess so, night