• 0

I'm too stupid to understand C#


Question

I started reading hfcsharp a few days back; everything was going smoothly until I reached my lab program "A day at the Race".

I read through the problem in the book, and did not know where to start from, so I went directly to the hfc# website page and downloaded the solution. The solution was perfect, with no errors; but I was unable to understand some lines in the code. The book did a poor job of explaining this, to me, and I have read through the book twice looking for a solution. I found nothing reliable to compare and understand from, though I did look through some questions about the same lab program; most were too complicated for me to understand. The question list was pretty big, so i made a PDF pointing to which parts I didn't understand, so that you guy can look into it.

I also read threw some similar problem relating to this topic in stack overflow and all the keywords i didn't understand in MSDN docs ,none of them helped in getting the answer that i expected, but instead they  raised more questions.  

I used to code using assemble  language and vhdl before but when never have i coded anything using c++, c or c# i find this very hard to understand it's nothing like assemble language or vhdl please help i'm really interested in learning C# i want to peruse my career in it.

Link to comment
Share on other sites

Recommended Posts

  • 0
13 hours ago, Ch33f said:

Here i am trying to learn C# and every where i go most of the programmers recommend me to start with C or learn C++ instead , also in some other forums programmers say that C# makes people stupid; because of the visual studio software that points out every mistake  a coder makes in his program and requires less thinking when coding

It requires less thinking about things that don't matter in most languages except for C and C++. Like managing header files and dealing with linker errors. It requires exactly the same amount of thinking about things that do matter in general, like data structure and algorithms, good program architecture, object-oriented and functional design, etc. Some C and C++ programmers tend to have a view of the world very centered around their favorite programming language. No need to listen to them.

  • Like 2
Link to comment
Share on other sites

  • 0

Random is a built in class, hold your mouse over "Random" and you will see "System.Something.Random".

 

String, Int32 and other types are also classes actually.

 

Every variable type is actually a class. The whole idea of classes is that you can make your own variable type, Dog as example to hold information that you want a Dog to have. This is called object oriented programming, every variable can be seen as an object of a certain type, a Dog, int(Int32), string(String), bool(Bool) etc.

 

System types are lowercase like "string" and are referring to the class (String).

 

Why is this usefull?

 

Imagine storing 2 dogs,

 

string dogName1 = "Foo";

string dogName2 = "Bar";

int dogAge1 = 6;

int dogAge2 = 12;

 

It's clear that this is a nightmarish way to realise a list of dogs...

 

So lets use a list:

 

List<string> dogNames = new List<string>();

List<int> dogAges = new List<int>();

dogNames.Add("Foo");

dogNames.Add("Bar");

dogAges.Add(6);

dogAges.Add(12);

 

Ok this seems a bit more usefull and easier to extend.

 

Now let's stop creating a list for every dog detail and use a class instead:

 

class Dog {

    public string Name;

    public int Age;

}

 

List<Dog> dogs = new List<Dog>();

dogs.Add(new Dog {

    Name = "Foo",

    Age = 6

});

dogs.Add(new Dog {

    Name = "Bar",

    Age = 12

});

 

Now we got actual dog objects and just a single list of "objects" to work with.

 

Classes can also contain methods like Walk(12);

Which by example changes the variable Steps in a Dog object by increasing it with 12.

 

OOP can be very useful is big programs, it makes it easier to distinguish between different variables, there is no longer any need for variables like "dogNames".

Link to comment
Share on other sites

  • 0
C# is nothing like VB.net though. The shared methods between the two is .net specific and not specific to the language.  You shoulnt be learning just one language at any rate because there are different use cases and platforms. Limiting yourself to just cross-platform languages doesn't make a lot of sense if it doesn't fit the use case. 

I suppose so, I'm not that great at programming. I struggle to find where to start, lol. Main, great, lol.

Sent from my SM-N910V using Tapatalk

Link to comment
Share on other sites

  • 0
17 hours ago, BinaryData said:


I suppose so, I'm not that great at programming. I struggle to find where to start, lol. Main, great, lol.

Sent from my SM-N910V using Tapatalk
 

Most programs start by looking at the data.

Start with realising a data model, what data do you wanna get and what are the relations between different parts of data?

 

Next up is the way you're gonna get the data. Is it gonna be one class that gets and stores all data? Or multiple classes, like a User, Car and Dog class? Or maybe extend that with a repository pattern? Or maybe use a different pattern?

 

The repository pattern is a way to keep the logic and data parts seperated:

 

DogRepository class has a method Get(int id) as example that calls a context class it's Get(int id) method. This way there can be multiple contexts like a DogSQLContext or a DogXMLContext. The contexts contains the code to get the data and the repository contains the logic that's done with that data.

 

There are multiple programming patterns, MVC is also a patterns as example that can be combined with a repository pattern as example.

 

So the most important is to get the data design right after that think classes design that makes sense, make use of existing patterns, don't try to invent your own pattern if there isn't a reason to do so, it will only make it harder for others to understand your code and the logic behind it.

 

If you're creating an application and you're unsure if your class and data design is good consider creating a topic and ask for suggestions, it's way better to already find problems in the design before you start coding then find problems in the design later on and find out you have to rewrite whole parts.

 

Starting with a good idea of how you're going to make a program is halve the work, I learned it the hard way :p

 

 

P.S I'm writing way too long posts these days D:

Link to comment
Share on other sites

  • 0
On 1/21/2017 at 6:00 PM, Ch33f said:

 

both of your answers helped me carve a path into  understanding on how the program runs, so i dug deeper looking through other research material on this, point and Next yet again ran in to some doubts.

 

1)  Under point there is a line point p =  MypictureBox.location  can this be represented as MypictureBox(location) or MypictureBox.location()

2)  Here MypictureBox is Controlpoint and location is a variable so i have seen in previous programs that Class.method() can be used in a statement to declare a method or while initializing components and as in above code we see Controlpoint. Variable  being used,what other types can be used,can variable.variable be used ?

3) i some code i have seen lines like Size buttonSize = (Size)startPoint; here starting point being the variable like point p being used in this code see here how teh bracket is being sued before a variable i have see this type of line behaviour  while casting an variable  from short to int, my question is what is happening here, can brakets be decalrd befor a variable or a class or a method ?

4)Under form1 (page 2) up on removing Application.DoEvent() line had no effect on the code when compiled same goes for making race.Enabled = false to true

 

When ever i compile my code and press Race! button the execution is instant and the winning dog is revealed right away, is there some line of code that i could ad to modify the animation of picture box go slower ?

 

I will try to answer some of these, but first you might want to review my C# OOP Primer, its a powerpoint presentation i created to teach C# and OOP.  (Not comprehensive of every concept, but beginner-like enough to get started)    

https://drive.google.com/open?id=0BzXmEDpVnkJXTVZsYjM1Q3Fsb2s

 

1)MypictureBox(location)  No.  This appears to be passing in a parameter called "location" to a method or constructor called MypictureBox.

MypictureBox.location()  Possibly.  if the member MypictureBox.Location has a getter method called location(), then this method will return the value of MypictureBox.Location.

 

2)MyPictureBox is an object of class PictureBox.  Location is a member variable of that class.  Class.Method() is actually not correct... It is Object.Method(), and that is how the method is called (ran) for that specific object.  Each object will have its own copy of that method.  

however there is a specific exception to that rule.  Class.method() can be used to call a method of a Static Class, but that is a very specific and complex exception, and one which I will not explain here until you understand the basics.

 

Object.Method() where Method is the Class Name is a specific method called a constructor, and its only use is to initialize the member variable of the class.

The variables used, must always match the defined list of parameters the method is expecting.  However there may be multiple methods with the same name, each requiring different parameters.  However each must be explicitly defined as to what parameters the method requires.

p = Point() vs  p = new Point(int, int)   vs  p = new Point(float, float)  would be an example of this.

 

3)This is a specific instance of something called Typecasting... which is a special form of converting from 1 type to another, and must only be used in certain circumstances.

 

If you consider the class point and the class circle, I can demonstrate this.


 

Class Point()
{
//only showing the relevant details, not the whole class
	protected int X;
	protected int Y;
}


Class Circle() : Point()
{
//Circle inherits point
//only showing the relevant details, not the whole class
	protected Diameter D;
}



Circle C = new Circle();
Point P = (Point)C;


C will have members X,Y, and D
P will only have X and Y
P's X and Y will be the same X and Y as the one in C.

This is an advanced concept of Inheritance.  Point is the base class, Circle is the derived class.  Typecasting allows you to get the base piece of a derived class.  Since most of the common components used in C# is a hierarchy of base and derived components, this will often be used to get a pointer to a base object.    This behavior is only used when accessing the inner object.  

I did not see this anywhere in the PDF example however.

 

Not to be confused with guys[GuyNumber].UpdateLabels();

guys[GuyNumber]  is an array called guys.  [GuyNumber] is the index of the specific cell in the array you are accessing.  Each Guy in the Array has its own method .UpdateLabels();

 

 

 

 

4)I can't say what race.Enabled does or does not do, as we can't see where is it declared or defined (are we missing a page?)

Application.DoEvents() simple means for the application to accept input (button presses) and handle each event appropriately (Click -> do something) until the loop is done.

https://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents(v=vs.110).aspx

Link to comment
Share on other sites

This topic is now closed to further replies.