I want to display only the quote in the quote textbox but I also want to display the quotee name in the quotee textbox the problem is I can only return one value if I return both values they kinda go together in the quote textbox
public static string DisplayQuote()
{
Dictionary<string, string> quotes = new Dictionary<string, string>();
quotes.Add("Dr. Seuss",
"\"You’re off to great places, today is your day. Your mountain is waiting, so get onyour way.\"");
quotes.Add("A.A. Milne",
"\"You’re braver than you believe, and stronger than you seem, and smarter than you think.\"");
quotes.Add("Willie Nelson", "\"Once you replace negative thoughts with positive ones, you’ll start having positive results.\"");
quotes.Add("Les Brown",
"\"In every day, there are 1,440 minutes. That means we have 1,440 daily opportunities to make a positive impact.\"");
quotes.Add("Dr Seuss",
"\"When you are enthusiastic about what you do, you feel this positive energy. It’s very simple.\"");
int index = 0;
for (int i = 0; i < 5; i++)
{
index = randomQuote.Next(quotes.Count);
}
_quote = quotes.Values.ElementAt(index).ToString();
_quotee = quotes.Keys.ElementAt(index).ToString();
return (_quote, _quotee).ToString();
}
My WPF:
namespace DailyQuotes
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DailyQuote daily = new DailyQuote();
txtQuotee.Text = daily.Quoteee;
txtPeriod.Text = daily.GetPeriod();
txtQuote.Text = daily.DisplayQuote();
txtTime.Text = daily.Time.ToString("h:mm tt");
}
}
}
Why don't you just return the tuple instead of calling ToString
on it?
public static (string quote, string quotee) DisplayQuote()
{
....
return (_quote, _quotee);
}
Then you can access the two returns like this:
DailyQuote daily = new DailyQuote();
txtQuotee.Text = daily.quoeee;
txtQuote.Text = daily.quote;
This requires named tuples , which are available from C# 7.0 and onwards. If you don't have C# 7.0 you'll need to use unnamed Tuples like so;
public Tuple<string,string> DisplayQuote()
{
...
return new Tuple<string,string>(_quote, _quotee);
}
var tuple = DisplayQuote();
txtQuotee.Text = tuple.Item1;
txtQuote.Text = tuple.Item2;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.