Solved

Return 2 values at once

Posted on 2011-03-21
6
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 30

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 30

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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 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
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 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