|
Hello everyone,
I am a freshman in C# and stuck with a stupid problem.
class ProjectDoc
{
private System.Windows.Forms.Panel panelProjectDoc;
this.panelProjectDoc = new System.Windows.Forms.Panel();
this.Controls.Add(this.panelProjectDoc);
public Point myfunc(Point dropPoint)
{
int X_snap = 0;
int Y_snap = 0;
return new Point(X_snap, Y_snap);
}
private void Filter_Click(object sender, EventArgs e)
{
BuildBlock buildBlock = new BuildBlock();
this.panelProjectDoc.Controls.Add(buildBlock);
buildBlock.Location = myfunc(new Point(900, 400));
}
}
class BuildBlock : UserControl
{
private void BuildBlock_MouseUp(object sender, MouseEventArgs e)
{
Point p = new Point(0, 0);
Point q = new Point(0, 0);
p = this.Location;
q = (Parent as ProjectDoc).myfunc(p);
this.Location = q;
}
}
Above code gives run time error when mouseUp event occurs.
[ System.NullReferenceException ]
However, if i replace the line :
this.panelProjectDoc.Controls.Add(buildBlock);
with
this.Controls.Add(buildBlock);
it does not give error (however, this way is not what i want )
any idea how to eliminate the error ?
- me
|
|
|
|
|
When it says "Null reference error" it means that you are trying to refer to an object that has not been set. In this case (and assuming there is no code you missed) it would be the Parent property. Since you have used (Parent as ProjectDoc) I can only assume that the parent is not a ProjectDoc derived object.
Firstly, whenever you use as make a habit of checking the result:
MyClass mc = obj as MyClass;
if (mc != null)
{
...
} It will save you a lot of grief later on!
Secondly, put a breakpoint on the offending line, and see what the Parent property actual is. This should tell you what you need to do.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
Thanks for the reply "OriginalGriff".
I knew there was something wrong, and checking for null will also tell the same.
same will the try .. catch .
However, i was looking for the solution to access myfunc in another class.
The reply by "bhiller" solved the problem.
Thanks anyway, I will keep in mind to check for null
|
|
|
|
|
Let us analyze the hierarchy of the elements:
You start with a form (ProjectDoc ). You add a UserControl panelProjectDoc to its control, thus ProjectDoc becomes Parent of panelProjectDoc . Then you add a UserControl buildBlock to panelProjectDoc , thus panelProjectDoc becomes Parent of buildBlock . Consequently, ProjectDoc is the grand parent of buildBlock . You would need the Parent of the Parent at the position where you get the run time error.
|
|
|
|
|
In other words, you should write (parent.Parent as ProjectDoc).myfunc(p)
|
|
|
|
|
yes, if you want it to blow up in your face at the first unforeseen way of using it, that is what you do. There is another approach called defensive programming that keeps you from putting everything in one line of code.
|
|
|
|
|
|
make sure to keep the catch block empty, no reason to alarm the user or burden the support people.
|
|
|
|
|
Makes you wonder why "On Error Resume Next" never made it into the C# spec.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
I was just talking to a colleague about that following the BBC Micro post in the lounge!
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Luc Pattyn wrote: in one line of code
What's a line of code?
MyType x = y as MyType ; if ( x != null ) { x.foo() ; x.bar() ; } else { something.else() ; } ...
|
|
|
|
|
you discovered the space bar now? usually you have a newline where we put a space...
|
|
|
|
|
Yes, just more proof that (in the C family of languages) spaces and newlines are interchangable and generally unimportant (to the compiler).
|
|
|
|
|
Thank you Sauro Viti.
That worked !!!
|
|
|
|
|
Thank you for explaining.
I got your point.
and the code from "Sauro Viti" worked fine
|
|
|
|
|
I have some class that derived from class queue ( generic namespace ) and i need to implement the IConable Interface.
Each object on the queue is implament the IClonable interface also.
Im not sure that my clone impementation of the queue derived class is the right way ...
The code:
<code>
public object Clone()
{
derivedFromQueueCollection retCollection = new derivedFromQueueCollection();
lock( this )
{
ObjectOnQueue[] arr = ToArray();
for( Int32 i = arr.Length ; i > 0 ; i-- )
{
retCollection.Enqueue( arr[ i ].Clone() as ObjectOnQueue);
}
}
return retCollection;
}
</code>
modified on Wednesday, August 25, 2010 9:36 AM
|
|
|
|
|
|
|
Rather than having the same question asked 3 times, why not stick with 1?
|
|
|
|
|
|
and you probably won't until you tidy up all of your duplicate posts!
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair.
nils illegitimus carborundum
me, me, me
|
|
|
|
|
I'm sorry, but the answer today[^] is INFEASIBLE
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
|
Well spotted!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
c# code for instal my font to client machine when my setup run
SuresPanneer
|
|
|
|