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
leowest
leowest4mo ago
$paste
MODiX
MODiX4mo ago
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!
Angius
Angius4mo ago
Do you change this value at any point?
Spaghetti_cult_owner
yes It's set as 0 at startup and it gets changed later on Going to send the code real quick
Spaghetti_cult_owner
BlazeBin - cwvpksiwvdrp
A tool for sharing your source code with the world!
leowest
leowest4mo ago
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
Spaghetti_cult_owner
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?
leowest
leowest4mo ago
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.
Spaghetti_cult_owner
Adding singletons to the checklist
leowest
leowest4mo ago
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
Spaghetti_cult_owner
I have went through that, and overriding / overloading, abstraction, encapsulation
leowest
leowest4mo ago
that's good
leowest
leowest4mo ago
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
leowest
leowest4mo ago
this is what I like suggesting people to these are a series of small videos that take care of explaining things in small parts
Spaghetti_cult_owner
But I still can't understand why a public variable is acting like there is a local overriding it Thanks
leowest
leowest4mo ago
which one was that
Spaghetti_cult_owner
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
leowest
leowest4mo ago
because you initialize it as 0
Spaghetti_cult_owner
Indeed I do, but the value changes, right? In the vehicle constructor
leowest
leowest4mo ago
ah forgot the constructor
MODiX
MODiX4mo ago
leowest
REPL Result: Success
var car = new Car();
Car.Count = 20;
Console.WriteLine(Car.Count);
car = new Car();
Console.WriteLine(Car.Count);

public class Car
{
public static int Count { get; set; } = 0;
public Car()
{
Count++;
}
}
var car = new Car();
Car.Count = 20;
Console.WriteLine(Car.Count);
car = new Car();
Console.WriteLine(Car.Count);

public class Car
{
public static int Count { get; set; } = 0;
public Car()
{
Count++;
}
}
Console Output
20
21
20
21
Compile: 442.983ms | Execution: 42.859ms | React with ❌ to remove this embed.
Spaghetti_cult_owner
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
leowest
leowest4mo ago
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
Spaghetti_cult_owner
Oh, it does compile
leowest
leowest4mo ago
then u changed it because the one u posted above in here have issues
Spaghetti_cult_owner
I removed some parts that were comments and may have snipped something
leowest
leowest4mo ago
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
Spaghetti_cult_owner
<Car name>.ToString()
leowest
leowest4mo ago
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
leowest
leowest4mo ago
yeah if u print it on the main method
leowest
leowest4mo ago
but you wanted to print it from a method inside the UI that have no knowledge of what car is
Spaghetti_cult_owner
Oh yeah, I have thought about that
leowest
leowest4mo ago
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
//public static int rows = 10;
static string VehicleRows(int rows)
{
int test1 = Vehicle.Count;
/* for (int i = 0; i <= Vehicle.Count; i++)
{
return "| o| Speed: 000km/h, Distance driven: 100km |";
}
return null;*/
//Console.WriteLine(rows);
Console.WriteLine(string.Join("", Enumerable.Repeat("Test text.", 5))); //Need backup
}
//public static int rows = 10;
static string VehicleRows(int rows)
{
int test1 = Vehicle.Count;
/* for (int i = 0; i <= Vehicle.Count; i++)
{
return "| o| Speed: 000km/h, Distance driven: 100km |";
}
return null;*/
//Console.WriteLine(rows);
Console.WriteLine(string.Join("", Enumerable.Repeat("Test text.", 5))); //Need backup
}
Spaghetti_cult_owner
and that goes into the topic of automatically named cars which I'll have to figure ou how to do
leowest
leowest4mo ago
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
Spaghetti_cult_owner
yeah, my resources didn't have collections will have to check it out I guess lists too
leowest
leowest4mo ago
list is part of collections
Spaghetti_cult_owner
oh, convenient
leowest
leowest4mo ago
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
Spaghetti_cult_owner
Thanks a lot