Jump to content



Photo

I'm really getting frustrated.

Answered Go to the full post

  • Please log in to reply
205 replies to this topic

#196 +snaphat (Myles Landwehr)

snaphat (Myles Landwehr)

    Electrical & Computer Engineer

  • Tech Issues Solved: 29
  • Joined: 23-August 05
  • OS: Win/Lin/Bsd/Osx
  • Phone: dumb phone

Posted 23 January 2014 - 21:57

Considering the fact that you can't write the simplest of programs in C# without invoking the class keyword I tend to disagree. Take a look at Microsoft's own Hello World example. Immediately the reader is thrown into classes, objects, namespaces, static vs non-static class functions, and so on.

 

Someone writing C# code can't avoid OO. That's the nub of the matter. And that's why I advocate eschewing languages like it. The OP should strip all that away and use a simple imperative / procedural language. No complicated OO classes, namespaces, objects, polymorphism, operator overloading, inheritance, generics, or anything else that's completely unnecessary. By all means when someone has a firm grasp of things, but until then it's just a distraction.

Simpy put: you are not being fair. You can write an entire procedural program using a single class. This is conceptually no different from taking a C program and wrapping it in a C++ class. You do not need to understand or deal directly with the nature of objects, classes, inheritance, polymorphism, etc. do to this. At worse, you have to use namespaces in an analogous manner to #includes in C and deal with Strings. Doing these things required next to zero knowledge about OOP concepts. The OP is struggling with the concept of control flow structure and implementing basic decision making. What part of that requires an understanding of OOP concepts?

 

It's like FloatingFatMan said, the OP is trying to run before he can even stand. And what's more is that it doesn't matter if he uses a non-OOP language, he is just going to do the same thing with that unless he decides to pace himself and get the fundamentals down. If it isn't classes, it is going to be memory management, pointers, structs, and data-structures and you are in the same boat as now. This isn't a language issue, it's a pacing issue.




#197 FloatingFatMan

FloatingFatMan

    Resident Fat Dude

  • Tech Issues Solved: 1
  • Joined: 23-August 04
  • Location: UK

Posted 23 January 2014 - 22:34

It's like FloatingFatMan said, the OP is trying to run before he can even stand. And what's more is that it doesn't matter if he uses a non-OOP language, he is just going to do the same thing with that unless he decides to pace himself and get the fundamentals down. If it isn't classes, it is going to be memory management, pointers, structs, and data-structures and you are in the same boat as now. This isn't a language issue, it's a pacing issue.

 

TBH, and I mean no disrespect to the OP, but it's something I've seen time and time again; people's reach vastly exceeding their grasp.  Everyone today is in so much of a hurry and want what they want immediately, without putting in the time needed to actually learn those skills.

 

It's like those week long intensive training courses with a Microsoft certification exam at the end. The only thing you learn in those is how to pass the exam, that's it.  You still won't be any damn good programming in the language, because you haven't got the months and months that you absolutely NEED under your belt with using it.

 

Until the OP takes a good long look at himself, and realizes that he just doesn't have the basic grounding that he MUST have to be a decent programmer, he's going to continue to run on the spot, and his school course will be a complete waste of everyone's time.  He cannot get the knowledge he needs from books alone, especially with his additional needs. He needs a good teacher, he needs to practice, practice, practice, and he needs to control his aspirations.



#198 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 23 January 2014 - 23:14

Could anybody please explain to me in a very simple way (compare it to something in real life) what objects are, what a class is and what operators are and what all of these do?

First you should understand:

 

 - Functions

 - Basic data types (int, float, string)

 - Variables

 

Assuming you understand the above. Basic data types like integers and floats are great for representing single, scalar values, for example someone's height.

float height = 1.88m; // my height in meters

It's also easy to write functions that operate on single values using these types. Let's write a function that takes two floats and returns their sum:

float Sum(float a, float b) {
    return a + b;
}

We can call this function like this:

float result = Sum(3f, 5f); 
// now result is 8f

However, it becomes a problem when you want to represent composite values. For example, how would you represent a 2D coordinate in C#? Perhaps with two variables:

float X = 12f;
float Y = 15f;

Now, how would you write a function that takes two 2D coordinates and returns their sum? Note that the sum of two 2D points is a 2D point, i.e. mathematically:

 

(X', Y') = (X1, Y1) + (X2, Y2)

where

X' = X1 + X2

Y' = Y1 + Y2

 

So, you would need to take in 4 values (X1, Y1, X2 and Y2) and return 2 values (X' and Y'). Something like:

(X, Y) = Sum(x1, y1, x2, y2);

Alas, this is not valid C#! A function can only return one value; there is no way to write a Sum function such that this code will compile.

 

We'd need to somehow return our two floats as a single value...

 

Which is exactly where classes come in! In C#, we can define a new data type by compositing existing data types, like so:

class Point2D {
     public float X;
     public float Y;
}

This allows us to write our Add function easily:

Point2D Sum(Point2D a, Point2D b) {
    var result = new Point2D();
    result.X = a.X + b.X;
    result.Y = a.Y + b.Y;
    return result;
}

Here, Point2D is a class. a, b, and result are objects, i.e. instances of a class. We could have as many Point2D objects as we want (i.e. instances), but there's only one Point2D class (only one definition of Point2D). There's some new syntax here: the new operator and the dot operator. These allow us to create an object and access its members, respectively. 

 

Try not to stumble too much on the syntax. Simply understand that we defined a new type, "Point2D", as a composite of two floats, which we named X and Y. We can now create all the Point2Ds we want, just like we could create all the floats or strings we want. Point2D is a new data type, our own data type, and it has two members. We can now define functions that create and operate on this type. This allows us to use related data as a single value.

 

We could use our new Sum function like so:

var p1 = new Point2D();
p1.X = 10;
p1.Y = 10;
var p2 = new Point2D();
p2.X = 13;
p2.Y = 15;

var p3 = Sum(p1, p2);
Console.WriteLine("({0},{1}) + ({2},{3}) = ({4},{5})", p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y);
// prints "(10,10) + (13,15) = (23,25)"

Now, writing:

var p3 = Sum(p1, p2);

(where p1, p2 and p3 are Point2Ds) is ok, but it'd be much nicer if just like we do with floats, we could more naturally write:

var p3 = p1 + p2;

To do this, we need to define what the operator "+" means, when applied to two Point2Ds. Intuitively enough, we need to modify our definition of Point2D to specify this meaning:

class Point2D {
    public float X;
    public float Y;
    public static Point2D operator +(Point2D p1, Point2D p2) 
    {
        var result = new Point2D();
        result.X = p1.X + p2.X;
        result.Y = p1.Y + p2.Y;
        return result;
    }
}

Again, try not to focus too much on the syntax. We're defining "operator +" as a function that takes two Point2Ds and returns their sum, i.e. another Point2D. And now, the above code just works!

 

Look ma' I just learned operator overloading!

 

Finally, it'd also be nice if it didn't require three lines of code to create and initialize a Point2D:

var p1 = new Point2D();
p1.X = 3;
p1.Y = 5;
// All this typing!!! URRRGH

Wouldn't it be nice if we could just write this in one line, say like so:

var p1 = new Point2D(3, 5);
// AAAAH WAY BETTER

And this would automatically assign 3 to X and 5 to Y. C# allows us to do this with special methods called constructors. So let's modify the Point2D class again:

 

    class Point2D
    {
        public float X;
        public float Y;


        public Point2D(float x, float y)
        {
            X = x;
            Y = y;
        }

        public static Point2D operator +(Point2D p1, Point2D p2)
        {
            return new Point2D(p1.X + p2.X, p1.Y + p2.Y);
        }
    }

The strange Point2D method with no return type is what's called a constructor. It allows us to write the initialization logic in only one place, and then create our objects much more easily.

 

Notice that I also changed the "operator +" function to use this constructor directly instead of creating a Point2D and initializing the members separately: it now fits nicely on a single line.

 

We can go further and simplify all of our code. For instance, we can rewrite our addition example like so:

var p1 = new Point2D(10, 10);
var p2 = new Point2D(13, 15);


var p3 = p1 + p2;
Console.WriteLine("({0},{1}) + ({2},{3}) = ({4},{5})", p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y);
// prints "(10,10) + (13,15) = (23,25)"

Isn't this great? We can now work with 2D coordinates basically as easily as floats! We made our own data type!

 

And that's about all I'm willing to fit into a forum post. Hopefully you will have understood that:

 

 - classes allow you to define your own data types by compositing existing data types

 - classes can define operations on their members (like the "+" operator)

 - A class is the definition of the data type: what it is and what it does. An object is a concrete instance of that data type. There's only ever one definition per type (one class), but there can be as many objects of that type as you want.

 

Take your time. There's lots to learn about classes: public vs protected vs private members, static vs instance members, inheritance, virtual methods, etc. If you were following a good book with exercices and doing the exercices, you would likely not need to keep asking for basic tutorials here. I suggested the Yellow Book many times to you (which would explain all this much better than me), and I can only repeat my suggestion. You don't need to be stuck and wait for people to help you on forums. You can follow a good book like that and practice on your own.

 

As an exercise, try to extend the Point2D with more operations:

 

 - substraction

 - scalar addition/substraction

 - dot product

 - cross product

 - norm

 - etc.

 

Don't hesitate if you have more specific questions though.

 

Here's a full, compilable listing if you want to play with the example code:
 

using System;


class Point2D
{
    public float X;
    public float Y;

    public Point2D(float x, float y)
    {
        X = x;
        Y = y;
    }

    public static Point2D operator +(Point2D p1, Point2D p2)
    {
        return new Point2D(p1.X + p2.X, p1.Y + p2.Y);
    }
}

class Program
{
    static void Main()
    {
        var p1 = new Point2D(10, 10);
        var p2 = new Point2D(13, 15);

        var p3 = p1 + p2;

        Console.WriteLine("({0},{1}) + ({2},{3}) = ({4},{5})", p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y);
        // prints "(10,10) + (13,15) = (23,25)"
    }
}


#199 FloatingFatMan

FloatingFatMan

    Resident Fat Dude

  • Tech Issues Solved: 1
  • Joined: 23-August 04
  • Location: UK

Posted 24 January 2014 - 06:36

^ Nicely done mini-tutorial there. :)



#200 Jarrichvdv

Jarrichvdv

    Not all who wander are lost

  • Joined: 14-November 10
  • Location: Belgium, Asse
  • OS: Windows 8.1
  • Phone: Windows Phone 8 GDR3

Posted 24 January 2014 - 07:42

I don't think the OP understands a single word of the that 'mini-tutorial' but yea, nice one nonetheless.



#201 OP ShellBox

ShellBox

    Neowinian

  • Joined: 15-January 14
  • Location: Northamptonshire
  • OS: Windows 7 Ultimate 64 bit
  • Phone: HTC Wildfire

Posted 27 January 2014 - 14:13

I don't think the OP understands a single word of the that 'mini-tutorial' but yea, nice one nonetheless.

 

Unfortunately I didn't no. That's why I struggle reading "simple" programming fundamental books and tutorials. I just simply don't understand what a function is. I know what a variable is and how to do if else statements.



#202 +riahc3

riahc3

    Neowin's most indecisive member

  • Tech Issues Solved: 11
  • Joined: 09-April 03
  • Location: Spain
  • OS: Windows 7
  • Phone: HTC Desire Z

Posted 27 January 2014 - 14:19

Hello,

ShellBox, like I mentioned I dont think programming is for you if you dont understand what a function is at this point.

And the rest, this is a 14 page thread. By now....this should be closed/ended/dead.

Did you checkout daniweb?

Like I said, I wont post here anymore but it just keeps popping up on the minispy.

#203 Jarrichvdv

Jarrichvdv

    Not all who wander are lost

  • Joined: 14-November 10
  • Location: Belgium, Asse
  • OS: Windows 8.1
  • Phone: Windows Phone 8 GDR3

Posted 27 January 2014 - 14:28

A function is a set of statements, aimed to perform a single thing.

For example, an function to calculate something. If you put the code for a calculation in a function, you can just 'call' this function everywhere you want in your code without having to write all the actual code over and over again every time you need it.
 

public int CalculateLoan(int incomePerHour, int hoursWorked)
{
   int loan = incomePerHour * hoursWorked;
   return loan;
}


This EXTREMELY simplified function(or method, whatever you want to call it) needs to 2 variables to work. First, it needs an incomePerHour which is an Integer and a number of hours, also an Integer.

Than it goes to work. It calculates the loan and stores it in a seperate variable 'loan' and then returns loan. The function's work is done here. It took 2 parameters, performed a set of actions on it and returns the result.

The advantage is,every time you need to calculate a loan in your application, you can simply call CalculateLoan and give it the 2 variables it needs

 

int myLoan = CalculateLoan(12, 8);

// myLoan will contain 96

Now, this is extremely simplified and by no means complete but it just demonstrates the basic concept of a function. You also have functions that don't necessarily return something. They just DO something. You could also write that CalculateLoan function on 1 line instead of 2 (omitting the loan variable) but don't focus on that for now.

Now I'm not a teacher (and maybe it shows :/ ) but I hope it helped.



#204 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 27 January 2014 - 18:18

Unfortunately I didn't no. That's why I struggle reading "simple" programming fundamental books and tutorials. I just simply don't understand what a function is. I know what a variable is and how to do if else statements.

I did state that you need to understand what is a function first. That's why you shouldn't try to learn every concept in any order, and follow a good method like the one I've been recommending in every single post I made in this thread. 



#205 OP ShellBox

ShellBox

    Neowinian

  • Joined: 15-January 14
  • Location: Northamptonshire
  • OS: Windows 7 Ultimate 64 bit
  • Phone: HTC Wildfire

Posted 28 January 2014 - 09:41

I did state that you need to understand what is a function first. That's why you shouldn't try to learn every concept in any order, and follow a good method like the one I've been recommending in every single post I made in this thread. 

 

A lot of things that has been recommended to me in this thread is something I simply not understand like I have previously stated. 



#206 FloatingFatMan

FloatingFatMan

    Resident Fat Dude

  • Tech Issues Solved: 1
  • Joined: 23-August 04
  • Location: UK

Posted 28 January 2014 - 12:25



A lot of things that has been recommended to me in this thread is something I simply not understand like I have previously stated. 

 

Then you clearly are not actually reading the replies people give you.

 

Andre S has repeatedly suggested you look up the Yellow Book.  This book will explain all those basic terms you don't understand.  If you're not going to do as suggested, then give up. You're never going to be able to program if you don't understand the basic concepts, and if you're not going to take people's advise to GET that basic knowledge, what's the point?

 

Follow the link, download the PDF, and read it.  I've read it myself, it's a VERY good book that gives you all the information you need at the level you need it.  If you're not going to do that, you will never progress in your desire to be a programmer.





Click here to login or here to register to remove this ad, it's free!