TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Victor Dallarosa
NA
6
1.1k
IComparable in WPF
Mar 30 2020 3:08 PM
Hello Guys,
I need to implement a Search button that will compare the value from my Text box with the values in the arrays and if they match, it returns the index, otherwise it will return -1. I implemented the random arrays of doubles and ints already as well as all the interface and buttons. It is a must to use IComparable and CompareTo() but I have no idea on how to implement it. I tried implementing a Search method, but it is not working, I don't know how to call it in the Search Button Click_Event.
That's what I have so far:
public partial class MainWindow : Window
{
int[] numb = new int[6];
double[] numb2 = new double[6];
public MainWindow()
{
InitializeComponent();
}
//Create Int
private void Button_Click(object sender, RoutedEventArgs e)
{
resultsBox.Items.Clear();
resultsBox.Items.Add("Index Value\n");
Random rnd = new Random();
for (int i = 0; i < 6; i++)
{
numb[i] = rnd.Next(0, 999);
string m = i.ToString() + "\t" + numb[i].ToString();
resultsBox.Items.Add(m);
}
}
//Create Double
private void CreateDouble_Click(object sender, RoutedEventArgs e)
{
resultsBox.Items.Clear();
resultsBox.Items.Add("Index Value\n");
Random rnd = new Random();
for (int i = 0; i < 6; i++)
{
numb2[i] = Math.Round(rnd.NextDouble() * (999), 2);
string m = i.ToString() + "\t" + numb2[i].ToString();
resultsBox.Items.Add(m);
}
}
//Search
private void SearchButton_Click(object sender, RoutedEventArgs e)
{
}
static int Search<T>(T[] dataArray, T searchKey) where T : IComparable<T>
{
//Iterate through the array.
for (int iter = 0; iter < dataArray.Length; iter++)
{
//Check if the element is present in the array.
if (dataArray[iter].CompareTo(searchKey) == 0)
{
//Return the index if the element is present in the array.
return iter;
}
}
//Otherwise return the index -1.
return -1;
}
}
}
Thank you!
Reply
Answers (
1
)
How to Create Setup/Installer exe file from Visual Studio C#
Syntax to send text to a RichTextBox