PPSz
PPSz
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
like the difference between _maxSpeed and maxSpeed from my example. It's completely irrevelant to know exactly what's happening. Visual Studio won't let you use the wrong one
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
also I know it isn't always easy, but some stuff you don't have to understand completely to use them, just brute force stuff, at some point you'll figure out
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
programming is confusing until it clicks, I suggest that video. I think it does a better job in explaining basics
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
_maxSpeed is inside car1 and car2 objects from my example
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
I learned C# from older version of this one: https://www.youtube.com/watch?v=0QUgvfuKvWU it's a little older but basics are still the same
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
I'm not sure if it's allowed to post a link but I'd suggests different tutorial
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
fields doesn't have () at the end and methods have () a the end
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
ok, maybe to make things less confusing as there are different words for same thing: field, variable, parameter, argument are almost the same thing but they are called differently in different places. Just a container to store value of certain type type, class, blueprint is more or less also the same method, function, action is just a block of code inside { } that does some logic
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
yes, that's good idea to try yourself how the program will behave if you make that change
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
since it's declared:
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
there are two different variables. I assign value of maxSpeed to _maxSpeed
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
field is a variable in class:
class Car
{
private int _maxSpeed; // < this field
...
class Car
{
private int _maxSpeed; // < this field
...
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
_maxSpeed is a field in class, maxSpeed is a paremeter
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
here's an example:
class Car
{
private int _maxSpeed;

public Car(int maxSpeed)
{
_maxSpeed = maxSpeed;
}

public string Drive()
{
return "I drive but not faster than " + _maxSpeed;
}
}

public class MainClass
{

private static void Main()
{
Car car1 = new Car(100);
Car car2 = new Car(120);

var result1 = car1.Drive();
var result2 = car2.Drive();

Console.WriteLine(result1);
Console.WriteLine(result2);
}
}
class Car
{
private int _maxSpeed;

public Car(int maxSpeed)
{
_maxSpeed = maxSpeed;
}

public string Drive()
{
return "I drive but not faster than " + _maxSpeed;
}
}

public class MainClass
{

private static void Main()
{
Car car1 = new Car(100);
Car car2 = new Car(120);

var result1 = car1.Drive();
var result2 = car2.Drive();

Console.WriteLine(result1);
Console.WriteLine(result2);
}
}
class Car has one field (variable) that's maxSpeed, which is unique for every car we create in this example. If you invoke Drive() method on car1 or car2 we get different results. What you can get confused on when comparing this to Random class is Next() method in random class returns a random number instead of predefined variable (that's why the class is called Random). You might ask why you need to create an instance of this object (line Random numberGenerator = new Random()), it's because you can give this instance a seed for example: Random numberGenerator = new Random(12345). That means if you do:
Random numberGenerator = new Random(12345)
int num01 = numberGenerator.Next(1,11);
int num02 = numberGenerator.Next(1,11);
int num03 = numberGenerator.Next(1,11);
Random numberGenerator = new Random(12345)
int num01 = numberGenerator.Next(1,11);
int num02 = numberGenerator.Next(1,11);
int num03 = numberGenerator.Next(1,11);
you'll get three random numbers but they will be always the same between program runs. Computers just calculate list of random numbers based on the seed (if you do new Random() the seed is also random). It's like seed in Minecraft, where worlds are randomly generated but you can give the game a seed and you'll get always the same world
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
every other method you have to call yourself
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
Main() is unique method that's called on program start
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
Main is a method
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
for example Public static void Main () is a method inside Class MainClass in your code
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
in C#
204 replies
CC#
Created by Soinagie on 3/3/2023 in #help
✅ confused about classes
you can only define a method inside a class
204 replies