C
C#2y ago
Soinagie

✅ confused about classes

Class MainClass
{
Public static void Main ()
{
Random numberGenerator = new Random();
int num01 = numberGenerator.Next(1,11);
int num02 = numberGenerator.Next(1,11);
Console.WriteLine (num01 "and" num02);
Console.ReadKey();
}
}
Class MainClass
{
Public static void Main ()
{
Random numberGenerator = new Random();
int num01 = numberGenerator.Next(1,11);
int num02 = numberGenerator.Next(1,11);
Console.WriteLine (num01 "and" num02);
Console.ReadKey();
}
}
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
class
class
and even if he did, why? Is random number generator some sort of class? Like I don't get it at all
138 Replies
Soinagie
SoinagieOP2y ago
Shouldn't then both num01 and num 02 be the same since they're from the same new Random?
PPSz
PPSz2y ago
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 class
Soinagie
SoinagieOP2y ago
But numberGenerator isn't a method
PPSz
PPSz2y ago
numberGenerator is an instance of Random class so it's an object
Soinagie
SoinagieOP2y ago
But it has the same name So its the same So num01 and num02 should be the same
PPSz
PPSz2y ago
no, because the number comes from .Next() method, not from object itself
Soinagie
SoinagieOP2y ago
How can a method be after .? And this method wasn't defined
PPSz
PPSz2y ago
this method is defined in .Net
Soinagie
SoinagieOP2y ago
Idk what's that Then is .Next() or Random a method?
PPSz
PPSz2y ago
when you have object, you call its methods by using . .Next() is a method. Random is class
Soinagie
SoinagieOP2y ago
Wdym by 'its methods'?
PPSz
PPSz2y ago
I mean objects have methods and to call them you use .
Soinagie
SoinagieOP2y ago
How can objects have methods? Methods are actions That you call By saying them
PPSz
PPSz2y ago
objects consist of fields (variables) and methods that you can perform on them
Soinagie
SoinagieOP2y ago
Object is a thing Right? And method is an action
PPSz
PPSz2y ago
yes, but objects can perform actions
Soinagie
SoinagieOP2y ago
Conputer performs actions Not objects Sorry it doesn't makes sense Can't you just call a method?
PPSz
PPSz2y ago
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 code
Soinagie
SoinagieOP2y ago
Then Main is a method or a class?
PPSz
PPSz2y ago
Main is a method
Soinagie
SoinagieOP2y ago
But it doesn't get called
PPSz
PPSz2y ago
Main() is unique method that's called on program start every other method you have to call yourself
Thinker
Thinker2y ago
You can imagine that your computer calls the method when your program starts
Soinagie
SoinagieOP2y ago
Ok But objects with methods still don't make sense to me Just call a method You don't need object for that
Thinker
Thinker2y ago
You call a method on an object (unless the method is static)
Soinagie
SoinagieOP2y ago
Then just mention object in method And what you want with it
Thinker
Thinker2y ago
well, that's kinda already what you do
Soinagie
SoinagieOP2y ago
Where?
Thinker
Thinker2y ago
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 implict
Soinagie
SoinagieOP2y ago
I'll be honest I have no idea what that means
Thinker
Thinker2y ago
Anyway, you call methods on objects. That's just what it is.
Soinagie
SoinagieOP2y ago
So it object.method? And this method changes object? And only this object?
Pobiega
Pobiega2y ago
object.Method()
Thinker
Thinker2y ago
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.
Soinagie
SoinagieOP2y ago
Idk what static method is
Pobiega
Pobiega2y ago
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
Soinagie
SoinagieOP2y ago
So Main is the only static method?
Pobiega
Pobiega2y ago
you can make your own but Main is always static
Soinagie
SoinagieOP2y ago
But it doesn't get called
Pobiega
Pobiega2y ago
its the entrypoint to your application it gets called by dotnet you dont call it yourself
Soinagie
SoinagieOP2y ago
Whats dotnet?
Pobiega
Pobiega2y ago
the runtime
Soinagie
SoinagieOP2y ago
What runtime
Pobiega
Pobiega2y ago
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
Soinagie
SoinagieOP2y ago
So static methods are normal methods that you call by themself And they do something and instance methods are things that I dont understand
Thinker
Thinker2y ago
there's nothing such as a "normal" method
Pobiega
Pobiega2y ago
the important difference between static and non-static is that statics dont belong to an object, they belong to a class
Soinagie
SoinagieOP2y ago
Everything belongs to a class
nukleer bomb
nukleer bomb2y ago
classes 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"
Soinagie
SoinagieOP2y ago
But there's main class It's not a blueprint
Pobiega
Pobiega2y ago
Main isnt a class, its a method.
Soinagie
SoinagieOP2y ago
Fine MainClass then
Pobiega
Pobiega2y ago
public static class Program
{
public static void Main()
{
Console.WriteLine("hello");
}
}
public static class Program
{
public static void Main()
{
Console.WriteLine("hello");
}
}
Soinagie
SoinagieOP2y ago
Can't I just call actions when I need to? But Random was created in MainClass
Pobiega
Pobiega2y ago
A variable of type Random was created in the method in your code sample above, MainClass contains only Main
Soinagie
SoinagieOP2y ago
So Random is a type like int?
Pobiega
Pobiega2y ago
yes Look at this code, for example
public static class Program
{
public static void Main()
{
var inc = new Incrementer();
Console.WriteLine(inc.Increment());
Console.WriteLine(inc.Increment());
}
}

public class Incrementer
{
private int _count;

public int Increment()
{
return ++_count;
}
}
public static class Program
{
public static void Main()
{
var inc = new Incrementer();
Console.WriteLine(inc.Increment());
Console.WriteLine(inc.Increment());
}
}

public class Incrementer
{
private int _count;

public int Increment()
{
return ++_count;
}
}
Soinagie
SoinagieOP2y ago
So its a type and a class at the same time?
Pobiega
Pobiega2y ago
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
nukleer bomb
nukleer bomb2y ago
There is a
class Random
{

}
class Random
{

}
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 Random numberGenerator = new Random() does After that, numberGenerator is the actual object of blueprint class Random
Soinagie
SoinagieOP2y ago
Pls don't do crossed out text as it only confuses me more
nukleer bomb
nukleer bomb2y ago
Okay, I meant that the word "blueprint" is here only for better understanding. It is correct to call it a class
Soinagie
SoinagieOP2y ago
How is numberGenerator a class? I thought it was an object
PPSz
PPSz2y ago
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
nukleer bomb
nukleer bomb2y ago
numberGenerator is an object of class Random
Soinagie
SoinagieOP2y ago
So its an object, not class
Pobiega
Pobiega2y ago
yes. but that object is of the Type Random there is even a .GetType() method on all objects, that will give back the type
Soinagie
SoinagieOP2y ago
Why did you do maxspeed 2 times but with '_'?
PPSz
PPSz2y ago
_maxSpeed is a field in class, maxSpeed is a paremeter
Soinagie
SoinagieOP2y ago
What field?
PPSz
PPSz2y ago
field is a variable in class:
class Car
{
private int _maxSpeed; // < this field
...
class Car
{
private int _maxSpeed; // < this field
...
Pobiega
Pobiega2y ago
"field" is what we call "object-level variables"
Soinagie
SoinagieOP2y ago
This is getting too much So _maxSpeed and maxSpeed are the same since they're both variables?
nukleer bomb
nukleer bomb2y ago
You just got a lot of information here. You need to read something to understand it consistently
Soinagie
SoinagieOP2y ago
Then why did you call it twice if they're the same?
Pobiega
Pobiega2y ago
they are different kinds of variables.
PPSz
PPSz2y ago
there are two different variables. I assign value of maxSpeed to _maxSpeed
Pobiega
Pobiega2y ago
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 stays
Soinagie
SoinagieOP2y ago
Can't you just use maxSpeed outside?
PPSz
PPSz2y ago
since it's declared:
Soinagie
SoinagieOP2y ago
Just don't call it private
Pobiega
Pobiega2y ago
go ahead and try programming is best learnt by doing so go ahead
PPSz
PPSz2y ago
yes, that's good idea to try yourself how the program will behave if you make that change
Soinagie
SoinagieOP2y ago
The name 'maxSpeed' does not exist in the current context But it exists
Thinker
Thinker2y ago
nope, it only exists in the method itself
Soinagie
SoinagieOP2y ago
How can it not exist?
Pobiega
Pobiega2y ago
it does not.
Thinker
Thinker2y ago
after that point, it's gone
Soinagie
SoinagieOP2y ago
But I set it to public Plus _maxSpeed shouldn't exist then either
Thinker
Thinker2y ago
can you show your code again, because it's not clear what you are referring to
Pobiega
Pobiega2y ago
why not? they are declared at different places
Soinagie
SoinagieOP2y ago
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); } } } }
nukleer bomb
nukleer bomb2y ago
maxSpeed and _maxSpeed are two different things
Soinagie
SoinagieOP2y ago
There's = They're the same Or should be
nukleer bomb
nukleer bomb2y ago
= doesn't mean equality in programming. It means "copy value from right to left"
Soinagie
SoinagieOP2y ago
so maxSpeed is a type and _maxSpeed is a method
Pobiega
Pobiega2y ago
not at all
Thinker
Thinker2y ago
maxSpeed is a parameter/variable, _maxSpeed is a field
Soinagie
SoinagieOP2y ago
I still don't know what a field is
Thinker
Thinker2y ago
it's essentially a variable in a class
Soinagie
SoinagieOP2y ago
so a variable
Thinker
Thinker2y ago
well... yes
Soinagie
SoinagieOP2y ago
so both are variables?
Pobiega
Pobiega2y ago
different kinds of variable thou one is a parameter, the other is a field they are both variables
Soinagie
SoinagieOP2y ago
yeah sorry I don't see a difference
Pobiega
Pobiega2y ago
¯\_(ツ)_/¯
PPSz
PPSz2y ago
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
Soinagie
SoinagieOP2y ago
_maxSpeed is a method right?
Thinker
Thinker2y ago
no
Soinagie
SoinagieOP2y ago
but its called in a class with nothing else so its a static method
Pobiega
Pobiega2y ago
its not a method. its not static.
Soinagie
SoinagieOP2y ago
sorry but this didn't really help, appriciate it though!
Thinker
Thinker2y ago
class Class // This is a class
{
private int _field; // This is a field (variable in a class)

// This is a method
public void Method(int parameter /*this is a parameter*/)
{
// This is a variable
int variable = parameter;
_field = variable;
}
}
class Class // This is a class
{
private int _field; // This is a field (variable in a class)

// This is a method
public void Method(int parameter /*this is a parameter*/)
{
// This is a variable
int variable = parameter;
_field = variable;
}
}
// This is a variable containing an object
Class object = new Class();
object.Method(1); // This is calling the method Method
// This is a variable containing an object
Class object = new Class();
object.Method(1); // This is calling the method Method
Soinagie
SoinagieOP2y ago
then how am I supposed to know if it's a field or a method?
PPSz
PPSz2y ago
fields doesn't have () at the end and methods have () a the end
Thinker
Thinker2y ago
class Class
{
// If it's in here, it's a field

public void Method()
{
// If it's in here, it's a variable
}
}
class Class
{
// If it's in here, it's a field

public void Method()
{
// If it's in here, it's a variable
}
}
Soinagie
SoinagieOP2y ago
and how are fields different for variables?
Pobiega
Pobiega2y ago
its all a matter of scope
Soinagie
SoinagieOP2y ago
what scope?
Pobiega
Pobiega2y ago
a field is available everywhere in the class. methods can use the value as they please. a variable is "local"
Soinagie
SoinagieOP2y ago
this is what I'm getting from watching a begginer's tutorial
Pobiega
Pobiega2y ago
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
PPSz
PPSz2y ago
I'm not sure if it's allowed to post a link but I'd suggests different tutorial
Soinagie
SoinagieOP2y ago
you can post it here probably but it's only in a class Car, not MainClass
PPSz
PPSz2y ago
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 ...
Pobiega
Pobiega2y ago
well yes, only in the class the field belongs to, of course
Soinagie
SoinagieOP2y ago
then _maxSpeed shouldn't be able to jump to MainClass
Pobiega
Pobiega2y ago
it doesnt
Soinagie
SoinagieOP2y ago
or maybe maxSpeed I don't even know anymore
PPSz
PPSz2y ago
_maxSpeed is inside car1 and car2 objects from my example
Soinagie
SoinagieOP2y ago
I think I'm completly lost let's just call it quits I really appriciate your help though!
PPSz
PPSz2y ago
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 one
Pobiega
Pobiega2y ago
and most importantly, you got to get your hands dirty. write code.
blinkbat
blinkbat2y ago
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
Soinagie
SoinagieOP2y ago
I have no idea what you just said but thanks!
Thinker
Thinker2y ago
$close
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
Thinker
Thinker2y ago
^ please do this if you feel finished here
Want results from more Discord servers?
Add your server