Solved

Return 2 values at once

Posted on 2011-03-21
6
202 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now