How to send and retrieve from methods
I am trying to send and receive values ββsince I have my object oriented code, in Addition.xaml. I am trying to send a variable AddLevel, which is an int, to Addition.xaml I have:
private void pageRoot_Loaded(object sender, RoutedEventArgs e)
{
Methods.AddLevels(AddLevel, Question1,Question2, Answer);
QuestionText1.Text = System.Convert.ToString(Question1);
QuestionText2.Text = System.Convert.ToString(Question2);
}
In this method, I am trying to send an int AddLevel variable to the Methods class to determine what to do in the Methods class that I have:
public static int AddLevels(int AddLevel, int Question1, int Question2, int Answer)
{
Random rnd = new Random();
if(AddLevel == 0 || AddLevel == 1)
{
Question1 = rnd.Next(0, 10);
Question2 = rnd.Next(0, 10);
Answer = Question1 + Question2;
}
return Question1;
return Question2;
return Answer;
}
Just to summarize, I am trying to send AddLevel from Addition.xaml to AddLevels method in Methods class, then I am trying to get Question1, Question2 and Answer from this method. How should I do it?
source to share
One option is to use Tuple
:
private void pageRoot_Loaded(object sender, RoutedEventArgs e)
{
Tuple<int, int, int> result = Methods.AddLevels(AddLevel, Question1, Question2, Answer);
QuestionText1.Text = System.Convert.ToString(result.Item1);
QuestionText2.Text = System.Convert.ToString(result.Item2);
}
public static Tuple<int, int, int> AddLevels(int addLevel, int question1, int question2, int answer)
{
if (addLevel == 0 || addLevel == 1)
{
Random rnd = new Random();
question1 = rnd.Next(0, 10);
question2 = rnd.Next(0, 10);
answer = question1 + question2;
}
return Tuple.Create(question1, question2, answer);
}
Another option, but less recommended, is to use the keyword ref
:
private void pageRoot_Loaded(object sender, RoutedEventArgs e)
{
Methods.AddLevels(AddLevel, ref Question1, ref Question2, ref Answer);
QuestionText1.Text = System.Convert.ToString(Question1);
QuestionText2.Text = System.Convert.ToString(Question2);
}
public void AddLevels(int addLevel, ref int question1, ref int question2, ref int answer)
{
if (addLevel == 0 || addLevel == 1)
{
Random rnd = new Random();
question1 = rnd.Next(0, 10);
question2 = rnd.Next(0, 10);
answer = question1 + question2;
}
}
source to share
Create your method to return Dictionary:
public static Dictionary<string, int> AddLevels(int AddLevel, int Question1, int Question2, int Answer)
{
Random rnd = new Random();
Dictionary<string, int> dic = new Dictionary<string,int>();
if(AddLevel == 0 || AddLevel == 1)
{
Question1 = rnd.Next(0, 10);
Question2 = rnd.Next(0, 10);
Answer = Question1 + Question2;
}
dic.Add("Question1", Question1);
dic.Add("Question2", Question2);
dic.Add("Answer", Answer);
return dic;
}
If you want to accept the value of Question1, Question2, Answer, just write it like this:
Dictionary<string, int> dic = AddLevels(AddLevel, Question1, Question2);
int question1 = dic["Question1"]; // this will return the value for Question1.
One of the requirements for a dictionary is Keys
to be unique, you must guarantee it. In your case, the keys: "Question1", "Question2", "Answer"
.
You must write parameter names starting with a small letter: addLevel, question1, question2
etc.
source to share
I could be wrong, but it looks like you are overcomplicating things to avoid binding? AddLevels looks like it's easily part of the DataContext?
SomePage.xaml
<TextBox x:Name="QuestionText1" Text={Binding Question1} />
<TextBox x:Name="QuestionText2" Text={Binding Question2} />
SomePage.xaml.cs
public partial class SomePage : UserControl
{
public SomePage()
{
InitializeComponent();
this.DataContext = new SomePageViewModel();
}
private void pageRoot_Loaded(object sender, RoutedEventArgs e)
{
((SomePageViewModel)this.DataContext).AddLevels(AddLevel);
}
}
SomePageViewModel.cs
public class SomePageViewModel : INotifyPropertyChanged
{
public int Answer
{
get { return _answer; }
set
{
if (_answer != value)
{
_answer = value;
OnPropertyChanged("Answer");
}
}
}
public int Question1
{
get { return _question1; }
set
{
if (_answer != value)
{
_question1 = value;
OnPropertyChanged("Question1");
}
}
}
public int Question2
{
get { return _question2; }
set
{
if (_question2 != value)
{
_question2 = value;
OnPropertyChanged("Question2");
}
}
}
public void AddLevels(int addLevel)
{
Random rnd = new Random();
if(addLevel == 0 || addLevel == 1)
{
Question1 = rnd.Next(0, 10);
Question2 = rnd.Next(0, 10);
Answer = Question1 + Question2;
}
}
}
source to share