Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Class to return a list or single item

$
0
0

So I'm writing some code in c# to provision virtual machines in VMware, and i'd like to consolidate my classes/functions as much as possible. What I have noticed is there are times when I know i'm only going to get one item back from a call, and yet i'll use the same call to get more than one item back.

for example when I need a list of datacenters the call returns a list of datacenters, whereas once I've selected a datacenter, the call will return a list of the one datacenter.

on the c# side i'm fairly new, what I've been doing is just grabbing [0] from the return, is it possible to have a class that returns either a list of items, or when there is only one item, simple return that one item not as a list?

        protected List<Datacenter> GetDataCenter(VimClient vimClient, string dcName = null)
        {
            //
            // Get a list of datacenters
            //
            List<Datacenter> lstDatacenters = new List<Datacenter>();
            List<EntityViewBase> appDatacenters = new List<EntityViewBase>();
            try
            {
                if (dcName == null)
                {
                    //
                    // Return all datacenters
                    //
                    appDatacenters = vimClient.FindEntityViews(typeof(Datacenter), null, null, null);
                }
                else
                {
                    //
                    // Return the named datacenter
                    //
                    NameValueCollection dcFilter = new NameValueCollection();
                    dcFilter.Add("name", dcName);
                    appDatacenters = vimClient.FindEntityViews(typeof(Datacenter), null, dcFilter, null);
                }
                foreach (EntityViewBase appDatacenter in appDatacenters)
                {
                    Datacenter thisDatacenter = (Datacenter)appDatacenter;
                    lstDatacenters.Add(thisDatacenter);
                }

 
                return lstDatacenters;
 
            }
 
            catch (VimException ex)
 
            {
 
                //
 
                // VMware Exception occurred
 
                //
 
                txtErrors.Text = "A server fault of type " + ex.MethodFault.GetType().Name + " with message '" + ex.Message + "' occured while performing requested operation.";
                Error_Panel.Visible = true;
                return null;
            }
        }

This is the first example, I have several classes similar, and it's possible that I've written this one improperly for what I need it to do.


Jeffrey S. Patton Jeffrey S. Patton Systems Specialist, Enterprise Systems University of Kansas 1001 Sunnyside Ave. Lawrence, KS. 66045 (785) 864-0242 | http://patton-tech.com


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>