|
The object you pass to Monitor.Exit needs to be the same object that you passed to Monitor.Enter / Monitor.TryEnter .
When you call your Resize method, you're setting the array field to a new array instance. You then pass that new instance to Monitor.Exit , even though you've never passed it to Monitor.Enter .
The simplest solution would be to use a separate read-only object for all Monitor calls:
class SmartArray3
{
private readonly object _locker = new object();
...
public void Add(int value)
{
Monitor.Enter(_locker);
try
{
...
}
finally
{
Monitor.Exit(_locker);
}
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Good spot!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hi,
In an attempt to graduate into a more professional programmer, I'm trying to comment my code more and use classes more. I have a demo project working and sadly enough I'm not very well versed in creating/using my own classes but would like to change that.
I have a datagridview demo project that I've written with the idea that I will have multiple datagridviews in my final project, on multiple tabs. Instead of cutting and pasting and changing the code multiple times, it seems like my own class would benefit me greatly in this instance, where I could instantiate the same object multiple times.
If this is correct, can I have help creating a class out of this demo project? It's ~1000 lines of code, but I'll be happy to post it if that would help.
Thanks
|
|
|
|
|
I'm not planning on even looking at 1000 lines of your code - I don't have the time - but I'd start by looking at creating a UserControl[^] rather than a generic class - they display directly. And then either embedding a DataGridView or deriving from one. If you are planning on using it in tabs, I'd also consider creating another UserControl, derived from TabPage which encapsulated the DataGridView UserControl to make that easier to use as well.
How easy this is for you to do will depend on how you wrote the original code: it may be worth your creating a new project and "playing with it" until you get the idea before you start launching into moving existing code.
I use UserControls a lot - they help to contain code and simplify interfaces between modules. Worth looking at!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Thanks for the reply. I read the MS article, and I also found this video.
This video seems a little more understandable for me being new to this, as I can see it done, would this also be a good representation of what you are talking about?
https://www.youtube.com/watch?v=l5L_q_jI494[^]
|
|
|
|
|
Difficult to tell - his voice and the microphone "wind noise" was so annoying I couldn't watch that much!
Seriously, give it a try on a "new project" and see what happens. It really does help with simplifying interfaces between modules and speeds up development too.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
That means you didn't make it to 4:20 as about that point a random hammer starts banging the wall behind him ... or he is residing in a cheap motel, I'm not sure?
I've started with a new project and a user control class. I can't get the class to instantiate, but I will keep try and report back if I fail.
Thanks.
|
|
|
|
|
Build the project, and it should add the new control to the top of the toolbox. Drag and drop one onto your form, then look at the designer.cs file - it'll show you the code VS uses to instantiate it.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Ok, I tried the code below but it did not work and my basic class is at the bottom. Under designer.cs, should I be using some of the code under InitializeComponent()?
dataGridViewUserControl1 dgv1 = new dataGridViewUserControl1();
dgv1.ColumnCount = 25;
dgv1.RowCount = 20;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UserControlDemo
{
public partial class dataGridViewUserControl1 : UserControl
{
public dataGridViewUserControl1()
{
InitializeComponent();
}
private int columnCount;
public int ColumnCount
{
get { return columnCount; }
set { columnCount = value; }
}
private int rowCount;
public int RowCount
{
get { return rowCount; }
set { rowCount = value; }
}
private void UserControl1_Load(object sender, EventArgs e)
{
for (int i = 0; i < columnCount; i++)
{
dataGridView1.Columns[i].Name = "column " + i;
}
for (int i = 0; i < rowCount; i++)
{
string[] rowName = new string[] { "row" + i.ToString() };
dataGridView1.Rows.Add(rowName);
}
}
}
}
|
|
|
|
|
If you look at the designer file, you'll find some more code: of nothing else there will be a Controls.Add method call to add the new control instance to the forms Controls collection - that's the line which actually makes it display within the form. (For your app, it'll eventually be in the TabPage.Controls collection to make it a "child" of that control rather than the firm itself, but that's the advanced bit )
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
you´re graduating and you have no clue how to develop something more descent than this?
Don´t get me wrong, I like the fact you´re willing to improve, but when I was graduating I had to do way and way and way better then this...
|
|
|
|
|
I don't think graduating is used in an educational context in this instance, I think he is just trying to become (graduate to) a better developer.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Not from school, you misread. I'm self taught and trying to graduate to the next level.
|
|
|
|
|
OK apologies. In that case, best way to learn is go through a good book. Digest it from A to Z and do all the exercises starting with the "Hello World" program. Tutorials on the internet are not always that good.
Also look up encapsulation, inheritence and polymorphism, the corner stones for OO programming.
That should help you getting a good structure in your application.
Hope this helps.
|
|
|
|
|
Hello all,
may I ask you for a suggestion where can I find some information about following topic?
I am trying to do something like this:
In MS SQL I have two tables Objects, Attributes. I have relation one to many via object_id (one object can have multiple attributes).
I would like to have a listbox with Objects and a e.g. gridview with attributes for selected object. How can I achieve that?
So far I created a m_DataSet [DataSet] which contains records returned by inner join select.
I have a m_ObjectsBS [BindingSource]. I filled binding source as this:
m_ObjectsBS.DataSource = m_DataSet.Tables[0].DefaultView.ToTable(true, "ObjectName"); (in order to have distinct names there).
I filled listbox as this:
m_lbxObjects.DataSource = m_ObjectsBS;
m_lbxObjects.ValueMember = "ObjectName";
m_lbxObjects.DisplayMember = "ObjectName";
To fill a gridview I can use m_DataSet.Tables[0] as for a new binding source and it is done?
Thanks a lot and I am sorry if I am creating a duplicated thread.
|
|
|
|
|
for example i have
[System.Flags]
public enum PrescriptionTypePrint
{
PrescriptionOnly = 1,
InstructionOnly = 2,
Both = PrescriptionOnly | InstructionOnly
}
how i could make it simplify replacing the || to | on the code below
PrescriptionTypePrint pp = PrescriptionTypePrint.Both;
if (pp == PrescriptionTypePrint.PrescriptionOnly || pp == PrescriptionTypePrint.Both)
{
}
becuase i think this would not work
if (pp == (PrescriptionTypePrint.PrescriptionOnly | PrescriptionTypePrint.Both))
{
}
thank you so much for the some one who shed me some light
|
|
|
|
|
I would do this:
[System.Flags]
public enum PrescriptionTypePrint
{
PrescriptionOnly = 1,
InstructionOnly = 2,
}
and then:
if (pp == (PrescriptionTypePrint.PrescriptionOnly | PrescriptionTypePrint.InstructionOnly))
{
}
I think that should work.
In any case using "both" as naming convention is a bit confusing. I'm also not sure about the behaviour of the both definition in the enum definition.
|
|
|
|
|
i dont know if using the name "both" is the right name to use,
but it will act as it has the value of "PrescriptionOnly" and "InstructionONly"
|
|
|
|
|
If you think of it in terms of binary:
PrescriptionOnly = 01
InstructionOnly = 10
Both = 11
And you want either PrescriptionOnly or Both:
01 or 11
So, of the four possible states the variable can be in, you want two:
None 00 X
PrescriptionOnly 01 Y
InstructionOnly 10 X
Both 11 Y
So all you have to do is:
if ((pp & PrescriptionTypePrint.PrescriptionOnly) != 0)
{
...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Use another binary operator
if( (pp & PrescriptionTypePrint.Both) != 0)
{
} This will binary-AND two integer operands. The result will equal zero if the oparands don't share a common bit set to '1'. Therefore 'pp' will not result in the expression to equal zero whenever any one of the bits of 'Both' are set in 'pp'.
Ciao,
luker
|
|
|
|
|
Um...that'll also execute it for the InstructionOnly case, won't it?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Yep, it will.
Your questioning seems like that's not what you wanted. Looking again at what you wrote, I think that's what you need:
if( ( pp & PrescriptionOnly ) != 0 )
{
}
Ciao,
luker
|
|
|
|
|
Um...you mean what the OP wanted?
That is what I had already suggested...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Sorry, Griff. Yes, I meant the OP.
And Wow, your answer is exhaustive. I'll give it an up.
Ciao,
luker
|
|
|
|
|
opsss, I miss the conversation, sorry i was out for a few days.
By the way THANK YOU SO MUCH for the suggestions, surely I will try it.
|
|
|
|