66 Replies
We do not help in DMs
$details
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, what you expect the result to be, what .NET version you are using and what platform/environment (if any) are relevant to your question. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
ok
ill paste
Read all of it
We do not help with entire projects.
i understand
i wrote most of it but facing a problem which i dont know how to countiue can i screen shot?
a problem which is ..?
Please avoid screenshotting unless it contains the error
i wrote a program which drawing shape in the console and it it drawing an endless loop of shapes when i starting it i want to know why
Just send the code, then
ok wait
public static class ScreenSaver
{
// Start the screen saver
public static void Start()
{
// Loop until any key is pressed
while (!Console.KeyAvailable)
{
// Create a random shape
Shape shape = GenerateRandomShape();
// Initialize shape with random values
shape.InitWithRandomValues();
// Draw the shape
shape.Draw();
// Wait for half a second
Thread.Sleep(500);
// Show details of the shape
shape.ShowDetails();
// Wait for another half a second
Thread.Sleep(500);
// Clear the console
Console.Clear();
}
}
// Generate a random shape
private static Shape GenerateRandomShape()
{
Random rand = new Random();
int shapeType = rand.Next(1, 6); // Random number between 1 and 5
switch (shapeType)
{
case 1:
return new Square(0, 0, Shape.ConsoleColor.white, 0,0);
case 2:
return new Circle(0, 0, Shape.ConsoleColor.white, 0);
case 3:
return new Rectangle(0, 0, Shape.ConsoleColor.white, 0, 0);
case 4:
return new VerticalLine(0, 0, 0, Shape.ConsoleColor.white, 0);
case 5:
return new HorizontalLine(0, 0, 0, Shape.ConsoleColor.white, 0);
default:
return new Square(0, 0, Shape.ConsoleColor.white, 0,0);
}
}
}
}
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/$code
public static class ScreenSaver
{
// Start the screen saver
public static void Start()
{
// Loop until any key is pressed
while (!Console.KeyAvailable)
{
// Create a random shape
Shape shape = GenerateRandomShape();
// Initialize shape with random values
shape.InitWithRandomValues();
// Draw the shape
shape.Draw();
// Wait for half a second
Thread.Sleep(500);
// Show details of the shape
shape.ShowDetails();
// Wait for another half a second
Thread.Sleep(500);
// Clear the console
Console.Clear();
}
}
// Generate a random shape
private static Shape GenerateRandomShape()
{
Random rand = new Random();
int shapeType = rand.Next(1, 6); // Random number between 1 and 5
switch (shapeType)
{
case 1:
return new Square(0, 0, Shape.ConsoleColor.white, 0,0);
case 2:
return new Circle(0, 0, Shape.ConsoleColor.white, 0);
case 3:
return new Rectangle(0, 0, Shape.ConsoleColor.white, 0, 0);
case 4:
return new VerticalLine(0, 0, 0, Shape.ConsoleColor.white, 0);
case 5:
return new HorizontalLine(0, 0, 0, Shape.ConsoleColor.white, 0);
default:
return new Square(0, 0, Shape.ConsoleColor.white, 0,0);
}
}
}
}
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/'''cs
public static class ScreenSaver
{
// Start the screen saver
public static void Start()
{
// Loop until any key is pressed
while (!Console.KeyAvailable)
{
// Create a random shape
Shape shape = GenerateRandomShape();
// Initialize shape with random values
shape.InitWithRandomValues();
// Draw the shape
shape.Draw();
// Wait for half a second
Thread.Sleep(500);
// Show details of the shape
shape.ShowDetails();
// Wait for another half a second
Thread.Sleep(500);
// Clear the console
Console.Clear();
}
}
// Generate a random shape
private static Shape GenerateRandomShape()
{
Random rand = new Random();
int shapeType = rand.Next(1, 6); // Random number between 1 and 5
switch (shapeType)
{
case 1:
return new Square(0, 0, Shape.ConsoleColor.white, 0,0);
case 2:
return new Circle(0, 0, Shape.ConsoleColor.white, 0);
case 3:
return new Rectangle(0, 0, Shape.ConsoleColor.white, 0, 0);
case 4:
return new VerticalLine(0, 0, 0, Shape.ConsoleColor.white, 0);
case 5:
return new HorizontalLine(0, 0, 0, Shape.ConsoleColor.white, 0);
default:
return new Square(0, 0, Shape.ConsoleColor.white, 0,0);
}
}
}
}
Its `
` not '
Not '
The key left to
1
, above Tab
At least on my keyboard, ymmvstill it wont let me enter the code
Works just fine
i now
wait
And the issue is that the loop never stops running, right?
i dont know its just spamming ** all over the console
does it stop if you hit a key?
no
I assume
*
is the character you use to draw the shapesi think theres a problem with one of the shapes
Well, I'd say use the debugger to see how it goes
correct
Go ahead, then
i dont know how to use it):
$debug
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
thats whats it doing
Did you place a breakpoint anywhere?
i think the problem is in one of the shape classes i have wrote
yes
And did it break the code execution?
If so, then step through and see what prints this mass of asterisks
still
coudlnt found it
Do a test
Instead of printing a random shape, try printing them individually'
Outside of the loop, even
Comment it out
See which shape causes issues
tried to do circle it and thats what it showed de
What's this entire property
oh your in VS? Just pause while it's drawing. You should go straight to where the loop is (probably)
I needed just the property
So, what it does is: when you get a value from Radius, it gets a value from Radius
oh my english is not that good so forgive me
To do that, it needs to get a value from Radius, which gets a value from Radius, which gets a value from Radius, which gets a value from Radius, which gets a value from Radius, which gets a value from Radius, which gets a value from Radius, which gets a value from Radius, which gets a value from Radius, which gets a value from Radius, which gets a value from Radius, which gets a value from Radius, which gets a value from Radius, which gets a value from Radius, which gets a value from Radius, which gets a value from Radius, which gets a value from Radius
Error: stack overflow
If you want to have a property like that, you need a backing field
You cannot reference the property in itself, you will get an infinite loop
And this will overflow the stackgot it
so how i fix it
By using a backing field
$getsetdevolve
can be shortened to
can be shortened to
can be shortened to
can be shortened to
See the examples that use
_underscored
fields
Second to last example, in particular
That is a property with a backing fieldok but in my program i get a random value for the radius
So?
The value you give to the property can be whatever
Using a backing field does not dictate what value can you give the property
We're only fixing this
The usage will remain the exact same
the backing field shot be in the same class?
How is it in the examples posted above?
bro
i got confused fr
What's confusing about it?
i didnt understand what to change in the code and where
Instead of referencing the property, which references the property, which references the property, which references the property, which references the property, which references the property, which references the property
Make a field
And reference the field from the property
ok
how to do it
BAD
GOOD
oh
wait ill do it
like that?
Yes
ok
Except your code has some logic in the serrer
So you might want to add the same logic here