Resolved Defining a new class that contains another class

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
125
Programming Experience
10+
I have a class defined that I am going to be assigning to a PropertyGrid. The class is defined as a normal class with properties, but one of the properties is another class. This worked just fine in Visual Basic, but in C# I get an error on the line that has the inclusion of the other class.

Class within a class:
namespace ATE_Lab
{
    public enum TemperatureDeviceName
    {
        Thermotron2800,
        Thermotron3800,
        Sun,
        InfraredScanner,
        WatlowF4,
        EZT570i,
        ESPEC
    }

    public enum TemperatureScale
    {
        Celsius,
        Fahrenheit
    }

public class TemperatureDevice
{

    private float _SetPoint = 25f;
    private bool _IsController = true;
    private TemperatureDeviceName _DeviceName = TemperatureDeviceName.Thermotron2800;
    private string _ErrorMessage = "";
    private bool _Control = true;
    private short _SoakTime = 0;
    private float _MinTemperature = 0f;
    private float _MaxTemperature = 0f;

    [CategoryAttribute("Device")]
    [DefaultValue(typeof(TemperatureDeviceName), "Thermotron2800")]
    [DescriptionAttribute("Device name of the communication device")]
    public TemperatureDeviceName DeviceName
    {
        get
        {
            return _DeviceName;
        }
        set
        {
            switch (value)
            {
                case TemperatureDeviceName.InfraredScanner:
                        _IsController = false;
                        break;
                case TemperatureDeviceName.WatlowF4:
                        ProtocolSetup.Address = 1;
                        ProtocolSetup.CommPort.Parity = System.IO.Ports.Parity.None;
                        break;
                case TemperatureDeviceName.EZT570i:
                        ProtocolSetup.Address = 1;
                        ProtocolSetup.CommPort.Parity = System.IO.Ports.Parity.Even;
                        break;
                default:
                        _IsController = true;
                        break;
            }
            _DeviceName = value;
        }
    }

    [CategoryAttribute("Setup")]
    [DefaultValue(typeof(TemperatureScale), "Celsius")]
    [DescriptionAttribute("Scale of the communication device")]
    public TemperatureScale Scale { get; set; }

    [CategoryAttribute("Setup")]
    [DescriptionAttribute("Communication protocol of the device")]
    public ATE_Lab.CommSetup ProtocolSetup { get; set; } = new ATE_Lab.CommSetup();


The errors are:
Error 1 Invalid token '=' in class, struct, or interface member declaration
Error 2 Invalid token '(' in class, struct, or interface member declaration



The corresponding line in the original VB.Net code is:
VB.Net original code:
    Public Property ProtocolSetup As New CommSetup



Any ideas on why it won't compile in C#?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
Which version of the C# language are you using? As I recall, older versions of C# won't let you initialize a property at the same time you are declaring it. In the older versions of the language, you would need to do the initialization in your constructor.
 

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
125
Programming Experience
10+
.Net 4.5.

So I did do the initializing in the constructor, but I get the following output:
TemperaturDevice.png


instead of the first two lines from above followed by the ProtocolSetup which looks like this:

CommSetup.png



It's getting there for sure. Not sure what else I need to do to make it display properly. I haven't done much work with the PropertyGrid before.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,469
Location
Chesapeake, VA
Programming Experience
10+
See:
 

tim8w

Well-known member
Joined
Sep 8, 2020
Messages
125
Programming Experience
10+
Top Bottom