Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJared Salwei
11,974 PointsC loop questions
I am building this app for school with C#. I don't understand why we are using the for loop three time in each button click. The app work but I am wondering if I am supposed to just use the loop once instead of in all three buttons? Right now if I hit clear, it does clear the box, but when I select a different course it shows the course and all the past ones were clicked? Any feedback would be great.
namespace App1 { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page { public static string VarOutput { get; set; } //place property within the class
public MainPage()
{
this.InitializeComponent();
txtBoxRas.Text = "Rasmussen College";
txtBoxRas.FontSize = 14;
txtBoxRas.Foreground = new SolidColorBrush(Windows.UI.Colors.Red);
}
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
ProcessCalc();
}
private void ProcessCalc()
{
Int32 Var1 = Convert.ToInt32(txtBoxInput1.Text) + Convert.ToInt32(txtBoxInput2.Text);
txtBoxDisplay.Text = Convert.ToString(Var1);
}
private void btnCourse1_Click(object sender, RoutedEventArgs e)
{
string[] names = new string[3] { "COP3488C,", "UWP1,", "This course is mobile app development." };
for (int i = 0; i < names.Length; i++)
{
VarOutput = VarOutput + names[i] + " ";
}
txtBoxCourse.Text = VarOutput;
var dialog = new MessageDialog(VarOutput);
}
private void btnCourse2_Click(object sender, RoutedEventArgs e)
{
string[] names = new string[3] { "AWC8944C,", "UWP2,", "This course is DataBase." };
for (int i = 0; i < names.Length; i++)
{
VarOutput = VarOutput + names[i] + " ";
}
txtBoxCourse.Text = VarOutput;
var dialog = new MessageDialog(VarOutput);
}
private void btnCourse3_Click(object sender, RoutedEventArgs e)
{
string[] names = new string[3] { "COP3499B,", "UWP2,", "This course is Web Design." };
for (int i = 0; i < names.Length; i++)
{
VarOutput = VarOutput + names[i] + " ";
}
txtBoxCourse.Text = VarOutput;
var dialog = new MessageDialog(VarOutput);
}
private void clear_Click(object sender, RoutedEventArgs e)
{
txtBoxCourse.Text = String.Empty;
}
}
}
Jared Salwei
11,974 PointsIt does work right now this is what we had to do.
Next, write three new C# arrays (or lists) of DataObjects and populate the DataObjects with several course numbers, course names, and course descriptions from your degree program. Then add several button controls to the MainPage.xaml and show each course number in the button controls. (Note: In your final app, you will need to provide a data list of all 14 courses in the upper division of your program, but in this week you should only add data for two or three courses as you will want to move the data to a new page in upcoming weeks.)
To summarize, you are required to: Create three new DataObjects (arrays or lists) in the MainPage.xaml.cs file Assign each of your course numbers, course names and course descriptions to the DataObjects Show the course number and course name in each button
Next you will write C# code that dynamically displays the course descriptions back to the screen when you click on the course number button. There are a number of ways to accomplish this (e.g. popup message boxes, new pages, or tool tips). Write the code to get the appropriate information from the DataObject you created in Step #2.
Steven Parker
231,198 PointsDon't forget to add the formatting marks to your code.
1 Answer
Taylor Espejo
3,939 PointsHi how you going ? I'm also a little confused and I havn't touched on c# for a little while now but I do believe the three for loops are essential to the way you're coding that app. Of course it could be done differently; but this works fine.
As far as your second issue - I believe when you clear you're only getting rid of that current" item ? You may need a method that clears the item from the array in order to completely remove past items from showing up.
Let me know if this helps you at all - or if it was on the right track :)
Steven Parker
231,198 PointsSteven Parker
231,198 PointsI'm confused, is the function you describe what the program is intended to do, and you're just looking for an explanation? Or is the program not working right and you want to fix it?
If the latter, what is the intended functionality?
Also please check the Markdown Cheatsheet pop-up below the "Add an Answer" section for instructions on formatting code for proper display.