C
C#9mo ago
Tajaba

OOP help with a project please

please dm need help
66 Replies
Buddy
Buddy9mo ago
We do not help in DMs $details
MODiX
MODiX9mo 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
TajabaOP9mo ago
ok ill paste
Buddy
Buddy9mo ago
Read all of it We do not help with entire projects.
Tajaba
TajabaOP9mo ago
i understand i wrote most of it but facing a problem which i dont know how to countiue can i screen shot?
Buddy
Buddy9mo ago
a problem which is ..? Please avoid screenshotting unless it contains the error
Tajaba
TajabaOP9mo 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
Angius9mo ago
Just send the code, then
Tajaba
TajabaOP9mo 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
Angius9mo ago
$code
MODiX
MODiX9mo 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
TajabaOP9mo 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
MODiX9mo 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
TajabaOP9mo 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
Angius9mo ago
` not '
Angius
Angius9mo ago
The key left to 1, above Tab At least on my keyboard, ymmv
Tajaba
TajabaOP9mo ago
still it wont let me enter the code
Angius
Angius9mo 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
TajabaOP9mo 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
Angius9mo ago
And the issue is that the loop never stops running, right?
Tajaba
TajabaOP9mo ago
i dont know its just spamming ** all over the console
maxmahem
maxmahem9mo ago
does it stop if you hit a key?
Tajaba
TajabaOP9mo ago
no
Angius
Angius9mo ago
I assume * is the character you use to draw the shapes
Tajaba
TajabaOP9mo ago
i think theres a problem with one of the shapes
Angius
Angius9mo ago
Well, I'd say use the debugger to see how it goes
Tajaba
TajabaOP9mo ago
correct
Angius
Angius9mo ago
Go ahead, then
Tajaba
TajabaOP9mo ago
i dont know how to use it):
Angius
Angius9mo ago
$debug
MODiX
MODiX9mo 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
TajabaOP9mo ago
No description
Tajaba
TajabaOP9mo ago
thats whats it doing
Angius
Angius9mo ago
Did you place a breakpoint anywhere?
Tajaba
TajabaOP9mo ago
i think the problem is in one of the shape classes i have wrote yes
Angius
Angius9mo ago
And did it break the code execution? If so, then step through and see what prints this mass of asterisks
Tajaba
TajabaOP9mo ago
still coudlnt found it
Angius
Angius9mo 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
TajabaOP9mo ago
No description
Tajaba
TajabaOP9mo ago
tried to do circle it and thats what it showed de
Angius
Angius9mo ago
What's this entire property
Tajaba
TajabaOP9mo 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
maxmahem9mo ago
oh your in VS? Just pause while it's drawing. You should go straight to where the loop is (probably)
Angius
Angius9mo 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
TajabaOP9mo ago
oh my english is not that good so forgive me
Angius
Angius9mo 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
TajabaOP9mo ago
got it so how i fix it
Angius
Angius9mo ago
By using a backing field $getsetdevolve
MODiX
MODiX9mo 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
Angius9mo ago
See the examples that use _underscored fields Second to last example, in particular That is a property with a backing field
Tajaba
TajabaOP9mo ago
ok but in my program i get a random value for the radius
Angius
Angius9mo 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
TajabaOP9mo ago
the backing field shot be in the same class?
Angius
Angius9mo ago
How is it in the examples posted above?
Tajaba
TajabaOP9mo ago
bro i got confused fr
Angius
Angius9mo ago
What's confusing about it?
Tajaba
TajabaOP9mo ago
i didnt understand what to change in the code and where
Angius
Angius9mo 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
TajabaOP9mo ago
ok how to do it
Angius
Angius9mo 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
TajabaOP9mo 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
Angius9mo ago
Yes
Tajaba
TajabaOP9mo ago
ok
Angius
Angius9mo 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