C
C#8mo ago
Tajaba

OOP help with a project please

please dm need help
66 Replies
Buddy
Buddy8mo ago
We do not help in DMs $details
MODiX
MODiX8mo ago
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)
Tajaba
TajabaOP8mo ago
ok ill paste
Buddy
Buddy8mo ago
Read all of it We do not help with entire projects.
Tajaba
TajabaOP8mo ago
i understand i wrote most of it but facing a problem which i dont know how to countiue can i screen shot?
Buddy
Buddy8mo ago
a problem which is ..? Please avoid screenshotting unless it contains the error
Tajaba
TajabaOP8mo ago
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
Angius
Angius8mo ago
Just send the code, then
Tajaba
TajabaOP8mo ago
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); } } } }
Angius
Angius8mo ago
$code
MODiX
MODiX8mo ago
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/
Tajaba
TajabaOP8mo ago
$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); } } } }
MODiX
MODiX8mo ago
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/
Tajaba
TajabaOP8mo ago
'''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); } } } }
Angius
Angius8mo ago
` not '
Angius
Angius8mo ago
The key left to 1, above Tab At least on my keyboard, ymmv
Tajaba
TajabaOP8mo ago
still it wont let me enter the code
Angius
Angius8mo ago
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);
}
}
}



}
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);
}
}
}



}
Works just fine
Tajaba
TajabaOP8mo ago
i now 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);
}
}
}



}
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);
}
}
}



}
Angius
Angius8mo ago
And the issue is that the loop never stops running, right?
Tajaba
TajabaOP8mo ago
i dont know its just spamming ** all over the console
maxmahem
maxmahem8mo ago
does it stop if you hit a key?
Tajaba
TajabaOP8mo ago
no
Angius
Angius8mo ago
I assume * is the character you use to draw the shapes
Tajaba
TajabaOP8mo ago
i think theres a problem with one of the shapes
Angius
Angius8mo ago
Well, I'd say use the debugger to see how it goes
Tajaba
TajabaOP8mo ago
correct
Angius
Angius8mo ago
Go ahead, then
Tajaba
TajabaOP8mo ago
i dont know how to use it):
Angius
Angius8mo ago
$debug
MODiX
MODiX8mo ago
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.
Tajaba
TajabaOP8mo ago
No description
Tajaba
TajabaOP8mo ago
thats whats it doing
Angius
Angius8mo ago
Did you place a breakpoint anywhere?
Tajaba
TajabaOP8mo ago
i think the problem is in one of the shape classes i have wrote yes
Angius
Angius8mo ago
And did it break the code execution? If so, then step through and see what prints this mass of asterisks
Tajaba
TajabaOP8mo ago
still coudlnt found it
Angius
Angius8mo ago
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
Tajaba
TajabaOP8mo ago
No description
Tajaba
TajabaOP8mo ago
tried to do circle it and thats what it showed de
Angius
Angius8mo ago
What's this entire property
Tajaba
TajabaOP8mo ago
public class Circle :Shape
{
private int Radius
{
get { return Radius; }
set { if (Radius<12.5) Radius = value; }
}
public Circle()
{}

public Circle(int x, int y, ConsoleColor color, int radius) :base(x,y,color)
{
Radius = radius;
}
public override void InitWithRandomValues()
{
Random rnd = new Random();
X = rnd.Next(1, 80);
Y = rnd.Next(1, 25);
Color = (ConsoleColor)rnd.Next(Enum.GetNames(typeof(ConsoleColor)).Length);
Radius = rnd.Next(1, (int)12.5);
}
public override void Draw()
{
double angleStep = 1.0 / Radius;
Console.SetCursorPosition(X, Y);
Console.ForegroundColor = (System.ConsoleColor)color;
for (double angle = 0; angle <= 2 * Math.PI; angle += angleStep)
{
int x = (int)Math.Round(X + Radius * Math.Cos(angle));
int y = (int)Math.Round(Y + Radius * Math.Sin(angle));
Console.SetCursorPosition(x, y);
Console.Write("*");

}
}

public override double GetArea()
{
return (Math.PI * Radius * Radius);
}

public override double GetPerimeter()
{
return (2 * Math.PI * Radius);
}

public override void ShowDetails()
{
Console.Write("X" + X, "Y" + Y, "Color" + color, "Radius" + Radius + "Area" + GetArea() + "Perimeter" + GetPerimeter());

}


}
}
public class Circle :Shape
{
private int Radius
{
get { return Radius; }
set { if (Radius<12.5) Radius = value; }
}
public Circle()
{}

public Circle(int x, int y, ConsoleColor color, int radius) :base(x,y,color)
{
Radius = radius;
}
public override void InitWithRandomValues()
{
Random rnd = new Random();
X = rnd.Next(1, 80);
Y = rnd.Next(1, 25);
Color = (ConsoleColor)rnd.Next(Enum.GetNames(typeof(ConsoleColor)).Length);
Radius = rnd.Next(1, (int)12.5);
}
public override void Draw()
{
double angleStep = 1.0 / Radius;
Console.SetCursorPosition(X, Y);
Console.ForegroundColor = (System.ConsoleColor)color;
for (double angle = 0; angle <= 2 * Math.PI; angle += angleStep)
{
int x = (int)Math.Round(X + Radius * Math.Cos(angle));
int y = (int)Math.Round(Y + Radius * Math.Sin(angle));
Console.SetCursorPosition(x, y);
Console.Write("*");

}
}

public override double GetArea()
{
return (Math.PI * Radius * Radius);
}

public override double GetPerimeter()
{
return (2 * Math.PI * Radius);
}

public override void ShowDetails()
{
Console.Write("X" + X, "Y" + Y, "Color" + color, "Radius" + Radius + "Area" + GetArea() + "Perimeter" + GetPerimeter());

}


}
}
maxmahem
maxmahem8mo ago
oh your in VS? Just pause while it's drawing. You should go straight to where the loop is (probably)
Angius
Angius8mo ago
private int Radius
{
get { return Radius; }
set { if (Radius<12.5) Radius = value; }
}
private int Radius
{
get { return Radius; }
set { if (Radius<12.5) Radius = value; }
}
I needed just the property So, what it does is: when you get a value from Radius, it gets a value from Radius
Tajaba
TajabaOP8mo ago
oh my english is not that good so forgive me
Angius
Angius8mo ago
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 stack
Tajaba
TajabaOP8mo ago
got it so how i fix it
Angius
Angius8mo ago
By using a backing field $getsetdevolve
MODiX
MODiX8mo ago
class Foo
{
private int _bar;

public int GetBar()
{
return _bar;
}

public void SetBar(int bar)
{
_bar = bar;
}
}
class Foo
{
private int _bar;

public int GetBar()
{
return _bar;
}

public void SetBar(int bar)
{
_bar = bar;
}
}
can be shortened to
class Foo
{
private int _bar;

public int GetBar() => _bar;

public void SetBar(int bar) => _bar = bar;
}
class Foo
{
private int _bar;

public int GetBar() => _bar;

public void SetBar(int bar) => _bar = bar;
}
can be shortened to
class Foo
{
private int _bar;
public int Bar {
get { return _bar; }
set { _bar = value; }
}
}
class Foo
{
private int _bar;
public int Bar {
get { return _bar; }
set { _bar = value; }
}
}
can be shortened to
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
can be shortened to
class Foo
{
public int Bar { get; set; }
}
class Foo
{
public int Bar { get; set; }
}
Angius
Angius8mo ago
See the examples that use _underscored fields Second to last example, in particular That is a property with a backing field
Tajaba
TajabaOP8mo ago
ok but in my program i get a random value for the radius
Angius
Angius8mo ago
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
Tajaba
TajabaOP8mo ago
the backing field shot be in the same class?
Angius
Angius8mo ago
How is it in the examples posted above?
Tajaba
TajabaOP8mo ago
bro i got confused fr
Angius
Angius8mo ago
What's confusing about it?
Tajaba
TajabaOP8mo ago
i didnt understand what to change in the code and where
Angius
Angius8mo ago
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
Tajaba
TajabaOP8mo ago
ok how to do it
Angius
Angius8mo ago
BAD
class Foo
{
public int Bar {
get => Bar;
set => Bar = value;
}
}
class Foo
{
public int Bar {
get => Bar;
set => Bar = value;
}
}
GOOD
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
class Foo
{
private int _bar;
public int Bar {
get => _bar;
set => _bar = value;
}
}
Tajaba
TajabaOP8mo ago
oh wait ill do it
private int radius;
public int Radius
{
get => radius;
set => radius = value;
}
private int radius;
public int Radius
{
get => radius;
set => radius = value;
}
like that?
Angius
Angius8mo ago
Yes
Tajaba
TajabaOP8mo ago
ok
Angius
Angius8mo ago
Except your code has some logic in the serrer
set { if (Radius<12.5) Radius = value; }
set { if (Radius<12.5) Radius = value; }
So you might want to add the same logic here
Want results from more Discord servers?
Add your server