C# inheritance build errors

I am new to Stack Overflow and a neophyte programmer. I am trying to build in Visual Studio 2010 C# someone else’s code as a learning opportunity. I am unable to figure out why I get the following three errors in the four classes (line 50, 59, 67, & 75) at the bottom of the attached stub code (I stripped out from the original program everything not germane to the errors):

1.  “c_basic_object”   Method must have a return type
2.  “:”  ; expected
3.  “(p_name)” Invalid token ')' in class, struct, or interface member declaration
4.  “p_name”  The type or namespace name 'p_name' could not be found (are you missing a using directive or an assembly reference?)

The code is almost identical to the Employee and Manager classes in Andrew Troelsen’s Pro C# 2005 book in Chapter 4.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestStub
{
class Program
{
    static void Main(string[] args)
    {
        String strTest1 = "This is a string #1";
        String strTest2 = "This is a string #2";

        c_basic_object objBasic = new c_basic_object(strTest1);
        Console.WriteLine(objBasic.m_name);

        c_abstract_button_widget objButton = new c_abstract_button_widget();

        Console.WriteLine("Test, Test, Test");
        Console.ReadKey(true);
        Console.WriteLine();
    }
}// End class Program

public class c_basic_object
{
    public string m_name = String.Empty;
    //Constructor  create_basic_object( p_name)
    public c_basic_object(string p_name)
    {
        m_name = p_name;
    }
    // create_basic_object
    //Constructor  construct_empty()
    public c_basic_object()
    {
    }
    // construct_empty
    //@ Destructor  Destroy()
    ~c_basic_object()
    {
    }
} // End class c_basic_object


// -- definitions only for the Virtual Abstract functions in the factory
public class c_abstract_button_widget: c_basic_object
{
    public c_basic_object(string p_name) : base(p_name)
    {
        //Console.WriteLine("Inside c_abstract_button_widget ");
    }
} // end c_abstract_button_widget

// c_abstract_button_widget
public class c_abstract_label_widget: c_basic_object
{
    public c_basic_object(string p_name) : base(p_name)
    {
    }
} // end c_abstract_label_widget

// c_abstract_label_widget
public class c_abstract_draw_surface_widget: c_basic_object
{
    public c_basic_object(string p_name) : base(p_name)
    {
    }
} // end c_abstract_draw_surface_widget

// c_abstract_draw_surface_widget
public class c_abstract_scrollbar_widget: c_basic_object
{
    public c_basic_object(string p_name) : base(p_name)
    {
    }
} // end c_abstract_scrollbar_widget
}// End namespace TestStub
Jon Skeet
people
quotationmark

The problem is here:

public class c_abstract_button_widget: c_basic_object
{
    public c_basic_object(string p_name) : base(p_name)
    {
        //Console.WriteLine("Inside c_abstract_button_widget ");
    }
}

To declare a constructor, you have to specify the class name - but you've written c_basic_object. You meant:

public class c_abstract_button_widget: c_basic_object
{
    public c_abstract_button_widget(string p_name) : base(p_name)
    {
        //Console.WriteLine("Inside c_abstract_button_widget ");
    }
}

You have the same problem in your other subclasses too.

Separately, you should absolutely start following the .NET Naming Conventions. In this case, your classes should be:

  • BasicObject
  • AbstractButtonWidget
  • AbstractLabelWidget
  • AbstractScrollbarWidget

... although given that your "abstract" classes aren't actually abstract, you possibly want to rename them more... and possibly your BasicObject should be WidgetBase (or AbstractWidget, and actually abstract...)

Your parameters should just be name instead of p_name.

I'd also recommend keeping all fields private (I'd ditch the m_ prefix as well, but if the field is private that's not so bad) - you may want to expose the name as a property.

Additionally, you hardly ever need finalizers/destructors in C#. Definitely don't start adding them until you really, really know that you need one.

people

See more on this question at Stackoverflow