Solved

Return 2 values at once

Posted on 2011-03-21
6
204 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

832 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