11 Replies - 1321 Views - Last Post: 10 June 2012 - 10:36 PM Rate Topic: -----

#1 Zigon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 20-January 12

Error when creating a second instance of a class (Parameter is not ...

Posted 09 June 2012 - 03:24 AM

Hi!

I'm having trouble figuring out what's happening here. I'm drawing some stuff on the Panel canvas, but there's a problem, when I try to create the secon instance of the class (Miza).
When the first instance is created, the table draws just fine, but when it gets to the second instance is being created, the error occours in the line 69 saying: "Parameter is not valid"

I've been going thru the code many times but to no avail. I just can't spot the problem...

So, here's the code for the Class Miza:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace GRAFIKA_TEST
    {
    class Miza
        {
        private string imeMize="Miza";
        public string ImeMize
            {
            get { return imeMize; }
            set { imeMize = value; }
            }

        private int radijMize;
        public int RadijMize
            {
            get { return radijMize; }
            set
                {
                if (value<50)
                    {
                    value = 50;
                    }
                radijMize=value;
                }
            }

        private int stolpec;
        public int Stolpec
            {
            get { return stolpec; }
            set { 
                if (value==0)
                    {
                    value = 1;
                    }
                stolpec = value * 30;
                }
            }

        private int vrsta;
        public int Vrsta
            {
            get { return vrsta; }
            set
                {
                if (value == 0)
                    {
                    value = 1;
                    }
                vrsta = value * 20;
                }
            }

        public void NarisiMize(Graphics e)
            {
            Graphics gfx = e;
            // Create a new pen and stuff to use for drawing the table
            Pen myPen = new Pen(Color.Black);
            SolidBrush myBrush = new SolidBrush(Color.Brown);
            SolidBrush barvaImenaMize=new SolidBrush(Color.Yellow);
            Font myFont=new Font("Ariel",8);

            // draw the table on canvas
            gfx.FillEllipse(myBrush, stolpec,vrsta, radijMize, radijMize);

            // draw the name of the table
            gfx.DrawString(imeMize, myFont,barvaImenaMize,stolpec+10,vrsta+20);

            // draw 4 chairs around the table
            gfx.DrawRectangle(myPen, stolpec-20, vrsta+15, 20, 20);
            gfx.DrawRectangle(myPen, stolpec+50, vrsta+15, 20, 20);
            gfx.DrawRectangle(myPen, stolpec+15, vrsta+50, 20, 20);
            gfx.DrawRectangle(myPen, stolpec + 15, vrsta-20, 20, 20);

            //get rid of stuff
            myPen.Dispose();
            myBrush.Dispose();
            barvaImenaMize.Dispose();
            myFont.Dispose();
            gfx.Dispose();
            }
        }
    }



And here's the code of the calling form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GRAFIKA_TEST
    {
    public partial class Form1 : Form
        {
        public Form1()
            {
            InitializeComponent();
            }
        protected override void OnPaint(PaintEventArgs e)
            {
            // Get the graphics object
            }

        private void panel1_Paint(object sender, PaintEventArgs e)
            {
            // this one is working OK!
            Miza miza = new Miza();
            miza.Vrsta = 1;
            miza.Stolpec = 1;
            miza.RadijMize = 50;
            miza.ImeMize = "Bojan";
            miza.NarisiMize(e.Graphics);

            // this one is NOT working! (WHY?)
            Miza miza1 = new Miza();
            miza1.Vrsta = 1;
            miza1.Stolpec = 2;
            miza1.RadijMize = 50;
            miza1.ImeMize = "Miha";
            miza1.NarisiMize(e.Graphics);
            }
        }
    }



I'm drawing this on a Panel...

Any ideas?

Regards and best wishes! Ales

Is This A Good Question/Topic? 0
  • +

Replies To: Error when creating a second instance of a class (Parameter is not ...

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6021
  • View blog
  • Posts: 23,396
  • Joined: 23-August 08

Re: Error when creating a second instance of a class (Parameter is not ...

Posted 09 June 2012 - 03:36 AM

Copy and paste the whole error message as it appears, so that we might have a better idea of what parameter is invalid.
Was This Post Helpful? 0
  • +
  • -

#3 Zigon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 20-January 12

Re: Error when creating a second instance of a class (Parameter is not ...

Posted 09 June 2012 - 03:41 AM

That's the only message I get... basicly.

The first instance draws just fine, so I was thinking that the problem lies in the "paint event" of the Panel...

Anyway,
Here's the exeption details, if it is of any help...

Quote

System.ArgumentException was unhandled
Message=Parameter is not valid.
Source=System.Drawing
StackTrace:
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
at System.Drawing.Graphics.FillEllipse(Brush brush, Int32 x, Int32 y, Int32 width, Int32 height)
at GRAFIKA_TEST.Miza.NarisiMize(Graphics e) in D:\PROGRAMIRANJE\GRAFIKA TEST\GRAFIKA TEST\Miza.cs:line 69
at GRAFIKA_TEST.Form1.panel1_Paint(Object sender, PaintEventArgs e) in D:\PROGRAMIRANJE\GRAFIKA TEST\GRAFIKA TEST\Form1.cs:line 39
at System.Windows.Forms.Control.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
at System.Windows.Forms.Nativewindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at GRAFIKA_TEST.Program.Main() in D:\PROGRAMIRANJE\GRAFIKA TEST\GRAFIKA TEST\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6021
  • View blog
  • Posts: 23,396
  • Joined: 23-August 08

Re: Error when creating a second instance of a class (Parameter is not ...

Posted 09 June 2012 - 04:16 AM

OK, so looking at the stack trace it looks like this is the last line in your code that's being executed:

at System.Drawing.Graphics.FillEllipse(Brush brush, Int32 x, Int32 y, Int32 width, Int32 height)


the being the line in code:
gfx.FillEllipse(myBrush, stolpec,vrsta, radijMize, radijMize);


So I would set a breakpoint on that line and check the values of all the variables being passed. It seems to me like they all should be OK, but I don't know much about the Graphics class, so it's possible that reusing the event's Graphics property is somehow forbidden or otherwise causing the problem. Or perhaps one of the values might be too large for the bounding canvas or something? Sorry, I'm just guessing.
Was This Post Helpful? 1
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Error when creating a second instance of a class (Parameter is not ...

Posted 09 June 2012 - 04:51 AM

*
POPULAR

In line 85 you dispose of the Graphics object you send to the NarisiMize method. You then attempt to use the same graphics object in the next call, but the object has been disposed.

Since you don't create the Graphics object, you probably shouldn't be disposing of it.

This post has been edited by Momerath: 09 June 2012 - 04:56 AM

Was This Post Helpful? 5
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3423
  • View blog
  • Posts: 10,545
  • Joined: 05-May 12

Re: Error when creating a second instance of a class (Parameter is not ...

Posted 09 June 2012 - 04:53 AM

*
POPULAR

It's because you disposed the Graphics object in line 85 of the first chunk of code you posted. So of course to second instance gets to play with a disposed object and it complains.

I believe that you are NOT supposed to dispose the Graphics object that you get in the PaintEventArgs.
Was This Post Helpful? 5
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6021
  • View blog
  • Posts: 23,396
  • Joined: 23-August 08

Re: Error when creating a second instance of a class (Parameter is not ...

Posted 09 June 2012 - 04:59 AM

There you go...didn't read down far enough. Although, shouldn't it have generated some sort of other error when attempting to access a disposed object the second time around?
Was This Post Helpful? 1
  • +
  • -

#8 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Error when creating a second instance of a class (Parameter is not ...

Posted 09 June 2012 - 05:15 AM

Since you can still have references to a disposed object, any object that can be disposed should have some flag that indicates that it has been disposed and the methods should be checking this and raising an exception if you do call them. ArgumentException seems like an odd one to raise to me since we have the ObjectDisposedException class.
Was This Post Helpful? 2
  • +
  • -

#9 Zigon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 20-January 12

Re: Error when creating a second instance of a class (Parameter is not ...

Posted 10 June 2012 - 08:29 PM

Oh, I see! But, I should dispose the graphic some how, shouldn't I? Or is it the case that I'm passing the it as an argument that it's being disposed in the calling class (form in this case)?

Another question crosses my mind: should I destroy my objects when finished drawing (miza), or does the garbage collector do it for me?
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3423
  • View blog
  • Posts: 10,545
  • Joined: 05-May 12

Re: Error when creating a second instance of a class (Parameter is not ...

Posted 10 June 2012 - 10:17 PM

In .NET, the convention is caller cleans up. So if you are called with resources, use them, but don't dispose them. If you calling somebody else, you provide the resources, since you are the one who provided them, you are the one who knows about their lifetime.

Sent from my T-Mobile G2 using Tapatalk 2
Was This Post Helpful? 2
  • +
  • -

#11 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Error when creating a second instance of a class (Parameter is not ...

Posted 10 June 2012 - 10:32 PM

I follow the rule of "If I create it, I dispose of it", similar to what Skydiver said above. In your code in the first post, you create a Font, 2 Brushes and a Pen. You also dispose of them at the end of the method. That's what you should be doing. You didn't create the Graphics object, so you shouldn't be disposing it.

As for your Miza objects, the GC will get them. They don't contain unmanaged resources so no need to implement IDisposable for that class.

This post has been edited by Momerath: 10 June 2012 - 10:35 PM

Was This Post Helpful? 2
  • +
  • -

#12 Zigon  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 20-January 12

Re: Error when creating a second instance of a class (Parameter is not ...

Posted 10 June 2012 - 10:36 PM

Ok. Thanks y'all!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1