简体   繁体   中英

Access to class into UserControl (C# WinForms)

I edit my question fo show details. Now it code work, but I wont know, may be I done incorrect architecture?

Main form:

    public partial class MainForm1 : Form
    {
        public MainForm1()
        {
            InitializeComponent();

            string errorMessage = "";

            DeviceInitControl.BO.BO_GetParams deviceParams;
            SerialPortInitControl.BO.BO_GetParams serialPortParams;
            HttpClientInitControl.BO.BO_GetParams httpClientParams;

            if (USC_AddDeviceControl.deviceInitParams.GetValues(out deviceParams, ref errorMessage))
            {
                //Device init
            }
            else
            {
                errorMessage = "Device initialisation parametrs error! " + errorMessage;
            }
            if (USC_AddDeviceControl.serialPortInitParams.GetValues(out serialPortParams, ref errorMessage))
            {
                //Serial port init
            }
            else
            {
                errorMessage = "Serial port initialisation parametrs error! " + errorMessage;
            }
            if (USC_AddDeviceControl.httpClientInitParams.GetValues(out httpClientParams, ref errorMessage))
            {
                //Http client init
            }
            else
            {
                errorMessage = "Http client initialisation parametrs error! " + errorMessage;
            }

            if (String.IsNullOrEmpty(errorMessage))
            {
                //Next step
            }
            else
            {
                //Show errorMessage to logs
            }
        }
    }

User control USC_AddDeviceControl contain other UserControls (DeviceInitControl, SerialPortInitControl and HttpClientInitControl).

    public partial class SerialPortInitControl : UserControl
    {
        public class BO
        {
            public enum BO_Language
            {
                RUS,
                EN
            }

            public class BO_Init
            {
                public class CbxItems
                {
                    public string[] names;
                    public string[] baudRates;
                    public string[] parities;
                    public string[] dataBits;
                    public string[] stopBits;
                }

                public CbxItems cbxItems = new CbxItems();
            }

            public class BO_GetParams
            {
                public string name;
                public string baudRate;
                public string paritie;
                public string dataBits;
                public string stopBits;
            }
            /*
            public class BO_SetParams
            {
                //Some controls in UserControlLibrary can contain it class 
            }
            */
        }


        public SerialPortInitControl()
        {
            InitializeComponent();
        }

        public void Init(in BO.BO_Language language, in BO.BO_Init initParams) // This methode is public because I can not initialisaion UserControl in constructor, because constructor can not take a parametrs
        {
            ControlsInit(in initParams);
            SetLanguage(in language);

            void ControlsInit(in BO.BO_Init controlsParams)
            {
                CbxInit(in controlsParams.cbxItems); //Some controls in UserControlLibrary can contain TextBox, DataGridView and other 

                void CbxInit(in BO.BO_Init.CbxItems items)
                {
                    CBX_Name.Items.AddRange(items.names);
                    CBX_BaudRate.Items.AddRange(items.baudRates);
                    CBX_Parity.Items.AddRange(items.parities);
                    CBX_DataBits.Items.AddRange(items.dataBits);
                    CBX_StopBits.Items.AddRange(items.stopBits);
                }
            }
        }

        public void SetLanguage(in BO.BO_Language language)
        {
            switch (language)
            {
                case BO.BO_Language.RUS:
                    {
                        LBL_Name.Text = "Имя порта";
                        LBL_BaudRate.Text = "Скорость";
                        LBL_Parity.Text = "Бит четности";
                        LBL_DataBits.Text = "Бит данных";
                        LBL_StopBits.Text = "Стоп бит";
                    }
                    break;

                case BO.BO_Language.EN:
                    {
                        LBL_Name.Text = "Port name";
                        LBL_BaudRate.Text = "Baud rate";
                        LBL_Parity.Text = "Parity";
                        LBL_DataBits.Text = "Data bits";
                        LBL_StopBits.Text = "Stop bits";
                    }
                    break;

                default:
                    {
                        throw new Exception("Control language is not define!");
                    }
                    break;
            }
        }

        public bool GetParams(out BO.BO_GetParams values, ref string errorMessage)
        {
            if (CheckGetParams(out values, ref errorMessage))
            {
                values = new BO.BO_GetParams()
                {
                    name = CBX_Name.Text,
                    baudRate = CBX_BaudRate.Text,
                    paritie = CBX_Parity.Text,
                    dataBits = CBX_DataBits.Text,
                    stopBits = CBX_StopBits.Text,
                };

                return true;
            }
            else
            {
                values = null;
                errorMessage = "Checking failed! " + errorMessage;
                return false;
            }

            bool CheckGetParams(out BO.BO_GetParams values, ref string errorMessage)
            {
                values = new BO.BO_GetParams();
                bool valid = true;

                if (string.IsNullOrEmpty(CBX_Name.Text)) { valid = false; errorMessage = "Name field is null or empty! "; }
                if (string.IsNullOrEmpty(CBX_BaudRate.Text)) { valid = false; errorMessage = "BaudRate field is null or empty! "; }
                if (string.IsNullOrEmpty(CBX_Parity.Text)) { valid = false; errorMessage = "Parity field is null or empty! "; }
                if (string.IsNullOrEmpty(CBX_DataBits.Text)) { valid = false; errorMessage = "DataBits field is null or empty! "; }
                if (string.IsNullOrEmpty(CBX_StopBits.Text)) { valid = false; errorMessage = "StopBits field is null or empty! "; }

                return valid;
            }
        }
        /*
        public bool SetParams(in BO.BO_SetParams values, ref string errorMessage)
        {
            if (CheckSetValues(in values, ref errorMessage))
            {
                return true;
            }
            else
            {
                errorMessage = "Checking failed! " + errorMessage;
                return false;
            }

            bool CheckSetParams(in BO.BO_SetParams values, ref string errorMessage)
            {
                errorMessage = "Methode is not defined! ";
                return false;
            }
        }
        */
    }

What you mean correct/modify in this code? I trying write clear code and need a help for it. Thank`s!

Your UserControl hasn't instance of BO class. You just define BO class inside SerialPortInitControl, so when you will create instanse of it, it will be like this:

var instBO = new SerialPortInitControl.BO.BO_GetValues()

so, you created object of BO class, but when you stored it in UserConrol?

public partial class SerialPortInitControl : UserControl
{
    public class BO
    {
        public class BO_GetValues
        {
            public string name;
            public string baudRate;
            public string paritie;
            public string dataBits;
            public string stopBits;
        }
    }

    //instance of BO class
    public BO BO_Instanse {get;}
    
    //instance of BO.Get_Values class
    public BO.BO_GetValues BO_GetValues_Instanse {get;}

    public SerialPortInitControl()
    {
        InitializeComponent();
        BO_Instanse = new BO();
        BO_GetValues_Instanse = new BO.BO_GetValues();
    }


    public bool GetValues(out BO.BO_GetValues values, ref string errorMessage)
    {
        if(BO_Instanse == null)//need real code here
        { 
            values = new BO.BO_GetValues()
            {
                name = CBX_Name.Text,
                baudRate = CBX_BaudRate.Text,
                paritie = CBX_Parity.Text,
                dataBits = CBX_DataBits.Text,
                stopBits = CBX_StopBits.Text,
            };

            return true;
        }
        else
        {
            values = null;
            errorMessage = "Checking failed! " + errorMessage;
            return false;
        }
    }
}

So, this will works:

var control = new SerialPortInitControl();
var bo = control.BO_Instanse;
var bo_values = control.BO_GetValues_Instanse;

Also you can create instances of you classes without UserControl class:

 var boInstanse = new SerialPortInitControl.BO();
 var boGetValues_Instanse = new SerialPortInitControl.BO.BO_GetValues();
public class Personmodel
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string NickName { get; set; }
        public int Points { get; set; }
        public int CurrLevel { get; set; }
        public int CurrClass { get; set; }
        public List<AvatarPicture> Avatars { get; set; } = new List<AvatarPicture>();
        public string FullName
        {
            get { return $"{ FirstName } { LastName }"; }

    }
}

    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM