none
Object with constructors and one input to multiple outputs

    Question

  • Hallo

    Hope all are well.

    This is probably a very simple question, but we want to start making use of a object class to define equipment types.

    The current object will be a camera with the following attributes (ControlWord, Data, StatusWord).

    So from the main program we got maybe 10 camera's with it's own set of data. From the main program we want to create a new object and set the attribute values, but the attribute values is all calculated based on the substring.

    eg Input data for camera = 123456789

    The Control word will be = 123

    Data will be = 456

    StatusWord will be 789

    So from the main program we want to create camera cm1 = new camera(inputData) in the rest of the program we want to make access the variables as needed. eg. cm1.Controlword or cm1.Data or cm.StatusWord.

    It must also have some default constructors to ensure the data is always valid when creating and instance of the object.

    Below is the class we've tried to make, but the issue is cm. doesn't give any attributes. sure we are missing some big pieces.

    namespace Data_Handler
    {
        public class Cameras
        {
            public Cameras(string InputData, out string ControlWord, out string Data, out string StatusWord)
            {
                string inputData = InputData;
                string controlword = ControlWord = "";
                string data = Data = "";
                string statusword = StatusWord = "";
            }
    
            public string ControlWord(string inputData)
            {
                string data = inputData.Substring(1,4);
                return data;
            }
            public string Data(string inputData)
            {
                string data = inputData.Substring(4,8);
                return data;
            }
            public string StatusWord(string inputData)
            {
                string data = inputData.Substring(8, 5);
                return data;
            }
        }    
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Data_Handler;
    using System.Diagnostics;
    
    namespace Plexico_R1
    {
        class MainSyncroizer
        {
            Cameras cm = new Cameras("12345");
            cm.        
        }
    }


    labjac

    • Moved by Sara Liu Tuesday, March 21, 2017 2:20 AM
    Sunday, March 19, 2017 8:26 PM

Answers

  • Try this.
        public class Cameras
        {
            public Cameras(string inputData)
            {
                ControlWord = inputData.Substring(1,4);
                Data = inputData.Substring(4,8);
                StatusWord = inputData.Substring(8, 5);
            }
    
            public string ControlWord{get; set;}
            public string Data{get; set;}
            public string StatusWord{get; set;}
        }    
    And by the way, if the StatusWord starts at character 8 and is 5 characters long, inputData must be at least 13 characters or there will be a crash.

    • Edited by Ante Meridian Tuesday, March 21, 2017 2:44 AM Added the min 13 chars bit.
    • Marked as answer by labjac Tuesday, March 21, 2017 5:12 PM
    Tuesday, March 21, 2017 2:40 AM

All replies

  • Hi labjac,

    Thank you for your update.

    Refer to your description, this issue is about the development issue of C#. Since our forum is to discuss Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, and Visual Studio Editor, I will help you move this thread to this appropriate forum: Visual Studio Languages Visual C# to seek for the professional support for your issue, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 21, 2017 2:19 AM
  • Try this.
        public class Cameras
        {
            public Cameras(string inputData)
            {
                ControlWord = inputData.Substring(1,4);
                Data = inputData.Substring(4,8);
                StatusWord = inputData.Substring(8, 5);
            }
    
            public string ControlWord{get; set;}
            public string Data{get; set;}
            public string StatusWord{get; set;}
        }    
    And by the way, if the StatusWord starts at character 8 and is 5 characters long, inputData must be at least 13 characters or there will be a crash.

    • Edited by Ante Meridian Tuesday, March 21, 2017 2:44 AM Added the min 13 chars bit.
    • Marked as answer by labjac Tuesday, March 21, 2017 5:12 PM
    Tuesday, March 21, 2017 2:40 AM
  • Hallo Ante

    Thanks for taking to time to send the example.

    I'm trying to understand how this all fits together, been watching all most every video and been on every website for some explanation. This is very power full and really would love to start using the object properly.

    The reason for the simple example at the top was to make more sense of it all.

    The end result is the following:

    Create a class of a camera (The object needs to have a CameraID and LocationID which is set when initializing the object. (camera cn1 = new camera(1,"Stores"). (This part I assume will be part of the constructor which is get only after creating the object).

    Then inside the class we want the following which is not part of the constructor (I assume)

    We want to pass one variable which might be DataHandler("12345678")

    And want to access the      

    ControlWord = cn1.DataHandler.ControlWord();  (output will be 1234)

    Data = cn1.DataHandler.Data(); (output will be 56789101)

    StatusWord = cn1.DataHandler.StatusWord(); (output will be 23456)

    But the object still needs to have cn1.CameraID (output 1)

    And the name cn1.LocationID (Output "Stores")

    The is very over simplified to the real application this is just to understand the concept. 

    The previous code below I made use of outputs in a method which is almost impossible to fault find afterwards. (Ignore the code below but understand the complexity and sure with Class and object we can create a simple structure to access the camera data..

             public void TXRXUDPData(string IPAddress, int PortNr, byte[] Cmd_Code, byte[] Cmd_Header,byte[] Cmd_Data, out string DataOutput,out byte[] DataInput,out bool success)
            {
                DataOutput = "";
                success = false;
                DataInput = new Byte[1024];
                int TimeoutCounter = 0;
                Array.Clear(DataInput, 0, DataInput.Length);
    
                if (CreateConnection(IPAddress,PortNr))
                {
                    int DataLen = 0;                
                    byte[] loc_Header = FinsHeader(IPAddress);
                    DataLen = loc_Header.Length;
                    byte[] loc_CmdCode = Cmd_Code;
                    DataLen = DataLen + loc_CmdCode.Length;
                    byte[] loc_CmdHeader = Cmd_Header;
                    DataLen = DataLen + Cmd_Header.Length;
                    byte[] loc_CmdData = Cmd_Data;
                    DataLen = DataLen + loc_CmdData.Length;
                    byte[] loc_SendByt = new byte[DataLen];
                    var SendData = new byte[DataLen];
    
                    Buffer.BlockCopy(loc_Header, 0, loc_SendByt, 0, loc_Header.Length);
                    Buffer.BlockCopy(loc_CmdCode, 0, loc_SendByt, loc_Header.Length, loc_CmdCode.Length);
                    Buffer.BlockCopy(loc_CmdHeader, 0, loc_SendByt, (loc_Header.Length + loc_CmdCode.Length), loc_CmdHeader.Length);
                    Buffer.BlockCopy(loc_CmdData, 0, loc_SendByt, (DataLen - loc_CmdData.Length), loc_CmdData.Length);
    
                    Buffer.BlockCopy(loc_SendByt, 0, SendData, 0, SendData.Length);               
    
                    try
                    {
                        PLC.Send(SendData, SendData.Length);
                        DataOutput = DtConv.ConvByteToStr(SendData);
                        while ((PLC.Available == 0) && (TimeoutCounter < 50))
                        { 
                        Thread.Sleep(10);
                        ++TimeoutCounter;
                        }   
                        DataInput = GetFinsData();
                        success = true;
    
                    }
                    catch
                    {                  
                        success = false;
                    }
                }
                else
                {               
                    success = false;
                }
                PLC.Close();            
            }



    labjac

    Tuesday, March 21, 2017 9:03 AM