|
thanks got it working.
Dictionary<int, int=""> dic = new Dictionary<int, int="">();<br />
<br />
for (int i = 0; i < myArray.Length; i++)<br />
{<br />
int value = myArray[i];<br />
if (dic.ContainsKey(value))<br />
{<br />
dic[value] += 1;<br />
}<br />
else<br />
{<br />
dic.Add(value, 1);<br />
}<br />
}<br />
<br />
int[] keys = new int[dic.Count]; <br />
int[] values = new int[dic.Count]; <br />
dic.Keys.CopyTo(keys, 0);<br />
dic.Values.CopyTo(values, 0);<br />
<br />
for (int i = 0; i < keys.Length; i++){<br />
Console.WriteLine("{0} \t : {1}", keys[i], values[i]);<br />
}</int,></int,>
|
|
|
|
|
Hi,
the easiest approach I know is a two-step algorithm:
1. sort the array or whatever collection you have; .NET knows how to do that.
2. iterate over the collection, and count how often an item is identical to the previous one.
BTW: if you are not allowed to modify (i.e. sort) the original collection, you will need
a helper collection, such as a HashTable/Dictionary, as others already explained.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Hi All,
I'm trying to get this working, I can't see any problems with the code but its just not working. Can anyone see what I'm doing wrong?
I have this in a class:
public static class Enums
{
public enum ApplicationView
{
BrowserView = 0,
DataBaseView = 1,
ConfigurationView = 2
}
}
With this in my usercontrol:
public sealed class ApplicationViewEventArgs : EventArgs
{
private Enums.ApplicationView _applicationView;
public Enums.ApplicationView ApplicationViewStatus
{
get
{
return _applicationView;
}
set
{
_applicationView = value;
}
}
public ApplicationViewEventArgs(Enums.ApplicationView ApplicationViewStatus) : base()
{
_applicationView = ApplicationViewStatus;
}
}
public partial class Navigator : UserControl
{
public delegate void ApplicationViewChanged(object sender, ApplicationViewEventArgs e);
public event ApplicationViewChanged ApplicationViewStatusChanged;
public Navigator()
{
this.InitializeComponent();
}
private void btnBrowseView_Click(object sender, RoutedEventArgs e)
{
if (ApplicationViewStatusChanged != null)
ApplicationViewStatusChanged(this, new ApplicationViewEventArgs(Enums.ApplicationView.BrowserView));
}
private void btnDatabaseView_Click(object sender, RoutedEventArgs e)
{
if (ApplicationViewStatusChanged != null)
ApplicationViewStatusChanged(this, new ApplicationViewEventArgs(Enums.ApplicationView.DataBaseView));
}
}
And this in my main window code:
private void Navigator_ApplicationViewStatusChanged(object o, ApplicationViewEventArgs e)
{
lyrBrowserView.Visibility = Visibility.Hidden;
lyrDatabaseView.Visibility = Visibility.Hidden;
switch (e.ApplicationViewStatus)
{
case Enums.ApplicationView.BrowserView:
lyrBrowserView.Visibility = Visibility.Visible;
break;
case Enums.ApplicationView.DataBaseView:
lyrDatabaseView.Visibility = Visibility.Visible;
break;
}
}
When a button is clicked nothing is happening. The click event executes but it never seems to execute the ApplicationViewEventArgs meaning ApplicationViewStatusChanged is always null ...
Cheers,
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Hi,
Jammer wrote: public event ApplicationViewChanged ApplicationViewStatusChanged;
You want this ApplicationViewStatusChanged to be different from null? where is the line of code
that assigns a value to it?
Hint: you need the += operator
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Ahhhh ... ok, off to look into!
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
This confused me when I first started writing events! If an event has not been subscribed to, it will be null. Simply subscribe to the event, as Luc said, and then it'll be raised.
Dave
|
|
|
|
|
Yeah, i'm seriously confused at the moment ... i'm getting all sorts of errors now.
I've moved this:
Navigator.ApplicationViewStatusChanged += new ApplicationViewEventArgs.ApplicationViewChanged(Navigator_ApplicationViewStatusChanged);
into my main window code behind, now its saying:
An object reference is required for the non-static field, method, or property 'SampleSort.Presentation.Navigator.ApplicationViewStatusChanged'
and:
The type name 'ApplicationViewChanged' does not exist in the type 'SampleSort.Presentation.ApplicationViewEventArgs'
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
am I on the right track with this?
public Navigator()
{
this.InitializeComponent();
Navigator.ApplicationViewStatusChanged += new Navigator.ApplicationViewChanged(sender, ApplicationViewEventArgs e);
}
do I need to register one of these for both buttons? Its just throwing loads of errors at the moment tho.
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Jammer wrote: Navigator.ApplicationViewStatusChanged += new Navigator.ApplicationViewChanged(sender, ApplicationViewEventArgs e);
Will need to go in the class where your usercontol is instanciated, not in the Navigator class itself, so the containing class can listen for its events.
Edit: It will probably look something like this:
navigator1.ApplicationViewStatusChanged += new Navigator.ApplicationViewChanged(navigator1_ApplicationViewStatusChanged);
void navigator1_ApplicationViewStatusChanged(object sender, Navigator.ApplicationViewEventArgs e)
{
}
Dave
|
|
|
|
|
oh btw the user control isn't created using anything like navigator navigator1 = new navigator(); as that is in XAML ... it just gets launched at runtime in the mainwindow() ...
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
it makes no difference, just refer to the object by the name you gave it in the xaml. Intellisense should take care of the rest for you.
Dave
|
|
|
|
|
Have tried that and its not showing me anything called Navigator1 (i've tried giving it just Name="Navigator1" and x:Name="Navigator1")
Man this is starting to hurt my head.
I know I'm being a pain but can you just review this for me:
public SampleSortMainWindow()
{
SetSystemConstants.SetConstants();
InitializeComponent();
Navigator1.ApplicationViewStatusChanged += new Navigator.ApplicationViewChanged(Navigator1_ApplicationViewStatusChanged);
lyrBrowserView.Visibility = Visibility.Visible;
lyrDatabaseView.Visibility = Visibility.Hidden;
}
the XAML is:
<UserControl<br />
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"<br />
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"<br />
xmlns:d="http://schemas.microsoft.com/expression/blend/2006"<br />
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"<br />
mc:Ignorable="d"<br />
Name="Navigator1"<br />
x:Class="SampleSort.Presentation.Navigator"<br />
Height="32" Width="64" ClipToBounds="True"><br />
<Grid>
And this is in the Navigator:
public sealed class ApplicationViewEventArgs : EventArgs
{
private Enums.ApplicationView _applicationView;
public Enums.ApplicationView ApplicationViewStatus
{
get
{
return _applicationView;
}
}
public ApplicationViewEventArgs(Enums.ApplicationView ApplicationViewStatus) : base()
{
_applicationView = ApplicationViewStatus;
}
}
public partial class Navigator : UserControl
{
public delegate void ApplicationViewChanged(object sender, ApplicationViewEventArgs e);
public event ApplicationViewChanged ApplicationViewStatusChanged;
public Navigator()
{
InitializeComponent();
}
private void btnBrowseView_Click(object sender, RoutedEventArgs e)
{
if (ApplicationViewStatusChanged != null)
ApplicationViewStatusChanged(this, new ApplicationViewEventArgs(Enums.ApplicationView.BrowserView));
}
private void btnDatabaseView_Click(object sender, RoutedEventArgs e)
{
if (ApplicationViewStatusChanged != null)
ApplicationViewStatusChanged(this, new ApplicationViewEventArgs(Enums.ApplicationView.DataBaseView));
}
}
The current error is Navigator1 does not exist in the current context. I added a breakpoint and looked at how things are initialised and the Navigator1 is the first thing to be initialized after the Main screen initialize so it should be able to find Navigator1 shouldn't it?
Thanks for the help Dave.
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Done a very simple example that you may be able to learn from so you can fix your problem.
I created a default WPF app (WpfApplication1) then created a user control (UserControl1).
In UserControl1 (in the namespace but outside the class) I created a delegate
public delegate void UserControlDelegate(object sender, EventArgs e);
and (inside the class) an event public event UserControlDelegate UserControlEvent;
then added a public property that when set raises the event with this:
if (null != UserControlEvent)
UserControlEvent(this, EventArgs.Empty);
Then I added this to Window1.xaml xmlns:UserControlNamespace="clr-namespace:WpfApplication1"
and then in the Grid block
<UserControlNamespace:UserControl1 x:Name="MyUsercontrol" Width="100" Height="100"></UserControlNamespace:UserControl1>
then changed the Window1 class in Window1.xaml.cs to
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
MyUsercontrol.UserControlEvent += new UserControlDelegate(MyUsercontrol_UserControlEvent);
}
void MyUsercontrol_UserControlEvent(object sender, EventArgs e)
{
Console.WriteLine("Event Raised");
}
}
This is not exactly how I'd do it in production (I'd use protected virtual methods to raise the event etc, etc... ) but it should get you started.
Dave
|
|
|
|
|
Wow, thanks for this dave ... much appreciated. Will have a look at this in comparison to my problem.
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Oh man ... what a doughnut I have been ...
Basically, the whole deal was that I was giving the Navigator an x:Name="" in the Navigator xaml NOT the mainwindow.xaml file ... its all working as expected now ...
Thank you Dave!
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
No problem
Dave
|
|
|
|
|
Hi Jammer,
I have some suggestions and comments:
1.
if a subject is not clear to you, my first advice is to read the documentation (use Google,
and look for links with MSDN or MSDN2 in them)
2.
since documentation often is rather short and lacks contextual information, I always advice
to have a good tutorial at hand, and read the relevant chapter.
3.
I did a short article on events and delegates; it does not explain all of it, it does providce
some examples and gives you some hints.
4.
When you have compile errors, look at them carefully and solve them; pay attention especially
to the first error message, and try to get rid of that one first. Then recurse until none are
left. Don't start adding unrelated code (and more errors) until you have fixed the ones you
already have.
5.
Also pay attention to the compiler warnings; a good programmer knows every warning is
a candidate for a run-time problem later on, so get rid of them too.
6.
We are not in the spoon feeding business. As a programmer you are supposed to search, read around,
try, correct and test; and when stuck, to ask specific questions while providing clear information.
"the compiler is throwing loads of errors" is not informative.
7.
keep it up!
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
point taken ... sorry. I've been reading and trying various things but i've just been going round in circles so far and its just getting to me a bit now.
Jammer
Going where everyone here has gone before!
My Blog
|
|
|
|
|
Hi,
How does one take data from a datagrid and display it in an Excel spreadsheet? I have got data in a datagrid and would like to import it to a spreadsheet. Please help.
Mvelo Walaza
Developer
Telkom SA
|
|
|
|
|
|
Hi,
Thanks for the reply, but I am doing a Windows application. So how different is the code from this article to the one of a Windows application? Or is there an article that demonstrates how to import from a (Windows application) to an Excel spreadsheet?
Mvelo Walaza
Developer
Telkom SA
|
|
|
|
|
Walaza wrote: So how different is the code from this article to the one of a Windows application
since the control is the same, the code shouldnt differ a lot.
try out and ask again, if you get stuck.
|
|
|
|
|
What do I replace the ff with:
Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=FileName.txt"); etc
because 'Response.' is not known in a Windows application.
Mvelo Walaza
Developer
Telkom SA
|
|
|
|
|
I need to capture mouse right click in WinForms DataGridView column header. I tried
class MyDataGridView: DataGridView {
protected override void
OnColumnHeaderMouseClick(DataGridViewCellMouseEventArgs e) {
....
OnColumnHeaderMouseClick occurs when header is right or left-clicked by
mouse. DataGridViewCellMouseEventArgs does not provide
information which mouse button is pressed.
How to find when mouse is right-clicked in column header ?
Andrus.
Andrus
|
|
|
|
|
HI
i write two application with C#. at now i want to create an interaction between them.
what is best way to do this (create interaction between 2 application) ?
can we do this with interfaces ?
|
|
|
|