Solved

Return 2 values at once

Posted on 2011-03-21
6
203 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

19 Experts available now in Live!

Get 1:1 Help Now