Please Help me out. I'm new to C#

abzstarboy

New member
Joined
Nov 12, 2019
Messages
3
Programming Experience
Beginner
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Midexampractice
{
  public class House
    {
        public static void Main(string[] args)
        {
            double length;
            double width;
            double area;
            public House(double l, double w)
            {
                length = l;
                width = w;
            }
            public double ShowData()
            {
                area = length * width;
                Console.WriteLine("I am a house, my area is " + area + "m2.");
            }
            public class testhouse
        {
            double length;
            double width;
            Console.WriteLine("Enter the length");
            double l = double.Parse(Console.ReadLine());
            Console.WriteLine("Enter the Width");
            double w = double.Parse(Console.ReadLine());
            House h = new House(length, width)
                h.ShowData();
         }
            }
        }


Screenshot (160).png
 
Last edited by a moderator:
Do you have a specific question? Anyway, on first glance it looks like you are trying to define methods within another method. Prior to C# 8, this wasn't allowed. In C# 8, you are allowed to do this as long as the methods are not public.

In the future, please post your code in code tags. Also copy and paste the errors into your post instead of attaching a screenshot.
 
Do you have a specific question? Anyway, on first glance it looks like you are trying to define methods within another method. Prior to C# 8, this wasn't allowed. In C# 8, you are allowed to do this as long as the methods are not public.

In the future, please post your code in code tags. Also copy and paste the errors into your post instead of attaching a screenshot.
Can please guide me step by step?
 
The problem here is that you tried to splat all the code into your program all at one time, and then decided to try to fix things afterwards. The better approach is write your program incrementally. Follow of a cycle of add some code, make it compile correctly, make it run correctly, check into source control or backup, then add one more bit of functionality to the code, make it compile correctly, make it run correctly, check into source control or backup, repeat.

So start of with just an empty class TestHouse. Add in an empty static Main() method to it. Get that to compile and run correctly. (Check this into source control or backup.) Next add an empty class named House. Get that to compile and run. Check in. In the Main() method add code to construct an instance of the House and assign it to a variable. Get that to compile and run. Check in. In the House class add a constructor that takes the length and width. Change your code in the Main() to pass some dummy values to the class constructor. Get that to compile and run. Check in. Add code to the House to store the length and width into fields. Get that to compile and run. Check in. Add an method to the House class to show the data, and have your code in the Main() method call that method for showing the data. Get that to compile and run. Check in. Modify the show data method to compute the area and show the area on the console. Get that to compile and run. Check in. Modify the Main() method to accept input from the user for the width and height. Get that to compile and run. Check in. Replace the dummy values passed in to the constructor in the Main() with the input values from the user. Get that to compile and run. Check in.
 
For future reference, please provide a title that summarises the issue you want help with. Everyone who posts a question wants help so a title asking for help is as much use as no title at all. We should be able to tell whether the thread is relevant to us without opening it.
 
Back
Top Bottom