Solved

Return 2 values at once

Posted on 2011-03-21
6
205 Views
Last Modified: 2012-05-11
Hi
Can anyone show me how to return more than one value from the following code, and display it in 2 different places.
Regards
George
public static string DisplaySeasonsImageLeft()
        {
            string SeasonImageLeft = "";
            string SeasonImageLeftAltText = "";
            switch (MonthOfYear)
            {
                case 1:
                case 2:
                case 3:
                    if (IsDS == true)
                    {
                        SeasonImageLeft = "/Content/Images/SpringSmallPNG.png";
                        SeasonImageLeftAltText = "Spring Scenes";
                    }
                    else
                    {
                      SeasonImageLeft = "/Content/Images/WinterSmallPNG.png";
                      SeasonImageLeftAltText = "Winter Scenes";
                    }
                    break;
                case 4:
                case 5:
                case 6:
                        SeasonImageLeft = "/Content/Images/SummerSmallPNG.png";
                        SeasonImageLeftAltText = "Summer Scenes";
                    break;
                case 7:
                case 8:
                case 9:
                        SeasonImageLeft = "/Content/Images/AutumnSmallPNG.png";
                        SeasonImageLeftAltText = "Autumn Scenes";
                    break;
                case 10://30 oct clock go back
                case 11:
                case 12:
                        SeasonImageLeft = "/Content/Images/WinterSmallPNG.png";
                        SeasonImageLeftAltText = "Winter Scenes";
                    break;
                default:
                        SeasonImageLeft = "/Content/Images/DeafaultSmallPNG.png";
                        SeasonImageLeftAltText = "Default Text";
                    break;
            }

            return SeasonImageLeft;
        }

Open in new window

0
Comment
Question by:WebsiteDesign
6 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35185619
The best way is to use structures:

http://msdn.microsoft.com/en-us/library/ah19swz4(v=vs.71).aspx

Let me few moments - I'll show
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 35185666
Check this - should be simple
public struct TwoStrings
    {
        public string SeasonImageLeft, SeasonImageLeftAlt;

        public TwoStrings(string s1, string s2)
        {
            SeasonImageLeft = s1;
            SeasonImageLeftAlt = s2;
        }
    }
================================================
    public partial class MyClass
    {
        public static string DisplaySeasonsImageLeft()
        {
            string SeasonImageLeft = "";
            string SeasonImageLeftAltText = "";
            switch (MonthOfYear)
            {
                case 1:
                case 2:
                case 3:
                    if (IsDS == true)
                    {
                        SeasonImageLeft = "/Content/Images/SpringSmallPNG.png";
                        SeasonImageLeftAltText = "Spring Scenes";
                    }
                    else
                    {
                        SeasonImageLeft = "/Content/Images/WinterSmallPNG.png";
                        SeasonImageLeftAltText = "Winter Scenes";
                    }
                    break;
                case 4:
                case 5:
                case 6:
                    SeasonImageLeft = "/Content/Images/SummerSmallPNG.png";
                    SeasonImageLeftAltText = "Summer Scenes";
                    break;
                case 7:
                case 8:
                case 9:
                    SeasonImageLeft = "/Content/Images/AutumnSmallPNG.png";
                    SeasonImageLeftAltText = "Autumn Scenes";
                    break;
                case 10://30 oct clock go back
                case 11:
                case 12:
                    SeasonImageLeft = "/Content/Images/WinterSmallPNG.png";
                    SeasonImageLeftAltText = "Winter Scenes";
                    break;
                default:
                    SeasonImageLeft = "/Content/Images/DeafaultSmallPNG.png";
                    SeasonImageLeftAltText = "Default Text";
                    break;
            }

            return new TwoStrings(SeasonImageLeft, SeasonImageLeftAltText);
        } 
        
        
 // just an example

        public void DoSomething()
        {
            TwoStrings ts = MyClass.DisplaySeasonsImageLeft();
            this.textBox1.Text = ts.SeasonImageLeft;
            this.textBox2.Text = ts.SeasonImageLeftAlt;
        }

Open in new window

0
 
LVL 7

Expert Comment

by:dimaj
ID: 35185987
You could also make an 'out' parameter.

i.e.
public bool MyFunct(string myStr) {
  myStr = "Hello";
  return true;
}

public void Tester () {
  string str = "";
  bool res = MyFunct(str);
  // now res = true and str = "Hello";
}
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35186398
Yeah, that's possible.

However, this is not an "out" parameter, strictly speaking. It's just an object send by reference. Out parameters is a bit different story. See, for example, boolean.TryParse method as an example of two values from one method.

I believe that in this case structures are more clever option. If you need to return two same type and use values - use structures. Most common example is Point structures, it has two coordinates, X and Y. And because they are to be used in a similar fashion it would be not wise to return in different way (one - return value from a nethod, another  - "out" parameter").
0
 
LVL 19

Expert Comment

by:Rikin Shah
ID: 35186656
Concatenate your return values with "|" (pipes) and split at the other hand and show differently.
0
 

Author Comment

by:WebsiteDesign
ID: 35187986
Hi

I'm getting the following error:

Error      2      Cannot implicitly convert type 'Web.Domain.Models.Global.TwoStrings' to 'string'      

Regards
George
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Linq asp.net mvc 13 17
Dynamics CRM Plugin Query TotalRecordCount returning -1 13 46
Change to file doesn't show up 16 57
reading excel file in .net 2 19
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question