✅ confused about classes
I'm watching brackeys and he's talking about referencing a class but it doesn't make sense to me, he didn't call it and even if he did, why? Is random number generator some sort of class? Like I don't get it at all
138 Replies
Shouldn't then both num01 and num 02 be the same since they're from the same
new Random
?Random is a predefined .Net class. You create instance of that class here
Random numberGenerator = new Random();
. num01
and num02
come from .Next(1,11)
method. This method rolls new number every time you invoke it
class
keyword is used when you declare your own classBut numberGenerator isn't a method
numberGenerator
is an instance of Random
class
so it's an objectBut it has the same name
So its the same
So num01 and num02 should be the same
no, because the number comes from
.Next()
method, not from object itselfHow can a method be after
.
?
And this method wasn't definedthis method is defined in .Net
Idk what's that
Then is
.Next()
or Random
a method?when you have object, you call its methods by using
.
.Next()
is a method. Random
is classWdym by 'its methods'?
I mean objects have methods and to call them you use
.
How can objects have methods?
Methods are actions
That you call
By saying them
objects consist of fields (variables) and methods that you can perform on them
Object is a thing
Right?
And method is an action
yes, but objects can perform actions
Conputer performs actions
Not objects
Sorry it doesn't makes sense
Can't you just call a method?
you can only define a method inside a class
in C#
for example
Public static void Main ()
is a method inside Class MainClass
in your codeThen Main is a method or a class?
Main is a method
But it doesn't get called
Main() is unique method that's called on program start
every other method you have to call yourself
You can imagine that your computer calls the method when your program starts
Ok
But objects with methods still don't make sense to me
Just call a method
You don't need object for that
You call a method on an object
(unless the method is static)
Then just mention object in method
And what you want with it
well, that's kinda already what you do
Where?
If you have a method
void Bar()
inside a class Foo
, that's essentially the same as static void Bar(Foo this)
In instance methods, the this
parameter is just implictI'll be honest I have no idea what that means
Anyway, you call methods on objects. That's just what it is.
So it
object.method
?
And this method changes object?
And only this object?object.Method()
Main
is a static method which means that you don't have to have an object to call it on, but Next
is an instance method which you have to call on an object.Idk what static method is
it has access to everything inside that method, but it could very well change other things as well.
well read the whole message, Thinker explains it :p
So Main is the only static method?
you can make your own
but Main is always static
But it doesn't get called
its the entrypoint to your application
it gets called by dotnet
you dont call it yourself
Whats dotnet?
the runtime
What runtime
okay, the computer
when you say "run this program", it runs the Main method
thats all you need to know
dont worry about all the details. Main is a special method. thats just the way it is
So static methods are normal methods that you call by themself
And they do something
and instance methods are things that I dont understand
there's nothing such as a "normal" method
the important difference between static and non-static is that statics dont belong to an object, they belong to a class
Everything belongs to a class
class
es are a kind of "blueprints" from which objects are created. In the class
, you can define the properties and actions (methods) of objects that will be created according to this "blueprint"But there's main class
It's not a blueprint
Main
isnt a class, its a method.Fine
MainClass
thenCan't I just call actions when I need to?
But
Random
was created in MainClass
A variable of type
Random
was created in the method
in your code sample above, MainClass
contains only Main
So
Random
is a type like int
?yes
Look at this code, for example
So its a type and a class at the same time?
we have Main up top, the special entrypoint method.
yes, as we covered in your infamous "what is an object" thread: all classes are types, but not all types are classes
There is a predefined somewhere, this is a "blueprint" of number generator.
But you can't generate numbers by using "blueprint" of number generator, you first need to create a number generator using this "blueprint", this is what blueprint class
Random numberGenerator = new Random()
does
After that, numberGenerator
is the actual object of Random
Pls don't do crossed out text as it only confuses me more
Okay, I meant that the word "blueprint" is here only for better understanding. It is correct to call it a
class
How is
numberGenerator
a class? I thought it was an objecthere's an example:
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:
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 worldnumberGenerator is an object of class Random
So its an object, not class
yes. but that object is of the
Type
Random
there is even a .GetType()
method on all objects, that will give back the typeWhy did you do
maxspeed
2 times but with '_'?_maxSpeed
is a field in class, maxSpeed
is a paremeterWhat field?
field is a variable in class:
"field" is what we call "object-level variables"
This is getting too much
So
_maxSpeed
and maxSpeed
are the same since they're both variables?You just got a lot of information here. You need to read something to understand it consistently
Then why did you call it twice if they're the same?
they are different kinds of variables.
there are two different variables. I assign value of maxSpeed to _maxSpeed
the one in the constructor only exists within that scope. it doesnt exist outside that method
so we store the value from
maxSpeed
in _maxSpeed
, so that it staysCan't you just use
maxSpeed
outside?since it's declared:
Just don't call it private
go ahead and try
programming is best learnt by doing
so go ahead
yes, that's good idea to try yourself how the program will behave if you make that change
The name 'maxSpeed' does not exist in the current context
But it exists
nope, it only exists in the method itself
How can it not exist?
it does not.
after that point, it's gone
But I set it to public
Plus
_maxSpeed
shouldn't exist then eithercan you show your code again, because it's not clear what you are referring to
why not? they are declared at different places
If there's
=
{
class Program
{
class Car
{
public 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);
}
}
}
}
maxSpeed
and _maxSpeed
are two different thingsThere's
=
They're the same
Or should be=
doesn't mean equality in programming. It means "copy value from right to left"so
maxSpeed
is a type
and _maxSpeed
is a methodnot at all
maxSpeed
is a parameter/variable, _maxSpeed
is a fieldI still don't know what a field is
it's essentially a variable in a class
so a variable
well... yes
so both are variables?
different kinds of variable thou
one is a parameter, the other is a field
they are both variables
yeah sorry I don't see a difference
¯\_(ツ)_/¯
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_maxSpeed
is a method
right?no
but its called in a class with nothing else
so its a static method
its not a method. its not static.
sorry but this didn't really help, appriciate it though!
then how am I supposed to know if it's a field or a method?
fields doesn't have
()
at the end and methods have ()
a the endand how are fields different for variables?
its all a matter of scope
what scope?
a field is available everywhere in the class. methods can use the value as they please.
a variable is "local"
this is what I'm getting from watching a begginer's tutorial
it only exists in the method it was declared, and only after it was declared. sometimes even in a smaller part of a method
and importantly, a field remembers its value as long as the object (the class instance, remember?) is active
I'm not sure if it's allowed to post a link but I'd suggests different tutorial
you can post it here probably
but it's only in a class
Car
, not MainClass
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
Microsoft Developer
YouTube
C# Fundamentals for Beginners
Classic Throwback Series with Bob Tabor | Originally published in 2019
Install the latest version of Visual Studio at https://visualstudio.microsoft.com/?WT_mc_id=dotnet-58793-cxa and follow along at home too.
C# is a powerful and widely used programming language that you can use to make websites, games, mobile apps, desktop apps and more with ...
well yes, only in the class the field belongs to, of course
then
_maxSpeed
shouldn't be able to jump to MainClass
it doesnt
or maybe
maxSpeed
I don't even know anymore_maxSpeed
is inside car1
and car2
objects from my exampleI think I'm completly lost
let's just call it quits
I really appriciate your help though!
programming is confusing until it clicks, I suggest that video. I think it does a better job in explaining basics
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
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 oneand most importantly, you got to get your hands dirty. write code.
you can use a static class which doesn't need to be instantiated to use its methods
otherwise you'd need to access the methods on an instance of the class
I have no idea what you just said but thanks!
$close
Use the
/close
command to mark a forum thread as answered^ please do this if you feel finished here