Static property issues
I have a static public property of a class I'll call A, whose value in a class I'll call B is 0, when in reality its value is something else. The value of the class in the Main() method is just as it should be, which is very perplexing.
42 Replies
$paste
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Do you change this value at any point?
yes
It's set as 0 at startup and it gets changed later on
Going to send the code real quick
BlazeBin - cwvpksiwvdrp
A tool for sharing your source code with the world!
that's a mess
you really shouldn't use it like that
looking at the code the most logical way here would be for u to remove the Count from Vehicle and do a proper collection
of the cars you're creating
it seems you're making things static for the solo purpose of printing it to the console because its a static method
but that is not how u should do it
is this a school project where u are limited on things u can use meaning what u learned or a personal project
if its a school project it would help if u provide what your teacher asked
No I'm learning c# on my own
So I guess I'll have to research collections
Other than all that, is there a reason that a parameter has two different values when it's static and thus can't have instances?
well in your example because its a bad design
you could technically keep a static field to keep a counter that survive thru instances but that can be a very specific scenario
but an actual useful scenario u would have a public static property would be a singleton
these are just examples.
Adding singletons to the checklist
I wouldn't add it to the list right now
add it to the list when u actually find a use for it
right now I would worry about variable and scopes
and classes and OOP
I have went through that, and overriding / overloading, abstraction, encapsulation
that's good
C# Fundamentals for Absolute Beginners
Learn C# programming from an expert in the industry. Get the tools, see how to write code, debug features, explore customizations, and more. For newer videos head over to dot.net/videos
this is what I like suggesting people to
these are a series of small videos that take care of explaining things in small parts
But I still can't understand why a public variable is acting like there is a local overriding it
Thanks
which one was that
The Count property of class Vehicle
It displays property on main()
But when I try checking it in the UI class it displays as 0
because you initialize it as 0
Indeed I do, but the value changes, right?
In the vehicle constructor
ah forgot the constructor
leowest
REPL Result: Success
Console Output
Compile: 442.983ms | Execution: 42.859ms | React with ❌ to remove this embed.
Interesting
Yeah, if I'm correct in the lines above the class car are for main(), it works
The moment you try to access count from another class it gives you 0
I mean that code u had would not compile in first place
and beyond that, how would u access the instance of each car
to print their information
Oh, it does compile
then u changed it because the one u posted above in here have issues
I removed some parts that were comments and may have snipped something
figured but yeah it does print the right number
because its public static
its like a global variable, they have specific use cases but that was not one of it
<Car name>.ToString()
try it
u were doing that from inside teh Ui
or are u going to make your entire code now static just so u can print things 😛
u see what I mean
yeah if u print it on the main method
but you wanted to print it from a method inside the UI that have no knowledge of what car is
Oh yeah, I have thought about that
The method u wanted to print from that exists in a different Class that have absolute no knowledge of what the instance of car is
and that goes into the topic of automatically named cars
which I'll have to figure ou how to do
well check the videos I sent u above
it has a chapter on working with collections
and one about variables and scopes
those are super important
yeah, my resources didn't have collections
will have to check it out
I guess lists too
list is part of collections
oh, convenient
yep, give those videos a go and if u have questions we are here
if u want watch all of it the guy explains things very well
Thanks a lot