• 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.

YellowDart

Member
Joined
Sep 25, 2003
Location
Mesa, AZ
Hey guys...

Just wondering if any of you developers out there have any good tips for learning C#, or OOP in general. I'm focusing on C#, simply for the fact that there is a TON of demand here in Phx for C#/ASP.NET developers. One of my buddies is a senior developer out here, and may be able to help me get my foot in the door for a really good job at the entry level w/ some room for advancement.

My experience so far is fairly limited in OOP. I know a bit of ActionScript (enough to develop my own custom classes to script behavior of objects within my own Flash movies). I've got some limited Java and VB exposure from college (ended up dropping them both, though... too much partying at the time). Lately, though, I've been working my *** off learning OOP and am starting to feel pretty good about it.

I have some decent web development experience, but not hardcore OOP/software dev stuff. I've got HTML, XHTML, CSS down. I know a bit of PHP, JavaScript and XML.

I've been reading lots of books and watching tutorials on Lynda.com (AS3.0) and more recently on MSDN (C#).

I've read this one cover to cover. (Dummies, LOL).

I'm partially through this book, but since I'm changing up my focus to C#, rather than ActionScript, I'm putting it down for now.

This one is on the way. Should have it within the next few days (fingers crossed).

At work, I've been reading through tutorials here as well.

I'm also strongly considering taking some C# night classes at a community college on my way home from work... depending on how well and how quickly I'm teaching myself how to code (I pick things up very quickly when I'm focusing on it).

So with that said, I feel like I've got most of the basics of OOP down. Any pointers you guys can offer? Anything I might be missing that might help me? Any other good books to recommend? Tutorial sites? Am I insane for reading programming text books for fun? :p
 
It sounds like you have a wide foundation to work from. The two things you may be missing are experience and confidence. Therefore, I suggest you pick something you would like to do, thats just a little harder than you think you can do and do it.

For example, you could create a small role playing game, where you have hitpoints and fight randomly generated monsters. Or a calculator, or a wiki, whatever you find interesting.
 
It sounds like you have a wide foundation to work from. The two things you may be missing are experience and confidence. Therefore, I suggest you pick something you would like to do, thats just a little harder than you think you can do and do it.

For example, you could create a small role playing game, where you have hitpoints and fight randomly generated monsters. Or a calculator, or a wiki, whatever you find interesting.

I like that idea. :)

I just might do that once I feel like I've got a better grasp on C#'s syntax. I've only been studying it for a week or so. Maybe after another week or two, I'll start working on a basic DOS console-based game.
 
Go to asp.net and watch the tutorial videos. Watch the AJAX videos too. They cover tons of stuff and are done very well. The guys from learnvisualstudio.net created alot of them.

Best way to learn is to build a project.
 
Good deal. I've been liking the videos from the guys at learnvisualstudio.net. I've been watching their beginner series of C# videos. Pretty good so far. I'm thinking in the next week or so I'll start working on something. ;)
 
I would spend a lot of time working with sql. Most ASP.Net jobs are going to be data related and you will use these a lot. I know it's vb.net :)

Code:
        Dim con As SqlConnection
        Dim com As SqlCommand
        Dim reader As SqlDataReader
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        con = New SqlConnection(connectionString)
        com = New SqlCommand("select [Style] from [Users] where [UserName]=@UserName", con)
        com.Parameters.Add("UserName", Data.SqlDbType.NVarChar).Value = User.Identity.Name
        con.Open()
        reader = com.ExecuteReader
        If reader.Read() Then
            Page.Theme = reader.Item("Style").ToString
        End If
        reader.Close()
        con.Close()

Also, try making a page using the role and membership features. With VS 2005 its very easy to get started. The LVS.net videos on asp.net can help you get started.

I have a few links below that might help.

Obviously
http://asp.net
http://www.asp.net/ajax/

Learn Visual Studio dot Net (lifetime membership is good if your serious. I'm on the forum there and it's pretty helpful. You'll proably get the same level of support on asp.net forums though for free)
http://learnvisualstudio.net

The Gridview
http://www.gridviewguy.com/

Role and Membership
http://www.odetocode.com/Articles/427.aspx
http://www.odetocode.com/Articles/428.aspx
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/security/membership.aspx

Email
http://www.worldofasp.net/aspnet/Tutorials/Email_ASP.NET2/Sending_Email_in_ASP.NET_2.0_83.aspx

I'll dig up some of my links from work tomorrow for you.
 
Ah, very nice! Thanks ebola! I've been focused on C# for now... just getting syntax, etc down, reading tutorials, watching lvc.net vids, etc. I'm hoping to move on to ASP.NET soon... so that'll be more than enough to get me started. :)

I imagine along the way I will pick up MSSQL, especially if I get more into the web app development side. I really like VS2005 so far. Intellisense is great, and I'm liking how easy it looks to attach your projects to a datasource. Been thinking about buying a lisence, but I may just stick with the express versions until 2008 comes out.
 
C# and the whole Dotnet universe is really a lot of fun. I program professionally in VB.Net and I pretty much look forward to coming to work. Not all days of course. I've been programming in Basic since VAX Basic came out, and lots of other languages along the way. The truth of the matter is, with DotNet, the language really makes little difference since most of the work is done by the base class libraries.

There are tons of free demos and examples all over the place. As Ebola suggested, pick a project and figure out how to do it. I don't recommend a role-playing game right off, in fact, stay away from graphics for a bit.

Ebola was right about using some database in your project. Very few business apps aren't connected to a database. Using the code he showed teaches how it's done, although some of the newer data access methods bypass that code and just use strongly typed data sets and object datasources. Sort of hard to explain, but instead of writing the code to show some data in a listbox, you just use a DataAdapter and it does all the work. Very nice but it doesn't let you know what's happening beneath the covers.

The 101 samples (http://msdn2.microsoft.com/en-us/vstudio/aa718334.aspx) is a good way to see how some things work. My ownly quibble is that you get the finished project and it doesn't really tell you how to create them from scratch. The various labs are better for that stuff. Step by step instructions.

Above all, have fun. If you're good at programming, you'll find out soon enough because you'll be having fun. I barely have time to play any games because I like writing code as much as playing with other people's code.
 
Haha. Very nice pointers in deed. My buddy (the senior developer) was telling me the same thing. The language matters little, since .NET will make them all work together in the end anyway. Pretty slick system. I'm going with C#, simply for the fact that that's where the demand is... gotta start somewhere, and C# will be a little bit more relevant in helping me get my foot in the door rather than AS3.0. :)

As a matter of fact, I've got my first project figured out. I was going to do it in ActionScript, so I could do some animation and custom graphics with it, but I'm going to do it in C# instead, since I think it's just a little more ambitious than I think I'm capable of right now...

I'm planning on writing a Windows-based form app to calculate and display dice rolls. I was a big DND nerd back in the day. Some of my buddies still play. So I was thinking about writing something that will calculate all your rolls for you. Attack rolls, damage rolls, saving throws, spell damage rolls, and display them all. It'd have combo boxes and text boxes to retrieve user input, make calculations based on random rolls and modifiers, then display the appropriate results.

It should be a good practice in event handling, repeating logic through loops, validating through if/switch statements, and passing data back and forth. Think I may get started on it after I do a bit more studying. I'll be sure to post it up here once I'm done. :D
 
So I started on my project over the weekend. It's going great. I'm almost done. Just having a little hiccup in regard to dynamically filling a variable length array with random numbers. When I access them, my loop seems to be writing the same number to all members of my array. Wierd. Going to need to step through it, I think, and see what's going on... after Heroes tonight. :p

Anyway, in regard to books... some news there. LOL

So my order arrived on Friday. I ordered this book from amazon.com used. Turns out the guy sent me the wrong one. :rolleyes:

My buddy (the senior developer) came through for me on Sat evening though. I'm borrowing a book on objects (The Object Primer), one on design patterns in OOP (more specific to Java), and one of the O'Reilly books on ASP.NET.
 
I'd save your money and not buy so many books. I did the same back in the day.

Avoid MS books for the most part. Occasionally they have a winner but most of them suck.

Wrox books are usually very good. You have to be careful of coding errors. Wrox books are really good but really poorly proofread.

My favorite programming books are the Sitepoint.com books. BTW they do have a new C# book. I have the ASP.Net VB/C# book and it's pretty good because it teaches you how to do a lot of stuff in code.

O'Reiley, I don't like how they layout their books. Learning from them can be difficult and I never seem to find good examples in their books.
 
Yea, I had some decent luck with some of the AS3.0 O'Reilly books. They are a little dry. All of the books I've got so far (aside from my AS books, and the one C# book), I'm borrowing from a friend, so no cash flow issues there. I'll probably buy a few just to have for a reference, but I won't be blowing a ton of cash on books right off the bat.

The Object Primer is a good read so far. I'm through the first 130 pages or so after a couple days with it. Mostly about prototyping large projects, planning, pre-coding stuff primarily. The next chapters are really getting into OO based theory, so I'm looking forward to that. Next up will be that OOP Design Patterns book. It's one of the "head first" series. My buddy seemed to like it. We'll see. I'd recommend The Object Primer for any who haven't read it. Very well written and easy to read. Not at all text-booky (compared to some of the O'Reilly books I've read through.)
 
OK, so I'm *almost* done with my project. The only thing that's keeping me, is some issues with an single dimension array.

For some strange reason, this array seems to be getting all its members set to the same value after generating and filling it with random numbers. Here's what I've got so far...

My event handler to receive info from a combo box indicating die type that's being rolled, and receiving a number of dice to roll. (The goal is to simulate spell damage: 10d6, etc).
Code:
private void multiDmgBtn_Click(object sender, EventArgs e)
        {
            // 11/11
            // need to work on array knowlege
            // for loop seems to work OK, but it doesn't appear to be giving multiple rolls
            // each member of the array is displaying the same value

            // 11/12
            // the loop and array seem to work fine now
            // for some strange reason, though, accessing the int values
            // through the ToString method seems to be messing with the values
            // stepping through with the debugger shows different values for each
            // array member...

            myMultiDmg = new TwentySider();

            /*********************************************/
                       
            myMultiDmg.BaseAttack = int.Parse(multiModBox.Text);
            
            _dieType = int.Parse(multiDieType.Text);
            _diceNeeded = int.Parse(multiDiceNeeded.Text);

            int[] myDiceArray = new int[_diceNeeded];
            int _arrayTotal = 0;

            for (int i = 0; i < _diceNeeded; i++)
            {
                myDiceArray[i] = TwentySider.RandomNumber(1, _dieType + 1);
                _arrayTotal += myDiceArray[i];
            }

            multiDmgResults.Text = _arrayTotal.ToString();
        }


Here's my class that is being referenced:

Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace GoDiceGo
{
    class TwentySider
    {
        //member fields
        private int _baseAttack;
        private int _otherBonuses;
        private int _penalties;
        private int _diceRoll;
        private int _diceNeeded;

        //member properties
        //defining get/set methods for each variable as public
        public int BaseAttack
        {
            get { return _baseAttack; }
            set { _baseAttack = value; }
        }

        public int OtherBonuses
        {
            get { return _otherBonuses; }
            set { _otherBonuses = value; }
        }

        public int Penalties
        {
            get { return _penalties; }
            set { _penalties = value; }
        }

        public int DiceRoll
        {
            get { return _diceRoll; }
            set { _diceRoll = value; }
        }

        public int DiceNeeded
        {
            get { return _diceNeeded; }
            set { _diceNeeded = value; }
        }

        //constructor function initializing private fields
        public TwentySider()
        {
            _baseAttack = 0;
            _otherBonuses = 0;
            _penalties = 0;
            _diceRoll = 0;
        }

        //static helper method to return a random number
        public static int RandomNumber(int min, int max)
        {
            //define new object of the Random class
            Random rand = new Random();

            //returning random int from args passed in
            return rand.Next(min, max);
        }
    }
}

It seems to compile and run OK, but the one issue I'm running into, is that the array seems to want to set all the member values stored within to the same value.

The weird thing is, when I step through my first for loop with the debug tool using a break point, it works fine. If I don't use the debugger to step through it, I get this issue. Anybody experience this before?
 
Last edited:
Not really sure what you're getting at by "seeding". /shrug

When researching and building my class, the only examples I really saw were just declaring a new instance of the random class. Is there something else I should be doing there as well?

/edit

OK, I think I see what you mean by seeding now after reading a bit more on it. http://www1.cs.columbia.edu/~lok/csharp/refdocs/System/types/Random.html

That could be it. Think I might try setting it to a timer off the system clock like they did here and see if I come up with any better results. That could explain why I was getting the same value when letting it run with the loop. Worth a shot, anyway.

Code:
public static int RandomNumber(int min, int max)
{
       //define new object of the Random class
       Random rand = new Random(unchecked((int)DateTime.Now.Ticks));

       //returning random int from args passed in
       return rand.Next(min, max);
}

So you don't apparently need to seed the random object. Doing so, though, would allow you to return the same sequence of results, or the same value. Seeding with the timer looks like it will allow you to change the seed every time the system clock ticks (though I'm not sure if Tick refers to milliseconds or seconds), which could be helpful in this situation. I'll test it tonight and see how it goes.

/edit 2

Thinking about it a little bit more. Does the fac that I declared this method as a static method, rather than an instance method have any bearing on the value that's returned over a given time span? Something to try as well, I suppose.
 
Last edited:
Nothing is seeming to work. I think I'm officially stumped here. I've tried seeding my random number generator with a random number, I've tried passing in an extra arg based on my "i" variable when I set up my for loop (for(int i = 0; i < _diceNeeded; i++)). I've tried seeding it based on the timer. Nothing seems to be working right. I even tried setting up an indexer to pass info into a class property, then fetching it back out. Nothing I'm trying is working. :-/

Code:
        public static int RandomNumber(int min, int max)
        {
            //define new object of the Random class
            Random rand = new Random(unchecked((int)DateTime.Now.Ticks));

            //returning random int from args passed in
            return rand.Next(min, max);
        }

        public static int MultiRandom(int diceNumber, int min, int max)
        {
            Random randomized = new Random(diceNumber);
            
            return randomized.Next(min, max);
        }

        public static int CreativeRandom(int min, int max)
        {
            int seed;
            Random rand = new Random();

            seed = rand.Next();

            Random randomized = new Random(seed);
            return randomized.Next(min, max);

        }

None of these seem to work... Stumped, I think...

The middle seemed to work somewhat. It returned 3 different numbers, but it would return the same 3 numbers every time the method was called, due to the static seed.

This thought process sparked the third option... but still no luck there... Back to the drawing board...
 
I conquered it! :D:D:D

Took a little doing, but I got it. Here's what I did:

Code:
        private void multiDmgBtn_Click(object sender, EventArgs e)
        {
            myMultiDmg = new TwentySider();

            try
            {
                myMultiDmg.BaseAttack = int.Parse(multiModBox.Text);
                _dieType = int.Parse(multiDieType.Text);

                _diceNeeded = int.Parse(multiDiceNeeded.Text);

                int[] myDiceArray = new int[_diceNeeded];
                int _arrayTotal = 0;

                Random randomizeMe = new Random(unchecked((int)DateTime.Now.Ticks));

                for (int i = 0; i < _diceNeeded; i++)
                {
                    myDiceArray[i] = TwentySider.CreativeRandom(randomizeMe.Next(), 1, _dieType + 1);
                    _arrayTotal += myDiceArray[i];
                    //arrayContents.Text = "Roll " + i.ToString() + ": " + myDiceArray[i].ToString() + "\n";
                }

                //multiDieRolls.Text = myDiceArray[0].ToString() + " + " + myDiceArray[1].ToString() + " + " + myDiceArray[2].ToString();

                multiDmgResults.Text = _arrayTotal.ToString();
            }
            catch
            {
                MessageBox.Show("Please enter a whole number.");
                multiDmgResults.Text = _origMultiDmg;
            }
        }


And here's my static method I called from the TwentySider class:

Code:
        public static int RandomNumber(int seed, int min, int max)
        {
            Random randomized = new Random(seed);
            return randomized.Next(min, max);
        }

So basically, I'm passing 3 args into my method. One for a seed, the other two for a min and max random number. The seed I pass in is based off another instance of the random class that's being seeded by ticks of the windows system clock. I had to get a little jiggy with what I was passing around there, but it works. Next step will be to have the individual values held in each array member display in lines of a list box and I'll be just about done. :D
 
Last edited:
So basically, I'm passing 3 args into my method. One for a seed, the other two for a min and max random number. The seed I pass in is based off another instance of the random class that's being seeded by ticks of the windows system clock. I had to get a little jiggy with what I was passing around there, but it works. Next step will be to have the individual values held in each array member display in lines of a list box and I'll be just about done. :D

My guess is that your code ran fast enough that the clock didn't tick between each time you initialized a new random object. Thus, they all got seeded with the same number and all gave the same number when you asked for one.

An alternative might be to pass the random object itself instead of a seed value. That way, since you won't be recreating the object, you don't have to worry about reseeding it.

Code:
       ...
       myDiceArray[i] = RandomNumber(randomizeMe, 1, _dieType + 1);
       ...

        public static int RandomNumber(Random randomized, int min, int max)
        {
            return randomized.Next(min, max);
        }

Or you can simplify it by removing the function all together.
Code:
       ...
       myDiceArray[i] = randomizeMe.Next(1, _dieType + 1);
       ...
 
I'll try that, and I think that's exactly what was happening. The loop was accessing the random class too fast for the seed value to change, so I ended up passing a random object seeded with the system clock into my method that would fire back another random number seeded by the random number I passed in.

I'll try the simplified route and see what kind of results I can get with it. It works great now as I've got it posted above, but I'm all for streamlining my code... so I'll give it a whirl and see how it goes.

The benefit that I found with reseeding my object with another random object seeded by clock ticks was that it seemed to generate more random numbers, if that makes sense. Some of the members of my array would tend to stick. If I printed my array members to a label, some of them wouldn't change as quickly... it may look like:

2 4 7 9 7

The next roll might look like:

6 3 7 9 7

The next one might look like:

9 1 6 9 7

Then I'd finally get an entirely new set of numbers after a few clicks:

7 3 4 6 2

When I seeded my first random number object with the system clock and passed it into my method to get a random number (which is an overloaded version of the original), it seemed to work fine. I'd get new numbers every time I clicked. Nothing would stick.

It's interesting how C# calculates random numbers. Definitely a great learning experience. I'll see what I can do about simplifying it when I get home from work, though. Thanks for the tips. :)
 
Code:
       ...
       myDiceArray[i] = randomizeMe.Next(1, _dieType + 1);
       ...

So I tried your advice, mccoyn. The second option worked just fine. I'm wondering if it was just the fact that the random class was being accessed so quickly within a static method rather than a local instance of itself? Interesting. But anyway, it streamlined my code a little bit. Thanks! :)
 
Back