Learning OO Programming Via C#?

Eluros

Member
Jul 7, 2008
177
0
0
Greetings,

In my daily job, I use SQL (which is obviously not an object oriented language) on a daily basis, and enjoy it quite a bit. However, for quite some time now, I've wanted to learn about object oriented programming-- it seems quite powerful, and it seems to be where to go for application development. Unfortunately, I've found that it's a bit difficult, as I have no formal education in programming (I was a philosophy major in college, so at least I have a strong background in formal logic), and my only experience with programming has been with non-OO languages.

I've invested about 12 hours, so far, learning about OO programming and C#. I downloaded Microsoft's XNA framework and have dabbled with creating basic Windows games. I'm getting it, but it's coming really slowly. I can explain what certain concepts (void, static, private, etc) mean, but knowing when and not to use them outside the context of a tutorial is quite difficult. As far as I can tell, my SQL experience has no substantial crossover with C#/OO knowledge.

So, I'm curious: am I going about this the right way? Is it unusual for it to still be extremely difficult after ~12 hours of study/work? I've spent quite a bit of time at http://www.functionx.com/csharp/index.htm for my training-- is that a good place to go?

Ultimately, my goal is to have the ability to develop basic applications in my spare time. I don't see myself using this regularly in my job, but I'm close friends with an independent application developer (who primarily creates Android/XBox/WP7 apps) and would love to be able to help him out, on occasion.

Thanks! I know it's a somewhat ambiguous topic, but I'm here to learn. Any advice/recommendation you can give me is welcome, even if it's not directly on-topic. Feel free to heap on the criticism, too. Whatever works.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
SQL isn't entirely void of OO principles. It has the attributes part down pat.

With Sql, you might have a database which has a table which has several columns. In oo terms, each of these (Database, table, columns) are objects. (even the syntax is similar, the '.' operator).

When doing OOP, what you are generally doing is first describing what the object looks like (the data it contains) and how it works with the world (The functions it has). This does not describe what is in the object any more than how creating a table describes what is contained in each row.

So with C# you have classes, classes are the scaffolding that describes what an object looks like. The data members (ints, chars, etc outside of functions) describe the sort of data a class contains, and the functions themselves describe how the class behaves.

With Sql databases you have relations. It is somewhat similar in OOP. On class can be (is a) just an extension of another class which gives it all the data members and functions of that class it inherits from (sort of like a join, with similar sorts of problems, IG what if both classes contain the same named data member or function.)

Yep, most of the principles of relational databases translate pretty well to OOP concepts. The main difference is that now you have to describe how each object works (sort of like stored proceedures).

hope this sort of helped
 

Eluros

Member
Jul 7, 2008
177
0
0
Thanks, Cogman; that did help. I never really thought of those analogies, but they make sense, more or less. If you were looking to write an article, it'd be interesting to read OO concepts explained in relational database terms, and vice versa.

I appreciate your thoughts. Every little bit helps in understanding the OO concepts.
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
Eluros,

You just need a good mentor to go over a few of the basics with you.

I'm guessing with void, static, private, etc you are referring to functions within classes.

Let's take a step back and go over basic object orientation, so you can wrap your head around the concepts so those will mean something to you.

Object Orientation is a way to start with very basic components, then building ontop of them until you get a more complete product.

Let's start with a class named:

Lifeform

What are basic things which are attributes of life? Is it alive, is it dead? Sure. We build into that class, it has a local variable which describes its state. Maybe a boolean, for Alive. We add property "IsAlive" to the class (I cut some code out of these examples to give you a quick look at it, so don't expect any of this to work if you copy/paste)

Code:
public class Lifeform
{

	public bool IsAlive {

		get { }

		set { }
	}



}


Now, we get into something really funky. There is no entity out there, or animal, or creature called "Lifeform", so this is not something that can be created and used on it's own, something needs to "inherit" it, or use its functionality since it cannot be used on it's own by itself. So therefore, we have to mark this class as "abstract".

Code:
public abstract class Lifeform
{

[snip]

}


Now when we think of object orientation. Classes fall into 2 categories. "Is a", or "has a". A mammal has a lifeform, or a mammal is a lifeform... Obviously the second. This is used as a way to determine how the classes should interact with one another. If something "is a" something, then it needs to inherit the object. So let's create a class for that below.

Code:
public abstract class Mammal: Lifeform
{

	public bool IsNocturnal {

		get { }

		set { }
	}

}

A mammal is considered a lifeform, so therefore, we inherit from Lifeform (the last portion of that class declaration after the colon). Since a mammal cannot be created on it's own so we must use Abstract on the mammal class much like the lifeform class. When we inherit it makes the new class look exactly like previous class, (which is known as a baseclass). Therefore, the Mammal class looks exactly like the Lifeform class, but also lets us add on top of what we had with just Lifeform. So when we interact with Mammal, we also have "IsAlive" that is found in the lifeform class. But now we also have "IsNocturnal". Lifeform doesn't need to know IsNocturnal, I don't think bacteria care if its light or dark outside and doesn't operate any differently, so this might be something we add to the Mammal class.

Now we get into a real class: Cat... We inherit from mammal like below. We also drop off the abstract.

Code:
public class Cat: Mammal
{

	public bool IsDomesticated {

		get { }

		set { }
	}


}

So now if we interact with Cat... We have the following properties of Cat. IsDomesticated, IsNocturnal, IsAlive...

Now we get into a new concept: Virtual functions.

Code:
public abstract class Mammal: Lifeform
{

	public bool IsNocturnal {

		get { }

		set { }
	}

	public virtual void Eat()
	{
                    Hunger = Hunger - 100;
	}



}


All mammals need to eat, so we add a virtual function called "Eat"... We don't have a return parameter so the return type is void... We mark the function as virtual. Which means: We can insert basic functionality in here for eating that applies to all mammals... But it allows the cat to override the functionality in case they want to do something special. So for mammal we might have a line in there like :

Code:
Hunger = Hunger - 100

So now the mammal is less hungry.

But for cat we may want:

Code:
public class Cat : Mammal
{


	public override void Eat()
	{

                if (food == "catnip")
               {
                     GoCrazy();
               }
               else
               {
                       base.Eat();
               }


	}

}

This overrides the base class Eat. Notice the else there? base.eat()... Lets you call the Eat in Mammal from the Cat class.

If the class which inherits Mammal does not include an Eat function, the default found in Mammal will be used.

You probably noticed that most everything here is Public... That means anything using the class can call the function or access the property. But you notice that in the Eat function for Cat, we have "GoCrazy." We might mark that function as private. That means only the Cat class can call that function internally. When a cat eats catnip, they go crazy... But a cat naturally doesn't go crazy on its own (thats up to debate but you see what I'm talking about here) so an external object/program cannot invoke the GoCrazy function on their own, the Cat does that internally on its own.

A static function basically means you are adding a function that does not require a particular instance of the object to call. Think of it like:

We are declaring cats (creating an instance):

Code:
Cat Fluffy = New Cat();
Cat Sparky = New Cat();

If (Sparky.IsAlive() == True)
{
   Sparky.Eat(Fluffy);
}

We have Fluffy and Sparky that is doing stuff. Static would be something like this:

Code:
Cat Stray = Cat.CreateWildCat();

There is no instance of Cat... We are just calling a function that applies to the cats's class, but is not an actual cat.

Code:
public class Cat
{

	public static Cat CreateWildCat()
	{

                 Cat NewCat = New Cat();
                 NewCat.IsDomesticated = false;
                 return NewCat;

	}


             public Cat HaveAKitty()
             {

                 Cat NewCat = New Cat();
                 NewCat.IsDomesticated = true;
                 return NewCat;


              }

}

As you can see there. One is static and one is not static but do nearly the same same thing.

Code:
   Cat Sparky = New Cat();
   Cat Fluffy = Sparky.HaveAKitty();

   Cat Wild = Cat.CreateWildCat();

One is created by another cat, the other is just created on its own without an existing cat.

I hope this exposes you to a bit more.
 
Last edited:

Eluros

Member
Jul 7, 2008
177
0
0
Brandon,

Wow-- thanks for the great introductory guide! I'm definitely impressed. That helps to clarify quite a few things, including abstraction, virtual functions (the importance of which I really didn't get beforehand), and OO inheritance in general.

I've written a few questions, but after giving them some thought, I think I was able to answer them. Still working through a few things in my head, but it's really clicking more than it has. Thanks so much.
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
Eluros,

The only other thing is Polymorphism. Which I will go over now, and you should have the basics of OO programming down.

Building on my previous example. Let's say we create a function somewhere in the code that looks like this:

Code:
void MakeMammalEat(Mammal myMammal)
{
   myMammal.Eat();
}

The type of of mammal, and since Mammal is an abstract class, we cannot create a mammal on its own, so the function doesn't make any sense. However, since a Cat does inherit Mammal, it is a valid variable to pass into this function. A Dog class may also inherit Mammal, and that would also be valid variable to pass in there.

Now, if you are passing in Cat in there, and calling the eat function, which function gets called? The Cat's Eat, or the Mammal's Eat? The answer is the Cat's eat... It overrides the virtual function in Mammal. Since we are only working at the mammal level, and not the cat level, we only have access to what we know about Mammals. We'd never* be able to have that function look at the "IsDomesticated" property of the cat, even though we passed a cat into the function, because as far as the function is concerned, we passed a mammal in, not a cat, and the function is only looking at the mammal subset of the cat.

(*=that is not true, but we won't get into that in this response)

Now if you think about it. That is a very powerful thing to be able to do. You can create functions which work on base classes but allow another class to totally "hijack" the functionality by overriding it. I will show you a real world scenario below on how this is used a bit later.

Another cool thing with Polymorphism is that we can declare variables as base classes and then create an instance of a specific class: Take a look at this:

Code:
Mammal Fluffy = New Cat();
Fluffy.Eat();

Ok. We create a variable as a base class type here (Mammal), but since Mammal is abstract, we cannot create a New Mammal by itself. But since we can create a Cat, and have it look like its base class Mammal, that is ok and valid... When we interact with Fluffy now, we don't know it's a cat. We know its a mammal. So we wouldn't have access to "IsDomesticated" when using Fluffy, but we would be able to access anything Mammal can. Since Mammal can Eat, we can call that function. Since Cat Overrides Eat, when we call Fluffy.Eat(), we are calling the Cat's Eat function, not the Mammal's Eat function.

So its much like the function above, we turn a cat into a mammal. But the reverse is also true, we can make a mammal from a cat.

I know this concept is a bit harder to understand. Polymorphism lets you look at your classes differently provided there is an association there.

Why is all this important and what would be the advantages of doing this? The answer is quite simple. You can create base classes which expect other classes to override functionality, but your code always interacts with the base class. It's a way to create an interface.

-----

I'll take a real world business situation here from my current job to give you an example.

We have files which get imported from clients into our system. The program contains basic functionality. Open File, read in a line, process the line, move to the next line (if available), close the file.

I create a base class ImportBase, which does all that stuff... But the question here is, what if the clients send us different types of data, different formats? We have to handle that differently? and we can. But all the other boilerplate code is handled in the base class... No need to copy paste the open file function from one class to another, when all the classes can share.

Look at the code

Code:
ImportBase MyImporter;

If (CommandLineParameter == "1")
{
  MyImporter = new Client1;
}

If (CommandLineParameter == "2")
{
  MyImporter = new Client2;
}

If (CommandLineParameter == "3")
{
  MyImporter = new Client3;
}

MyImporter.OpenFile();
MyImporter.ReadInLines();
MyImporter.ProcessLines();
MyImporter.CloseFile();

When I create Client1 class (or Client2, Client3), I inherit from ImportBase, and only override ProcessLines. I can also add in private functions, or whatever else I need into the Client1 class to make it do its job correctly. But on the outside of the class, I just work with it in a very generic manner as if it was the base class. I use the same variable, and just do a New on the different classes based on what ID they type in with a command line argument.
 

Eluros

Member
Jul 7, 2008
177
0
0
Brandon,

Thanks for the post on polymorphism. I'll be honest-- the whole thing is a little unclear to me, but it's a slow and steady sort of process. Let me see if this is right; sorry if the format of my answer is bizarre. I'm trying to format it in a way that makes sense to me.

If...
1. Class X inherits Class Y. //Class "Cat" inherits Class "Mammal"
2. Class X has function a(). //Class "Cat" has function Eat()
3. Class Y has function a(). //Class "Mammal" has function Eat()
4. Class X has function b(). //Class "Cat" has function IsDomesticated()
5. Class Y does not have function b(). //Class "Mammal" does not have function IsDomesticated()


Then...
6. If we create an X from the Y class, the z() function will default to the z() function from the X class. //If a new Mammal is created as a Cat, it will Eat() as a Cat.
7. If we create an X from the Y class, we cannot create a b() function, because b() is not found in the Y class. //If a new Mammal is created as a Cat, we cannot see IsDomesticated()

Is that correct? I'm unsure if I should rewrite (6) as the following:
6. If we create an X from the Y class, and the z() function is in an "override" call, the z() function will default to the z() function from the X class. //If a new Mammal is created as a Cat, and Eat() is in an override call from Cat, it will Eat() as a Cat.

Seriously, thanks so much. This is incredibly helpful.

On a related note, I have requested the following via Interlibrary Loan:
http://www.amazon.com/Microsoft-XNA-.../dp/0735651574

From what I understand, it's a good book for people who are completely new to programming, and really helps to lay out the fundamentals. I figure that I can continue my studies with C# on my own with free online resources, and by the time the book gets here, I'll hopefully be ready to practice implementing it. Seem like a good decision?
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
Actually, I think the both ways you wrote it actually apply for #6. Both are true. Let me explain.

Object Orientation can be thought of as having 2 parts:

Interface (what it looks like, and how you interact with it), and
Implementation (what it does, or how it acts).

Polymorphism is dealing with the interface (how you interact with it), and inheritance and all that is dealing with its implementation (how it acts). Writing the classes is creating the implementation. Writing code which uses those classes is dealing with the interface.

When we declare variables, we specify a type:

Code:
Mammal Fred;
Cat Fluffy;

The type is the interface. Fred is going to look and interact with me like a Mammal. Fluffy is going to look and interact with me like a cat.

It has nothing to do with what is actually created or what they are. The new statement indicates the actual implementation (including any overrides/inheritance.)

Code:
Mammal Fred = New Dog();

Fred looks (and interacts with me) like a mammal, but its implementation (how it acts) is like a dog!

If we tell Fred to Eat, it's still going to eat like a Dog, even though the most we can tell of him is that he is a mammal. We can't tell Fred to Bark like a dog, because we're not even sure if he is a dog. So any Dog specific stuff can't be used when we declare Fred as a Mammal... But since he is a dog, he will still act like a dog.

Does that help?

Edit-

With that, think of my real world business example up there.

The classes interact with me like an importer, but it acts like the certain client should.

I believe the reason why its confusing is classes can be both interface AND/or implementation. Dealing with a cat would have a cat interface, but its implementation might be the cat class, and/or the mammal, and/or the lifeform class. To any degree... Likewise dealing with a mammal would have a mammal interface, but its implementation might be in the cat class (or dog class), and/or the mammal class, and/or the lifeform class. To any degree.
 
Last edited:

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
You talk about having experience with no-OO languages, but you never mention any languages other than SQL... which ones have you used? What parts of object oriented programming have given you issues over your studies so far?
 

Eluros

Member
Jul 7, 2008
177
0
0
Actually, I think the both ways you wrote it actually apply for #6. Both are true. Let me explain.

Object Orientation can be thought of as having 2 parts:

Interface (what it looks like, and how you interact with it), and
Implementation (what it does, or how it acts).

Polymorphism is dealing with the interface (how you interact with it), and inheritance and all that is dealing with its implementation (how it acts). Writing the classes is creating the implementation. Writing code which uses those classes is dealing with the interface.

When we declare variables, we specify a type:

Code:
Mammal Fred;
Cat Fluffy;

The type is the interface. Fred is going to look and interact with me like a Mammal. Fluffy is going to look and interact with me like a cat.

It has nothing to do with what is actually created or what they are. The new statement indicates the actual implementation (including any overrides/inheritance.)

Code:
Mammal Fred = New Dog();

Fred looks (and interacts with me) like a mammal, but its implementation (how it acts) is like a dog!

If we tell Fred to Eat, it's still going to eat like a Dog, even though the most we can tell of him is that he is a mammal. We can't tell Fred to Bark like a dog, because we're not even sure if he is a dog. So any Dog specific stuff can't be used when we declare Fred as a Mammal... But since he is a dog, he will still act like a dog.

Does that help?

Edit-

With that, think of my real world business example up there.

The classes interact with me like an importer, but it acts like the certain client should.

I believe the reason why its confusing is classes can be both interface AND/or implementation. Dealing with a cat would have a cat interface, but its implementation might be the cat class, and/or the mammal, and/or the lifeform class. To any degree... Likewise dealing with a mammal would have a mammal interface, but its implementation might be in the cat class (or dog class), and/or the mammal class, and/or the lifeform class. To any degree.

Brandon,

I hadn't really thought about it in terms of implementation versus interface, but that makes sense. You wrote, "Writing the classes is creating the implementation... Writing code which uses those classes is dealing with the interface." That's excellent-- very clear, concise, and substantial in content. Again, thanks for the input.

You talk about having experience with no-OO languages, but you never mention any languages other than SQL... which ones have you used? What parts of object oriented programming have given you issues over your studies so far?

Aikouka,

To be honest, SQL is the only language I would consider myself marketable with. I actually have some experience with Ruby (which is OO), but my knowledge mostly consisted of tweaking others' programs/scripts. Like everyone and their grandmother, I've played around with various forms of BASIC and propriety languages. I configured a working pseudo-clone of Mortal Kombat on my TI-83 calculator, back in HS. My non-SQL experience is nothing to write home about, to be sure.

Learning/memorizing some of the pre-built classes can be tricky, because I don't always know the best way to research/learn what I want to. For example, last night, I was trying to assign a random value to a variable, where the lower limit of the range was a variable divided by two and the upper limit was that variable multiplied by two. Obviously, I have a lot of learning to do-- but I'm not sure how to find answers to questions like that. Five minutes (or so) of Google searches didn't turn up a meaningful answer; neither did Microsoft's online resources. I have no expectation of people holding my hand, but I'm finding that it's difficult to establish how to go about learning answers to specific questions.

As I think I mentioned in another post, I'm getting to where I abstractly understand many of the modifiers for classes (void/static/virtual/etc), but it's still difficult to know how and when to apply them. That will come with practice, I'm sure. There's plenty of fundamentals that I need to keep working through.

Other than that, things are going well. It's a lot to learn, and with other obligations, I'm lucky to have three hours a day to play with it all. However, I figure that if I can steadily keep moving forward, it's alright if it's slow.
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
To be honest, SQL is the only language I would consider myself marketable with. I actually have some experience with Ruby (which is OO), but my knowledge mostly consisted of tweaking others' programs/scripts. Like everyone and their grandmother, I've played around with various forms of BASIC and propriety languages. I configured a working pseudo-clone of Mortal Kombat on my TI-83 calculator, back in HS. My non-SQL experience is nothing to write home about, to be sure.

I was a little mystified when I first read this thread because it seemed you were trying to jump into OO concepts without having a strong foundation in programming itself. The XNA book you picked up... I probably would not start with that. It may sound degrading, but pick up a good Dummies book... they've never failed me in the past, and it's actually how I started learning programming back in high school with Visual Basic (thinking of VB makes me shudder a bit). The idea is to start learning some of the more basic concepts and then worry about things like inheritance and polymorphism. I don't think those two concepts even came up in my intro computer science course in college, but I can't really remember.

I'd recommend to try and find a book like this one:
http://www.amazon.com/C-2010-All---O...dp/0470563486/

You could also probably use an older one if necessary... I'm actually not sure what's different other than using a different version of Visual Studio (2008 vs 2010), so one probably uses a newer .NET framework.

Learning/memorizing some of the pre-built classes can be tricky, because I don't always know the best way to research/learn what I want to. For example, last night, I was trying to assign a random value to a variable, where the lower limit of the range was a variable divided by two and the upper limit was that variable multiplied by two. Obviously, I have a lot of learning to do-- but I'm not sure how to find answers to questions like that. Five minutes (or so) of Google searches didn't turn up a meaningful answer; neither did Microsoft's online resources. I have no expectation of people holding my hand, but I'm finding that it's difficult to establish how to go about learning answers to specific questions.

I wouldn't really waste your time memorizing anything. Being a programmer isn't really about how many APIs you know. I'm not as seasoned as some of the people here, but I look stuff up on Google all the time. I mean... just because I can psuedo-code a program doesn't mean I know the Java code to make it happen. Like recently, I've been doing a lot of Googling on Java GUI stuff as when it comes to GUIs, I've mostly worked in Microsoft's domain (mostly C#).

But then again there are also weird things about languages that you just have to know. A good example is actually a problem that I was having with a Java GUI. I was creating textual elements (buttons, labels, etc) with text of unknown size. I wanted the elements to never change size, so I specified the same minimum and maximum size, but I ran into a strange problem. Although the textual elements were the right size, other elements were being shifted around. It turns out that there's some strange bug in Java SWING where a text-based GUI component's perceived size within the layout can be affected by the unconcatenated string's length unless you specify a preferred size. This occurs even though Java automatically concatenates your string!

Who woulda thunk it?

But anyway... the point is, you can find a ton of stuff on Google. I'm not sure what sort of searches you were doing, but you could probably find that easily by just searching for "C# random number". You probably won't need anything more advanced than that as any tutorial on randoms typically teaches how to define the minimum and maximum value. The first search result seems pretty good... so what sort of things were you searching for?

http://social.msdn.microsoft.com/Forums/eu/Vsexpressvcs/thread/55fb3116-c978-4ac8-9381-a2605e16e256

As I think I mentioned in another post, I'm getting to where I abstractly understand many of the modifiers for classes (void/static/virtual/etc), but it's still difficult to know how and when to apply them. That will come with practice, I'm sure. There's plenty of fundamentals that I need to keep working through.

I think you be mixing some of the method qualifiers up and that may be causing some confusion. Keeping to the basics...

Void is typically used as a return type for a method ( i.e. public void doSomething() ). A method having a "void" return type means that it will not return anything after it is done processing.

Static on the other hand describes how a method can be accessed. A class being static basically means that the class only contains static methods. A good C# example of a class with lots of static methods is the Math class ( http://msdn.microsoft.com/en-us/library/system.math(v=vs.71).aspx ). When a method is static, you only need to call its containing class to access it. An example is how Math contains a static method Abs. To access this, you type Math.Abs. If a method is not static, you must create an instance of that class first. Static methods are really common when you need to write simple utility methods, which the Math class is chock full of. That "utility method" terminology isn't anything specific to C# that you need to worry about; in this context I just mean anything with a constant set of steps that only requires the input data to function. Math.Abs(#) is still a good example as it returns the absolute value of the number provided.

Virtual describes how a method is handled when dealing with polymorphism. Right now, I would forget all about virtual... it's beyond what you should be learning at the moment.

A similar thing you may want to be aware of is what's called a method's signature. A signature is what defines a method and must be unique in that class. What is part of the signature is dependent on the language, but it's typically the method name and the parameters (order and type does matter). For example, "public void x (int y)" and "public void x (char y)" are allowed as the parameters are different, but "public void x (int y)" and "public void x (int z)" are not allowed because the parameter's name does not matter, only the type. I mentioned that order matters as well, which means that "public void x (int y, char z)" and "public void x (char z, int y)" are allowed as well.

Having multiple methods with the same name but different parameters is called "overloading." This is fairly common in some APIs such as Math. Here's an example of all the different types of the method Abs:

http://msdn.microsoft.com/en-us/library/system.math.abs(v=vs.71).aspx
 

YoungGun21

Platinum Member
Aug 17, 2006
2,551
1
81
I just want to say this thread is awesome and to keep going. I'm going to be learning C# soon, so I'll probably reference this thread tons.

Thanks!
 

Eluros

Member
Jul 7, 2008
177
0
0
I was a little mystified when I first read this thread because it seemed you were trying to jump into OO concepts without having a strong foundation in programming itself. The XNA book you picked up... I probably would not start with that. It may sound degrading, but pick up a good Dummies book... they've never failed me in the past, and it's actually how I started learning programming back in high school with Visual Basic (thinking of VB makes me shudder a bit). The idea is to start learning some of the more basic concepts and then worry about things like inheritance and polymorphism. I don't think those two concepts even came up in my intro computer science course in college, but I can't really remember.

No worries-- I understand exactly where you're coming from. My hope was that I'd work through some basic concepts through online resources. It sounds, though, like you think I'll need to spend a good bit more time with the fundamentals before touching the higher level stuff. Is that correct? I had assumed, perhaps falsely, that I could lean OO and pre-OO concepts hand-in-hand, but if the best way is to start with the basic pre-OO concepts, then that's what I should do.

I'd recommend to try and find a book like this one:
http://www.amazon.com/C-2010-All---O...dp/0470563486/

You could also probably use an older one if necessary... I'm actually not sure what's different other than using a different version of Visual Studio (2008 vs 2010), so one probably uses a newer .NET framework.

Thanks for the recommendation. I may see if I can find that one.

I wouldn't really waste your time memorizing anything. Being a programmer isn't really about how many APIs you know. I'm not as seasoned as some of the people here, but I look stuff up on Google all the time. I mean... just because I can psuedo-code a program doesn't mean I know the Java code to make it happen. Like recently, I've been doing a lot of Googling on Java GUI stuff as when it comes to GUIs, I've mostly worked in Microsoft's domain (mostly C#).

But then again there are also weird things about languages that you just have to know. A good example is actually a problem that I was having with a Java GUI. I was creating textual elements (buttons, labels, etc) with text of unknown size. I wanted the elements to never change size, so I specified the same minimum and maximum size, but I ran into a strange problem. Although the textual elements were the right size, other elements were being shifted around. It turns out that there's some strange bug in Java SWING where a text-based GUI component's perceived size within the layout can be affected by the unconcatenated string's length unless you specify a preferred size. This occurs even though Java automatically concatenates your string!

Who woulda thunk it?

But anyway... the point is, you can find a ton of stuff on Google. I'm not sure what sort of searches you were doing, but you could probably find that easily by just searching for "C# random number". You probably won't need anything more advanced than that as any tutorial on randoms typically teaches how to define the minimum and maximum value. The first search result seems pretty good... so what sort of things were you searching for?

http://social.msdn.microsoft.com/Forums/eu/Vsexpressvcs/thread/55fb3116-c978-4ac8-9381-a2605e16e256

I had the idea of random numbers for C# down; my problem was that I couldn't figure out how to allow the lower limit of the range to be a variable from another class divided by two and the upper limit was that variable multiplied by two. I'm at work, so I can't see exactly what I was doing, but it was something like the following:

Code:
Random rand;
rand = new Random()
chance = rand.Next((hero.heroVIT/2), (hero.heroVIT*2));

That may be completely off base, but at any rate, I was having a hard time figuring out what was wrong, as I had initiated my heroVIT integers in another class. Of course, since I'm partially trying a figure-it-out-as-I-go sort of approach, it's possible I'm missing something significant.

I think you be mixing some of the method qualifiers up and that may be causing some confusion. Keeping to the basics...

Void is typically used as a return type for a method ( i.e. public void doSomething() ). A method having a "void" return type means that it will not return anything after it is done processing.

Static on the other hand describes how a method can be accessed. A class being static basically means that the class only contains static methods. A good C# example of a class with lots of static methods is the Math class ( http://msdn.microsoft.com/en-us/library/system.math(v=vs.71).aspx ). When a method is static, you only need to call its containing class to access it. An example is how Math contains a static method Abs. To access this, you type Math.Abs. If a method is not static, you must create an instance of that class first. Static methods are really common when you need to write simple utility methods, which the Math class is chock full of. That "utility method" terminology isn't anything specific to C# that you need to worry about; in this context I just mean anything with a constant set of steps that only requires the input data to function. Math.Abs(#) is still a good example as it returns the absolute value of the number provided.

Virtual describes how a method is handled when dealing with polymorphism. Right now, I would forget all about virtual... it's beyond what you should be learning at the moment.

A similar thing you may want to be aware of is what's called a method's signature. A signature is what defines a method and must be unique in that class. What is part of the signature is dependent on the language, but it's typically the method name and the parameters (order and type does matter). For example, "public void x (int y)" and "public void x (char y)" are allowed as the parameters are different, but "public void x (int y)" and "public void x (int z)" are not allowed because the parameter's name does not matter, only the type. I mentioned that order matters as well, which means that "public void x (int y, char z)" and "public void x (char z, int y)" are allowed as well.

Having multiple methods with the same name but different parameters is called "overloading." This is fairly common in some APIs such as Math. Here's an example of all the different types of the method Abs:

http://msdn.microsoft.com/en-us/library/system.math.abs(v=vs.71).aspx

Thanks for the good reading. That definitely helps clarify some things.

I just want to say this thread is awesome and to keep going. I'm going to be learning C# soon, so I'll probably reference this thread tons.

Thanks!

This thread is a tremendous help to me, as well; glad it's benefiting others, as well. Best of luck with your C# studies.
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
No worries-- I understand exactly where you're coming from. My hope was that I'd work through some basic concepts through online resources. It sounds, though, like you think I'll need to spend a good bit more time with the fundamentals before touching the higher level stuff. Is that correct? I had assumed, perhaps falsely, that I could lean OO and pre-OO concepts hand-in-hand, but if the best way is to start with the basic pre-OO concepts, then that's what I should do.

I would definitely emphasize starting off at the beginning. The examples won't be nearly as cool as a gaming book, but they'll really hammer in the basic concepts and in a better step-wise fashion. As for book choice, you may also want to consider an old college textbook. They may be available second-hand for really cheap or available to borrow. The only issue is that I don't think many cover C# as the common intro language is typically Java. If you really want to stick with C#, I'd probably go with a Dummies book... you usually can't go wrong there!

I had the idea of random numbers for C# down; my problem was that I couldn't figure out how to allow the lower limit of the range to be a variable from another class divided by two and the upper limit was that variable multiplied by two. I'm at work, so I can't see exactly what I was doing, but it was something like the following:

Code:
Random rand;
rand = new Random()
chance = rand.Next((hero.heroVIT/2), (hero.heroVIT*2));
That may be completely off base, but at any rate, I was having a hard time figuring out what was wrong, as I had initiated my heroVIT integers in another class. Of course, since I'm partially trying a figure-it-out-as-I-go sort of approach, it's possible I'm missing something significant.

Ah, okay. The first thing I should probably ask about is the heroVIT integer (primitive?). I can't be 100% sure what the problem is without seeing what error was reported, but my first guess is that heroVIT is a private member variable of whatever class hero is a type of. This gets me into a point I wanted to make about such things. It's common convention in object oriented programming to use what are commonly called "getters" and "setters" to access member variables inside of a class. An example of this would be "public int getHeroVIT()" and "public void setHeroVIT(int newHeroVIT)"

This is typically done for two reasons:
1) It allows you to control if a variable can be accessed:
It's not uncommon to have variables that are considered "read only" and subsequently do not have a setter.

2) It allows you to perform any checks or transformations on the data:
This may not be as common in an application where you have complete control, but sometimes users are allowed to enter data, and you may want to check the information before simply allowing it be put to use.

There's one other thing that may be an issue, but I don't think it is. Division is a really tricky thing sometimes with programming languages as not all of them handle it the same way. What's hard to understand is when you divide two numbers, what data type will the result be? Sometimes this depends on the divisor as in some languages, using '2' will result in an integer quotient (result), but it could end up with a double quotient. The method you're using in the Random class takes two integers, so if your division resulted in a double, then it may give you an error (or it may truncate the value). Like I mentioned, this is very language dependent.
 

Eluros

Member
Jul 7, 2008
177
0
0
I would definitely emphasize starting off at the beginning. The examples won't be nearly as cool as a gaming book, but they'll really hammer in the basic concepts and in a better step-wise fashion. As for book choice, you may also want to consider an old college textbook. They may be available second-hand for really cheap or available to borrow. The only issue is that I don't think many cover C# as the common intro language is typically Java. If you really want to stick with C#, I'd probably go with a Dummies book... you usually can't go wrong there!



Ah, okay. The first thing I should probably ask about is the heroVIT integer (primitive?). I can't be 100% sure what the problem is without seeing what error was reported, but my first guess is that heroVIT is a private member variable of whatever class hero is a type of. This gets me into a point I wanted to make about such things. It's common convention in object oriented programming to use what are commonly called "getters" and "setters" to access member variables inside of a class. An example of this would be "public int getHeroVIT()" and "public void setHeroVIT(int newHeroVIT)"

This is typically done for two reasons:
1) It allows you to control if a variable can be accessed:
It's not uncommon to have variables that are considered "read only" and subsequently do not have a setter.

2) It allows you to perform any checks or transformations on the data:
This may not be as common in an application where you have complete control, but sometimes users are allowed to enter data, and you may want to check the information before simply allowing it be put to use.

There's one other thing that may be an issue, but I don't think it is. Division is a really tricky thing sometimes with programming languages as not all of them handle it the same way. What's hard to understand is when you divide two numbers, what data type will the result be? Sometimes this depends on the divisor as in some languages, using '2' will result in an integer quotient (result), but it could end up with a double quotient. The method you're using in the Random class takes two integers, so if your division resulted in a double, then it may give you an error (or it may truncate the value). Like I mentioned, this is very language dependent.

Thanks again for the feedback.

So, while I've decided to stick with C# (based on research and speaking with some developers I know), I do see the wisdom is working from a starter book. I went to Barnes and Nobles and looked around... after checking out several books, this seemed both understandable and thorough, and received good reviews on Amazon. Anyone have any feedback? I'm considering ordering it.

http://www.amazon.com/Head-First-2E-...ref=pd_sim_b_4

I'm making progress with my independent learning and test code... things are moving. Hopefully some basic practical experience will help me understand what the book, whatever book I end up choosing, says.
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
I've never seen the book in person, but if I'm around a bookstore tomorrow, I'll try to check it out. I did do a couple "Surprise Me!" looks on Amazon, and the book seems decent overall. It's got an interesting note aspect... I guess to make us pay attention to those little tidbits we tend to ignore.

It looks like it has some nifty GUI examples for you to program as well.
 

Eluros

Member
Jul 7, 2008
177
0
0
I've never seen the book in person, but if I'm around a bookstore tomorrow, I'll try to check it out. I did do a couple "Surprise Me!" looks on Amazon, and the book seems decent overall. It's got an interesting note aspect... I guess to make us pay attention to those little tidbits we tend to ignore.

It looks like it has some nifty GUI examples for you to program as well.

Thanks so much; by any chance, were you able to look at it? I'm planning on picking it up, if I don't hear otherwise.
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Thanks so much; by any chance, were you able to look at it? I'm planning on picking it up, if I don't hear otherwise.

Unfortunately, I haven't been to a bookstore yet. Such an uncommon place for me. I did read through the book a bit on Amazon, and it seems decent. I like some of the points that they made in the book as they're similar things that I would point out.

I did think that it was a little odd that it dug so far into a GUI application in the beginning, but I guess that shows a bit of Visual Studio.
 

Eluros

Member
Jul 7, 2008
177
0
0
Unfortunately, I haven't been to a bookstore yet. Such an uncommon place for me. I did read through the book a bit on Amazon, and it seems decent. I like some of the points that they made in the book as they're similar things that I would point out.

I did think that it was a little odd that it dug so far into a GUI application in the beginning, but I guess that shows a bit of Visual Studio.

No worries; I appreciate your willingness to check it out.

I spent about 4 hours working on C# last night (as I don't yet have a book, I've been continuing to use online tutorials), and something incredible happened: inheritance "clicked"! I'm not sure why, but there was a distinct moment when I believe that I "got" the idea. Suddenly, it seemed so obvious, and I understood why it was such a powerful tool. I scrapped my monster.monsterHP/hero.heroHP integers, for example, and replaced them with monster.HP and hero.HP, where "hero" and "monster" are children of the "Character" class. Heroes and Monsters are Characters, and I suddenly coding without parent/child relationships seemed cumbersome. I realized that I could code skills to refer to HP, rather than heroHP or monsterHP, and thus could build one skill-- but have any character use it.

Basically, I'm a happy camper. I wanted to keep working, and have a basic "skills" class parent with unique skill class children, but it was 12:30am and I realized I was already going to be tired enough.

I'm still working through much of the polymorphism stuff-- much of it is still not intuitive-- but I'm making slow and steady progress. I tend to jump around between several tutorials, as well as Google searches when I have additional questions. It's a lot of research, and a book would be more efficient, but it's making me really think about what I'm doing, which I suppose is good.
 
Last edited:

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Suddenly, it seemed so obvious, and I understood why it was such a powerful tool. I scrapped my monster.monsterHP/hero.heroHP integers, for example, and replaced them with monster.HP and hero.HP, where "hero" and "monster" are children of the "Character" class. Heroes and Monsters are Characters, and I suddenly coding without parent/child relationships seemed cumbersome. I realized that I could code skills to refer to HP, rather than heroHP or monsterHP, and thus could build one skill-- but have any character use it.

Bingo! Congratulations.

Polymorphism will hit you the same way after you've thought about it a bit. It's just introducing a behavior in the base class, so that all derived classes are known to have that behavior, but implementing it in the derived classes so that each can execute the behavior according to its specific nature. Imagine a Vehicle class with a Start method, and derived classes like Car, Plane, and Rocket. Obviously you can 'Start' all these vehicles, but the actual implementation of Start will be different for each.
 

Eluros

Member
Jul 7, 2008
177
0
0
Bingo! Congratulations.

Polymorphism will hit you the same way after you've thought about it a bit. It's just introducing a behavior in the base class, so that all derived classes are known to have that behavior, but implementing it in the derived classes so that each can execute the behavior according to its specific nature. Imagine a Vehicle class with a Start method, and derived classes like Car, Plane, and Rocket. Obviously you can 'Start' all these vehicles, but the actual implementation of Start will be different for each.

Thanks, Mark.

My wife graciously let me spend about four hours (ah, the joys of adulthood, when four hours of free time is a huge window) working on this stuff last night, which was great. I'm continuing to make quite a bit of progress. I have a console application that's essentially a turn-based arena combat RPG. The player has three skills, and it was quite easy to set up all sorts of enemies, with different skills/AI methods. It's able to run through complete battle loops, have the player level up, and all that. I'm making progress, and feeling much more comfortable with the language. It's very slow going, but I'm pleased with it, especially given that I started-- more or less-- without any OO knowledge.

If anyone else out there is looking to break into OO programming, my C# experience so far has been very positive. If you're like me, it may take awhile, but it's certainly doable. Microsoft's Visual C# Express 2010 is great, and the only issues I've had so far have been with my own knowledge/implementation, not the software or compiler.

I imagine that I'll have some questions soon, but I'll keep trekking ahead. If any other new programmers have questions about my experience, I'm happy to share.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |