C
C#17mo 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
Soinagie17mo ago
Shouldn't then both num01 and num 02 be the same since they're from the same new Random?
PPSz
PPSz17mo 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
Soinagie17mo ago
But numberGenerator isn't a method
PPSz
PPSz17mo ago
numberGenerator is an instance of Random class so it's an object
Soinagie
Soinagie17mo ago
But it has the same name So its the same So num01 and num02 should be the same
PPSz
PPSz17mo ago
no, because the number comes from .Next() method, not from object itself
Soinagie
Soinagie17mo ago
How can a method be after .? And this method wasn't defined
PPSz
PPSz17mo ago
this method is defined in .Net
Soinagie
Soinagie17mo ago
Idk what's that Then is .Next() or Random a method?
PPSz
PPSz17mo ago
when you have object, you call its methods by using . .Next() is a method. Random is class
Soinagie
Soinagie17mo ago
Wdym by 'its methods'?
PPSz
PPSz17mo ago
I mean objects have methods and to call them you use .
Soinagie
Soinagie17mo ago
How can objects have methods? Methods are actions That you call By saying them
PPSz
PPSz17mo ago
objects consist of fields (variables) and methods that you can perform on them
Soinagie
Soinagie17mo ago
Object is a thing Right? And method is an action
PPSz
PPSz17mo ago
yes, but objects can perform actions
Soinagie
Soinagie17mo ago
Conputer performs actions Not objects Sorry it doesn't makes sense Can't you just call a method?
PPSz
PPSz17mo 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
Soinagie17mo ago
Then Main is a method or a class?
PPSz
PPSz17mo ago
Main is a method
Soinagie
Soinagie17mo ago
But it doesn't get called
PPSz
PPSz17mo ago
Main() is unique method that's called on program start every other method you have to call yourself
Thinker
Thinker17mo ago
You can imagine that your computer calls the method when your program starts
Soinagie
Soinagie17mo 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
Thinker17mo ago
You call a method on an object (unless the method is static)
Soinagie
Soinagie17mo ago
Then just mention object in method And what you want with it
Thinker
Thinker17mo ago
well, that's kinda already what you do
Soinagie
Soinagie17mo ago
Where?
Thinker
Thinker17mo 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
Soinagie17mo ago
I'll be honest I have no idea what that means
Thinker
Thinker17mo ago
Anyway, you call methods on objects. That's just what it is.
Soinagie
Soinagie17mo ago
So it object.method? And this method changes object? And only this object?
Pobiega
Pobiega17mo ago
object.Method()
Thinker
Thinker17mo 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
Soinagie17mo ago
Idk what static method is
Pobiega
Pobiega17mo 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
Soinagie17mo ago
So Main is the only static method?
Pobiega
Pobiega17mo ago
you can make your own but Main is always static
Soinagie
Soinagie17mo ago
But it doesn't get called
Pobiega
Pobiega17mo ago
its the entrypoint to your application it gets called by dotnet you dont call it yourself
Soinagie
Soinagie17mo ago
Whats dotnet?
Pobiega
Pobiega17mo ago
the runtime
Soinagie
Soinagie17mo ago
What runtime
Pobiega
Pobiega17mo 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
Soinagie17mo 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
Thinker17mo ago
there's nothing such as a "normal" method
Pobiega
Pobiega17mo ago
the important difference between static and non-static is that statics dont belong to an object, they belong to a class
Soinagie
Soinagie17mo ago
Everything belongs to a class
nukleer bomb
nukleer bomb17mo 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
Soinagie17mo ago
But there's main class It's not a blueprint
Pobiega
Pobiega17mo ago
Main isnt a class, its a method.
Soinagie
Soinagie17mo ago
Fine MainClass then
Pobiega
Pobiega17mo ago
public static class Program
{
public static void Main()
{
Console.WriteLine("hello");
}
}
public static class Program
{
public static void Main()
{
Console.WriteLine("hello");
}
}
Soinagie
Soinagie17mo ago
Can't I just call actions when I need to? But Random was created in MainClass
Pobiega
Pobiega17mo ago
A variable of type Random was created in the method in your code sample above, MainClass contains only Main
Soinagie
Soinagie17mo ago
So Random is a type like int?
Pobiega
Pobiega17mo 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
Soinagie17mo ago
So its a type and a class at the same time?
Pobiega
Pobiega17mo 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 bomb17mo 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
Soinagie17mo ago
Pls don't do crossed out text as it only confuses me more
nukleer bomb
nukleer bomb17mo ago
Okay, I meant that the word "blueprint" is here only for better understanding. It is correct to call it a class
Soinagie
Soinagie17mo ago
How is numberGenerator a class? I thought it was an object
PPSz
PPSz17mo 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 bomb17mo ago
numberGenerator is an object of class Random
Soinagie
Soinagie17mo ago
So its an object, not class
Pobiega
Pobiega17mo 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
Soinagie17mo ago
Why did you do maxspeed 2 times but with '_'?
PPSz
PPSz17mo ago
_maxSpeed is a field in class, maxSpeed is a paremeter
Soinagie
Soinagie17mo ago
What field?
PPSz
PPSz17mo ago
field is a variable in class:
class Car
{
private int _maxSpeed; // < this field
...
class Car
{
private int _maxSpeed; // < this field
...
Pobiega
Pobiega17mo ago
"field" is what we call "object-level variables"
Soinagie
Soinagie17mo ago
This is getting too much So _maxSpeed and maxSpeed are the same since they're both variables?
nukleer bomb
nukleer bomb17mo ago
You just got a lot of information here. You need to read something to understand it consistently
Soinagie
Soinagie17mo ago
Then why did you call it twice if they're the same?
Pobiega
Pobiega17mo ago
they are different kinds of variables.
PPSz
PPSz17mo ago
there are two different variables. I assign value of maxSpeed to _maxSpeed
Pobiega
Pobiega17mo 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
Soinagie17mo ago
Can't you just use maxSpeed outside?
PPSz
PPSz17mo ago
since it's declared:
Soinagie
Soinagie17mo ago
Just don't call it private
Pobiega
Pobiega17mo ago
go ahead and try programming is best learnt by doing so go ahead
PPSz
PPSz17mo ago
yes, that's good idea to try yourself how the program will behave if you make that change
Soinagie
Soinagie17mo ago
The name 'maxSpeed' does not exist in the current context But it exists
Thinker
Thinker17mo ago
nope, it only exists in the method itself
Soinagie
Soinagie17mo ago
How can it not exist?
Pobiega
Pobiega17mo ago
it does not.
Thinker
Thinker17mo ago
after that point, it's gone
Soinagie
Soinagie17mo ago
But I set it to public Plus _maxSpeed shouldn't exist then either
Thinker
Thinker17mo ago
can you show your code again, because it's not clear what you are referring to
Pobiega
Pobiega17mo ago
why not? they are declared at different places
Soinagie
Soinagie17mo 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 bomb17mo ago
maxSpeed and _maxSpeed are two different things
Soinagie
Soinagie17mo ago
There's = They're the same Or should be
nukleer bomb
nukleer bomb17mo ago
= doesn't mean equality in programming. It means "copy value from right to left"
Soinagie
Soinagie17mo ago
so maxSpeed is a type and _maxSpeed is a method
Pobiega
Pobiega17mo ago
not at all
Thinker
Thinker17mo ago
maxSpeed is a parameter/variable, _maxSpeed is a field
Soinagie
Soinagie17mo ago
I still don't know what a field is
Thinker
Thinker17mo ago
it's essentially a variable in a class
Soinagie
Soinagie17mo ago
so a variable
Thinker
Thinker17mo ago
well... yes
Soinagie
Soinagie17mo ago
so both are variables?
Pobiega
Pobiega17mo ago
different kinds of variable thou one is a parameter, the other is a field they are both variables
Soinagie
Soinagie17mo ago
yeah sorry I don't see a difference
Pobiega
Pobiega17mo ago
¯\_(ツ)_/¯
PPSz
PPSz17mo 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
Soinagie17mo ago
_maxSpeed is a method right?
Thinker
Thinker17mo ago
no
Soinagie
Soinagie17mo ago
but its called in a class with nothing else so its a static method
Pobiega
Pobiega17mo ago
its not a method. its not static.
Soinagie
Soinagie17mo ago
sorry but this didn't really help, appriciate it though!
Thinker
Thinker17mo 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
Soinagie17mo ago
then how am I supposed to know if it's a field or a method?
PPSz
PPSz17mo ago
fields doesn't have () at the end and methods have () a the end
Thinker
Thinker17mo 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
Soinagie17mo ago
and how are fields different for variables?
Pobiega
Pobiega17mo ago
its all a matter of scope
Soinagie
Soinagie17mo ago
what scope?
Pobiega
Pobiega17mo ago
a field is available everywhere in the class. methods can use the value as they please. a variable is "local"
Soinagie
Soinagie17mo ago
this is what I'm getting from watching a begginer's tutorial
Pobiega
Pobiega17mo 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
PPSz17mo ago
I'm not sure if it's allowed to post a link but I'd suggests different tutorial
Soinagie
Soinagie17mo ago
you can post it here probably but it's only in a class Car, not MainClass
PPSz
PPSz17mo 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
Pobiega17mo ago
well yes, only in the class the field belongs to, of course
Soinagie
Soinagie17mo ago
then _maxSpeed shouldn't be able to jump to MainClass
Pobiega
Pobiega17mo ago
it doesnt
Soinagie
Soinagie17mo ago
or maybe maxSpeed I don't even know anymore
PPSz
PPSz17mo ago
_maxSpeed is inside car1 and car2 objects from my example
Soinagie
Soinagie17mo ago
I think I'm completly lost let's just call it quits I really appriciate your help though!
PPSz
PPSz17mo 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
Pobiega17mo ago
and most importantly, you got to get your hands dirty. write code.
blinkbat
blinkbat17mo 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
Soinagie17mo ago
I have no idea what you just said but thanks!
Thinker
Thinker17mo ago
$close
MODiX
MODiX17mo ago
Use the /close command to mark a forum thread as answered
Thinker
Thinker17mo ago
^ please do this if you feel finished here
Want results from more Discord servers?
Add your server
More Posts
❔ .net cli use file-scoped namespaceHi! Is there a way for the dotnet cli to use file-scoped namespaces? I have .net 7 + c# 11 project b❔ How to I use my domain for my ASP.NET REST?Hi, question basically says it all. I don't have current application but I'm curious. I do have a do❔ DocumentFormat.OpenXML - insert row into an Excel TableHi, I have a problem with inserting a row into the Table in Excel document. I had no problems with i❔ Get value out of deserialized json object.I need to get value out of meta, from the "object". As for now I also use this: Dictionary<string, ❔ I'm confused how "x = false" can cause a NullReferenceExceptiondeclaration of _isUpdatingPeakValue is inside a public sealed class: ```C private volatile bool _is❔ Azure Speech Service wont shut upI am using azure in a WPF app, where I have two buttons, Read, and Stop The problem is, that when I❔ The remote certificate is invalid because of error in the certificate chain: partial chainWhen I debug it from VS, the request responds successfully, but when the service is deployed to anot❔ ✅ How does .NET MAUI Blazor relate to Blazor Server or WebAssembly?Or am I conflating those ideas? I see the .NET MAUI Blazor project template `index.html` has the li✅ Output not correctIm doing a Client/server program the program will give out in output if the user is a minor or not t❔ How to create an event handler for a variableI am currently developing a HTML editor that has tabs and a page that shows the output. The variable