none
Returning Variables From Method RRS feed

  • Question

  • I am passing one variable to a method and running a query against a database.  The database query will then return 4 variables, that I need to be able to access from anywhere within my class.  I do not want to set them as static variables as the values could potentially change.  What would be the best plan of attack for me to return a, b, c and d from my GetDBData() procedure?

    static void Main(string[] args)
    {
    	double AN;
    
    	var keepHostOptions = false;
    
    	while (true)
    	{
    		Console.WriteLine("Please Input The Number:");
    		string an = Console.ReadLine();
    
    		if (double.TryParse(an, out AN) == true)
    		{
    			GetDBData(an);
    			
    			//Need to be able to access the variables a, b, c, d from here
    		}
    		else
    		{
    			Console.WriteLine("Please Input A Valid Value!");
    		}
    }				
    
    public static string GetDBData(string an)
    {
    	string a;
    	string b;
    	string c;
    	string d;
    
    	using (var ctx = new ClientContext(url))
    	{
    		var web = ctx.Web;
    
    		List list = web.Lists.GetByTitle(site);
    		CamlQuery camlQuery = new CamlQuery();
    		camlQuery.ViewXml = string.Format("");
    
    		//ListItemCollection items = list.GetItems(camlQuery);
    
    		//ctx.Load(list);
    		//ctx.Load(items);
    		//ctx.ExecuteQuery();
    
    		var listItemCollection = list.GetItems(camlQuery);
    
    		ctx.Load(listItemCollection, eachItem => eachItem.Include(item => item, item => item["a"], item => item["b"], item => item["c"], item => item["d"]));
    
    		ctx.ExecuteQuery();
    
    		foreach(ListItem listItem in listItemCollection)
    		{
    		  a = listItem["a"].ToString();
    		  b = listItem["b"].ToString();
    		  c = listItem["c"].ToString();
    		  d = listItem["d"].ToString();
    		}
    
    	}
    }

    Tuesday, August 14, 2018 7:44 PM

Answers


  • I modified my syntax to the below, but on the return statement I get a compile error of unassigned local variable for a,b,c,d even though they are being assigned in the for block?

    public Tuple<string, string, string, string> GetDBData(string an)
    {
    	string a;
    	string b;
    	string c;
    	string d;
    
    	using (var ctx = new ClientContext(url))
    	{
                     ...
                    
    		foreach(ListItem listItem in listItemCollection)
    		{
    		  a = listItem["a"].ToString();
    		  b = listItem["b"].ToString();
    		  c = listItem["c"].ToString();
    		  d = listItem["d"].ToString();
    		}
    		return Tuple.Create(a, b, c, d);
    	}
    }

    Possibly because the compiler can't be certain that there will always be
    items in the collection, so it's possible that the foreach block will
    never get executed. To silence the "unassigned" error, give the strings
    an initial value. e.g. -

    string a = "";
    string b = "";
    string c = "";
    string d = "";

    - Wayne


    • Edited by WayneAKing Tuesday, August 14, 2018 9:04 PM
    • Marked as answer by IndigoMontoya Thursday, August 16, 2018 7:17 PM
    Tuesday, August 14, 2018 9:03 PM

All replies

  • Hi Indigo,

    You try using Tuple to return the multiple variables from your method. Please look int Tuple Class


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Tuesday, August 14, 2018 7:48 PM
    Moderator
  • Hi Indigo,

    You try using Tuple to return the multiple variables from your method. Please look int Tuple Class


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    I like this idea...

    I modified my syntax to the below, but on the return statement I get a compile error of unassigned local variable for a,b,c,d even though they are being assigned in the for block?

    public Tuple<string, string, string, string> GetDBData(string an)
    {
    	string a;
    	string b;
    	string c;
    	string d;
    
    	using (var ctx = new ClientContext(url))
    	{
    		var web = ctx.Web;
    		List list = web.Lists.GetByTitle(site);
    		CamlQuery camlQuery = new CamlQuery();
    		camlQuery.ViewXml = string.Format("");
    		var listItemCollection = list.GetItems(camlQuery);
    		ctx.Load(listItemCollection, eachItem => eachItem.Include(item => item, item => item["a"], item => item["b"], item => item["c"], item => item["d"]));
    
    		ctx.ExecuteQuery();
    		foreach(ListItem listItem in listItemCollection)
    		{
    		  a = listItem["a"].ToString();
    		  b = listItem["b"].ToString();
    		  c = listItem["c"].ToString();
    		  d = listItem["d"].ToString();
    		}
    		return Tuple.Create(a, b, c, d);
    	}
    }

    Tuesday, August 14, 2018 8:19 PM
  • You're playing like you are doing something with a Web solution. What would be retuned out of a Web solution would be a type or a type in a collection. The type would be a DTO that's what you should be returning is an object, a container object. 

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

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

    You could also return read only anaymous types that can only be used in the local scope of a method.

    https://www.c-sharpcorner.com/UploadFile/ff2f08/anonymous-types-in-C-Sharp/

    Learn how to use dynamic.

    https://www.filipekberg.se/2011/10/06/playing-with-anonymous-types-in-c/

    https://www.c-sharpcorner.com/uploadfile/ashish_2008/dynamic-type-in-C-Sharp-4-0/

    • Proposed as answer by WayneAKing Tuesday, August 14, 2018 8:52 PM
    • Unproposed as answer by WayneAKing Tuesday, August 14, 2018 8:53 PM
    Tuesday, August 14, 2018 8:51 PM

  • I modified my syntax to the below, but on the return statement I get a compile error of unassigned local variable for a,b,c,d even though they are being assigned in the for block?

    public Tuple<string, string, string, string> GetDBData(string an)
    {
    	string a;
    	string b;
    	string c;
    	string d;
    
    	using (var ctx = new ClientContext(url))
    	{
                     ...
                    
    		foreach(ListItem listItem in listItemCollection)
    		{
    		  a = listItem["a"].ToString();
    		  b = listItem["b"].ToString();
    		  c = listItem["c"].ToString();
    		  d = listItem["d"].ToString();
    		}
    		return Tuple.Create(a, b, c, d);
    	}
    }

    Possibly because the compiler can't be certain that there will always be
    items in the collection, so it's possible that the foreach block will
    never get executed. To silence the "unassigned" error, give the strings
    an initial value. e.g. -

    string a = "";
    string b = "";
    string c = "";
    string d = "";

    - Wayne


    • Edited by WayneAKing Tuesday, August 14, 2018 9:04 PM
    • Marked as answer by IndigoMontoya Thursday, August 16, 2018 7:17 PM
    Tuesday, August 14, 2018 9:03 PM