Thread: problem in pass a variable to a class

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    problem in pass a variable to a class

    Hi my friends,

    I wrote a program in C#.
    I want user put a number(n) in textBox and click button and then my

    program, draw a polygonal according with (n).

    My program include 2 class:

    1- class Form1
    2- class View (this class use opengl library to draw polygonal)


    But i have problem in pass (n) to glDraw method (one methods of View class)

    I defined a public variable in Form1 class with this name: n
    Then in button method set n equal the number that user entered in textbox.

    Then ret() method in Form1 class, return n (the number that user entered)

    In glDraw method of View class, i set:
    Form1 mt = null;
    int m = 3;
    m = mt.ret();

    To use m in glDraw method.
    If i set a static number instead m in glDraw method, program works correctly, but when i was receive number from user (like i explained before) it doesn't work and take an error.

    I add my program and error's picture to my post.
    I will be thankful if help me to complete my program.

    sorry for my long description.

    Best regards,
    Nima

    error picture: http://atn.parsaspace.com/error%20pic.jpg

    Code:
    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;
    using CsGL.OpenGL;
    
    
    namespace WindowsFormsApplication8
    {
        public partial class Form1 : Form
        {
            public int n;
            public Form1()
            {
                InitializeComponent();
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
    
            }
    
    
            public void button1_Click(object sender, EventArgs e)
            {
                n = Int32.Parse(InNumber.Text);
                View view = new View();
                view.Dock = DockStyle.Fill;
                Controls.Add(view);
    
            }
            public int ret()
            {
                return n;
            }
    
    
        }
    
        class View : OpenGLControl
        {
            public override void glDraw()
            {
                GL.glClear(GL.GL_COLOR_BUFFER_BIT |
                GL.GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
                GL.glBegin(GL.GL_LINES);
    
                Form1 mt = null;
                int m = 3;
                m = mt.ret();
                int i;
    
                double x0, y0, x1, y1, th = 360 / m;
                x0 = 1 * Math.Cos(th * Math.PI / 180);
                y0 = 1 * Math.Sin(th * Math.PI / 180);
                for (i = 1; i <= m; i++)
                {
                    x1 = 1 * Math.Cos(((i + 1) * th) * Math.PI / 180);
                    y1 = 1 * Math.Sin(((i + 1) * th) * Math.PI / 180);
                    GL.glVertex2d(x0, y0);
                    GL.glVertex2d(x1, y1);
                    x0 = x1;
                    y0 = y1;
                }
                GL.glEnd();
                GL.glFlush();
            }
            protected override void InitGLContext()
            {
                GL.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
                GL.glColor3f(0.0f, 0.0f, 0.0f);
                GL.glPointSize(1.0f);
            }
            protected override void OnSizeChanged(EventArgs e)
            {
                base.OnSizeChanged(e);
                GL.glMatrixMode(GL.GL_PROJECTION);
                GL.glLoadIdentity();
            }
        }
    
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You set mt to null, then try to access one of its members, thus your null reference exception. mt has to point to something (a form?).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    3
    Quote Originally Posted by Magos View Post
    You set mt to null, then try to access one of its members, thus your null reference exception. mt has to point to something (a form?).
    Thanks for your reply my friend.

    I'm trying to pass the number that user enter in textbox to glDraw() method of View class.
    I tested this way before:

    Code:
                int m = 3;
                Form1 mt=new Form1();
                m = mt.ret();
    But this error accured: http://atn.parsaspace.com/error%20pic2.jpg


    Best regards,
    Nima

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    3
    Thanks Magos,
    My problem solved here.

    Best regards,
    Nima

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class read as variable, default-type int? Say what?!
    By arcaine01 in forum C++ Programming
    Replies: 8
    Last Post: 07-10-2009, 05:34 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 08:19 AM
  4. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM