C
C#3mo ago
Tajaba

OOP help with a project please

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