• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Learning C# & ASP.NET...

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.
So to update... since it's been a while. I finished my dice rolling project after this most recent post. I showed my buddy (the senior software engineer). He suggested that I refactor it based on what I've learned. (I had just finished reading Object Primer and started in on a book on design patterns.)

The first iteration of my project was more of a mish-mash of tossing form controls on a blank form in Visual C# Express and hard coding all the functionality into the event handlers. I did have one (only one) class that controlled my dice rolls. So my refactor was done with the intent to create a class hierarchy for my dice.

- I created an individual class for each die type that would inherit from an abstract dice class.
- I created a framework for my dice rolls in an interface, then implemented that interface into a single concrete class. To generate a dice roll, I would pass one of my dice objects into my roll framework. The roll method would get the number of sides on the dice, then pass that as an int into a random object to return the appropriate value.
- I also implemented a framework to dynamically assign the appropriate type of dice object based on user input.

So in this refactor, I'm more or less using 2 patterns. The Strategy Pattern, to decouple my roll functionality from my dice objects. I also employ the Factory Method Pattern to dynamically create an object, then pass it into my roll class to retrieve the appropriate random number.

So my buddy proofed what I'd done and thought it was a huge improvement in terms of OO Design. I tend to agree. He mentioned a way of streamlining it that I still can't quite get my head around...

He suggested looking at the Builder Pattern and implementing that rather than Factory Method to dynamically create my dice objects. He also suggested storing my dice objects in an enum. At first, I saw some benefit... but after reading a bit more about Builder and Factory Method, I'm a little confused as to the advantage of one over the other in this scenario.

I understand that Builder really excells at creating complex objects. But for this project, my objects that I'm instantiating are fairly simplistic (all my dice objects really "know" is how many sides they have). Is there something I'm missing. Is Builder generally better practice? Is Builder generally considered to be easier to implement, or more elegant than Factory Method?
 
I'm not a programmer or atleast that's what I keep telling my boss. The less code I write, the better. Sometimes simple is good. How you write code really depends on the situation you are in. I'm a victim of RAD.
 
RAD is good for starters and most non-programmer programmers. VB.NET is perfect for that.

I applaud YellowDart for learning design patterns as that dives into the meat and bones of software engineering with methodologies and correct way of programming.
 
The less code I write, the better. Sometimes simple is good. How you write code really depends on the situation you are in.

I agree. I mean, to be honest, a dice roll could be easily simulated by the following code:

Code:
int diceRoll(int sides, int dice) {
    int total = 0;
    for (int i = 0; i < dice; i++) {
        total += Math.round(Math.random() * sides);
    }
    return total;
}

(Yeah, I know that's Java code, but I haven't done C# in so long and I don't have VS on this comp as a reference.)

However, in this situation, I can see how using classes could be useful, as this program seems to be more for learning than for actual use, so by using classes, the OP will obviously learn more about using them.
 
Thanks for the replies guys. So quick nub moment here... RAD? Not sure I'm familiar with that acronym yet. ;)

Tacoman: Thanks! That's reassuring. I think it helps that my buddy, who's been a developer since he graduated highschool (he's now 32, I believe), is mentoring me. Rather than giving me books on languages first, he gave me a book on UML and OO Design Methodology and a book on Design Patterns. I finished the first book, and I'm a little more than half way through the second book. (Reading about the Iterator Pattern... it's making my mind hurt. I'm not that knowledgeable with array lists, hashtables and vectors yet. LOL)

Soichiro: That's similar to the way that I implemented the roll functionality. Though, C# has it's own Random class, as opposed to a random method within the Math class like Java.

Code:
public int RollTheDice(dice myDice)
{
    Random rand = new Random(seed);
    int max = GetSides(myDice);
    return rand.Next(1, max + 1);
}

So basically, just the roll functionality is encapsulated here. Not the calculation of the total. I may refactor it again to encapsulate a little bit more functionality. I still have a fair bit of duplication in all my various event handlers for different dice rolls. So I may create another class to handle my calculations... we'll see.
 
Last edited:
I mainly do all my programming in C++ or Java, I always wanted to get into the .net world but IBM loves Java and a lot of shops are converting their ASP.net/C# jobs to Servlets and JSP's (because its free and very very powerful!).

But once you learn 1 language you can learn them all with ease (as long as its not like going from Java to MIPS-32 ASM)

But C++ to Java, C# to Java, Vb.net to Java or vice versa, pretty much all the same as long as you have a strong foundation on OO programming.

RAD isn't bad in some cases, others I find it horrible if not done properly.

Goodluck on the application.


Word of advice on getting a dev job is what they want is real world EXPERIENCE.
If you can once you get good, do real jobs people post at projects wanted websites, you won't get paid jack but good experience.
 
Good deal. Yea, that's one of the reasons I decided to focus on C# as my first true OO language. I figure I'll get the foundations of OOP and Design Patterns down. Once I have that, and a strong knowlege of C#, Java and C++ should be pretty easy to pick up. In fact, the Design Patterns book I'm currently reading is all Java. Zero C# in that book. The Object Primer was all Java too... so I'm getting some exposure to it through my reading, while my coding is primarily in C#. The two languages are so close syntactilly, it's scary. A few differences here and there, but if you know one, you can learn the other with no trouble at all. :)

That's a good tip on the career switch. Actually, I run a Web Development house on the side... so as I learn C#/ASP.NET, I'll start offering web app development as one of my services. That should add some of that real world experience under my belt if I can land a freelance dev job or two.

Right now, my friend/mentor is a contract/consultant full time. He doesn't work for anybody. Other companies outsource work to him, and he gets paid ridiculous amounts of money for his work. He's made a lot of great local contacts, so he may be able to help me get my foot in the door, or allow me to come in on a few projects with him as a contract worker. Lots of options. :D
 
So another bit of an update. Another final refactor to my dice project as a whole. I employed Strategy and Factory patterns to set up my framework, but still had a pretty good amount of code duplication. So I ended up refactoring it a bit further and creating a class to encapsulate calculation of rolls (basically containing all of the math and iteration logic) and a facade to handle all of the parsing of user input and exception handling. It worked out great... and I think I'm pretty much done with this portion of my project.

I've now got a fairly complex design, but it's very extensible. If I decide to change one thing, say, change my implementation for dynamic dice object creation, all I have to do is write my new implementation, and change two lines of code in my calculator class.

With that done, my buddy is now saying he'd like to see some form of data capability there. He wants to be able to store a character in the program (I'm thinking ADO.NET), and he wants to keep a history of the last 20 dice rolls (I'm thinking XML). So with those new requests, he points me to the MVC pattern and basically says "go nuts".

This is a beast of a pattern. I think it'll work out great for what he's asking me to do... but I'm definitely going to take this approach much slower. I think I need to revisit some of the tutorials I've done on ADO.NET and XML to get the particulars of handling that data down, but I think it'll be doable. I'm pretty stoked to get started on it.

One last small update... I've finished some reading on design patterns and have moved on to a nice thick O'Reilly book on ASP.NET. Pretty interesting so far. I like that you can embed C# or VB straight into the HTML, similar to old school ASP or PHP... just that it'll offer more power with the .NET framework and the ability to work with other developers in different languages under the .NET framework. Pretty sexy stuff. ;)

Once I get my head around some more of this stuff, I'll be working to learn AJAX in ASP.NET and helping my buddy with a couple of his independent projects for some of that "real world" experience. I'm really looking forward to getting into this at a deeper level... hard to believe I've only been at this just over a month. :D
 
Back