none
Convert Struct To Class RRS feed

  • Question

  • Greetings - I have a Struct set-up that is functioning and working, however I need it to be a class.  How would this syntax be converted from using a Struct to a Class?

    struct DBData
    {
    	pusupIDlic string supID;
    	pusupIDlic string manID;
    	pusupIDlic string tranID;
    }
    
    private static DBData CaptureDBDataFromDB(string cae)
    {
    	string supIDnumsupIDer = null;
    	string tranIDositor = null;
    	string manID = null;
    
    	using (var ctx = new ClientContext(URL))
    	{
    		var wesupID = ctx.WesupID;
    		List list = wesupID.Lists.GetsupIDyTitle(Site);
    		CamlQuery camlQuery = new CamlQuery();
    		camlQuery.ViewXml = string.Format("<View><ViewFields><FieldRef Name='aa' /><FieldRef Name='a' /><FieldRef Name='r' /></ViewFields><Query><Where><Eq><FieldRef Name='ca' /><Value Type='Text'>" + cae + "</Value></Eq></Where></Query></View>");
    
    		var listItemCollection = list.GetItems(camlQuery);
    
    		ctx.Load(listItemCollection, eachItem => eachItem.Include(item => item, item => item["aa"], item => item["a"], item => item["r"]));
    
    		ctx.ExecuteQuery();
    
    		foreach (ListItem listItem in listItemCollection)
    		{
    			supIDnumsupIDer = listItem["aa"].ToString();
    			tranIDositor = listItem["r"].ToString();
    			manID = listItem["a"].ToString();
    		}
    
    		var vervalues = new DBData
    		{
    			supID = supIDnumsupIDer,
    			manID = manID,
    			tranID = tranIDositor
    		};
    
    		return vervalues;
    	}
    }

    Sunday, August 26, 2018 11:48 PM

Answers

  • public class DBData
    {
    	public string supID {get; set;}
    	public string manID {get; set;}
    	public string tranID {get; set:}
    }
    
    private static DBData CaptureDBDataFromDB(string cae)
    {
    	string supIDnumsupIDer = null;
    	string tranIDositor = null;
            string manID = null;
    
    
    	
            DBData dbdata = new DBdata();
    
            dbdata.supID = subIDnumsubIDer;
             //the populate other properties
    
    //or you don't the above and do it like this
    
             var dbdata = new DBData
             {
    	   supID = supIDnumsupIDer,
    	    manID = manID,
    	    tranID = tranIDositor
    	  };
    
         return dbdata;

    Now you can do the nested class with multiple classes in a classname.cs 

    Or the more optimal approach is to have a DBname.cs so that the class/object can be passed into other class methods.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties

    A classic example of the custom class type you are being shown above  in this.

    https://en.wikipedia.org/wiki/Data_transfer_object

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    The DTO can travel between methods within the class or outside the class to another class. It can be sent remotely through layers in a Layered design, through tiers in N-Tier or from client to server or visa versa in client/server, as examples.

    https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ee658117(v=pandp.10)

    • Marked as answer by IndigoMontoya Monday, August 27, 2018 1:37 PM
    Monday, August 27, 2018 2:19 AM

All replies

  • Since you already have "public" keyword to all the publicly accessible fields, I guess just replacing "struct" with "class" is okay.

    There aren't much difference undernearth between class and struct. The "by reference"/"by value" and "on stack"/"on heap" difference does not affect your usage here.

    Monday, August 27, 2018 1:31 AM
    Answerer
  • public class DBData
    {
    	public string supID {get; set;}
    	public string manID {get; set;}
    	public string tranID {get; set:}
    }
    
    private static DBData CaptureDBDataFromDB(string cae)
    {
    	string supIDnumsupIDer = null;
    	string tranIDositor = null;
            string manID = null;
    
    
    	
            DBData dbdata = new DBdata();
    
            dbdata.supID = subIDnumsubIDer;
             //the populate other properties
    
    //or you don't the above and do it like this
    
             var dbdata = new DBData
             {
    	   supID = supIDnumsupIDer,
    	    manID = manID,
    	    tranID = tranIDositor
    	  };
    
         return dbdata;

    Now you can do the nested class with multiple classes in a classname.cs 

    Or the more optimal approach is to have a DBname.cs so that the class/object can be passed into other class methods.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties

    A classic example of the custom class type you are being shown above  in this.

    https://en.wikipedia.org/wiki/Data_transfer_object

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    The DTO can travel between methods within the class or outside the class to another class. It can be sent remotely through layers in a Layered design, through tiers in N-Tier or from client to server or visa versa in client/server, as examples.

    https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ee658117(v=pandp.10)

    • Marked as answer by IndigoMontoya Monday, August 27, 2018 1:37 PM
    Monday, August 27, 2018 2:19 AM
  • Since the current variant is functioning, have you evaluated the possible malfunctions of switching to class (in other parts of your program)?

    Monday, August 27, 2018 7:44 AM
  • Not sure, whether your variable names are really so messed up or maybe it is just a copy'n'paste error. But you should use speaking names, avoid abbreviations at all costs.

    Then: You query a list. Is it guaranteed to contain only one item?

    E.g.

    public class EntityName // Name it after the real entity (or relationship).
    {
        public string ManufacturerID { get; set; }
        public string SupplierID { get; set; }
        public string TransactionID { get; set; }
        public static EntityName Retrieve(string cae, string url, object site)
        {
            EntityName result = null;
            using (var ctx = new ClientContext(url))
            {
                var weSupplierID = ctx.WeSupplierID;
                List suppliers = weSupplierID.Lists.GetSupplierIDTitle(site);
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = string.Format("<View><ViewFields><FieldRef Name='aa' /><FieldRef Name='a' /><FieldRef Name='r' /></ViewFields><Query><Where><Eq><FieldRef Name='ca' /><Value Type='Text'>" + cae + "</Value></Eq></Where></Query></View>");
                var supplierItems = suppliers.GetItems(camlQuery);
                ctx.Load(supplierItems, eachItem => eachItem.Include(item => item, item => item["aa"], item => item["a"], item => item["r"]));
                ctx.ExecuteQuery();
                foreach (ListItem suppliersItem in supplierItems)
                {
                    result = new EntityName()
                    {
                        ManufacturerID = suppliersItem["a"].ToString(),
                        SupplierID = suppliersItem["aa"].ToString(),
                        TransactionID = suppliersItem["r"].ToString()
                    };
    
                    break;
                }
    
                return result;
            }
        }
    }

    Monday, August 27, 2018 8:14 AM