user5630104
user5630104

Reputation:

Keep getting the CS0122 Error and do not understand how to fix it

I have this code, and the 'lblPatientVital1-4', 'lblBedNumber' and 'lblPatientName' bits of code keep giving me the CS0122 error. Saying it is inaccessible due to its protection level. I have looked around the internet made the source files not read only but still have no luck.

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

namespace HospitalMonitor
{
    public class Controller
    {
        public CentralModule centralStationBedsideDetails;
        public string module1Name, module2Name, module3Name, module4Name, bedName, patientName;

        public CentralModule CentralStationBedsideDetails
        {
            get { return centralStationBedsideDetails; }
            set { centralStationBedsideDetails = value; }
        }

        public void SetSelectedModules(string module1, string module2, string module3, string module4, string bednumber, string pName)
        {
            module1Name = module1;
            module2Name = module2;
            module3Name = module3;
            module4Name = module4;
            bedName = bednumber;
            patientName = pName;
            SetCentralStationBedsideDetails();
        }

        public void SetCentralStationBedsideDetails()
        {
            centralStationBedsideDetails.lblPatientVital1.Text = module1Name;
            centralStationBedsideDetails.lblPatientVital2.Text = module2Name;
            centralStationBedsideDetails.lblPatientVital3.Text = module3Name;
            centralStationBedsideDetails.lblPatientVital4.Text = module4Name;
            centralStationBedsideDetails.lblBedNumber.Text = bedName;
            centralStationBedsideDetails.lblPatientName.Text = patientName;
        }
    }
}

Upvotes: 0

Views: 734

Answers (2)

Eric J.
Eric J.

Reputation: 150118

If you look at the line

centralStationBedsideDetails.lblPatientVital1.Text = module1Name;

the error message is telling you that

lblPatientVital1

is a property or field of centralStationBedsideDetails that is declared as (mostly likely) private (and certainly not public). This is common in WinForms forms.

You can either modify that property to make it public (or internal, if this code is in the same assembly), or you can provide an additional wrapper property or method that sets/gets the value of that internal property/field and is visible to your code (public, or internal and in the same assembly).

Upvotes: 1

David
David

Reputation: 218877

You're not showing the code for it, but presumably the implementation for CentralModule has members (either properties or class-level variables) by those names. The error is telling you that you're trying to access them as though they are public, but they are not public.

They may be protected, internal, or private perhaps. But they are not public. And therefore your code can't directly access them on that object.

You can make them public, or you can make public members (properties or methods) which provide the functionality you're looking for. Perhaps something like this:

public Label PatientVital1
{
    get { return lblPatientVital1; }
}

which you could use as:

centralStationBedsideDetails.PatientVital1.Text = module1Name;

Or, to de-couple consuming code from UI technologies, you can just expose a method:

public void SetPatientVital1Text(string text)
{
    lblPatientVital1.Text = text;
}

which you could use as:

centralStationBedsideDetails.SetPatientVital1Text(module1Name);

Upvotes: 0

Related Questions