tags:

views:

300

answers:

2

I am writing an app, still, where I need to test some devices every minute for 30 minutes. It made sense to use a timer set to kick off every 60 secs and do whats required in the event handler.

However, I need the app to wait for the 30 mins until I have finished with the timer since the following code alters the state of the devices I am trying to monitor.

I obviously don't want to use any form of loop to do this. I thought of using another windows form, since I also display the progress, which will simply kick off the timer and wait until its complete.

The problem I am having with this is that I use a device Class and cant seem to get access to the methods in the device class from the 2nd (3rd actually - see below) windows form.

I have an initial windows form where I get input from the user, then call the 2nd windows form where it work out which tests need to be done and which device classes need to be used, and then I want to call the 3rd windows form to handle the timer.

I will have up to 6-7 device classes and so wanted to only instantiate them when actually requiring them, from the 2nd form.

Should I have put this logic into the 1st windows form (program class ??) ?

Would I not still have the problem of not being able to access device class methods from there too ?

Anyway, perhaps someone knows of a better way to do the checks every minute without the rest of the code executing (and changing the status of the devices) or how I should be accessing the methods in the app ??

Hi,

The following is 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;
using System.IO.Ports;

namespace TestCall

{
    public partial class Form1 : Form
    {
        NDTClass NDT = new NDTClass();

        public Form1()
        {
            InitializeComponent();

            NDT.NDTOpen();
            NDT.NDT1_CMD1();
            (new Form2()).ShowDialog();
            NDT.NDTClose();

        }

        public class NDTClass
        {
            public static double SWVolt = 0.5;
            public static string Rxstring = "";
            public SerialPort port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);

            public string NDTOpen()
            {
                port.Open();
                port.Write("CURRENT ?\r\n");
                return Rxstring;
            }

            public void NDTClose()
            {
                port.Close();
            }

            public void NDT1_CMD1()
            {
                port.Write("DUAL MODE\r\n");
            }
        }
    }
}

The following is the called 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 TestCall
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            Form1.NDTClass.SWVolt = 99;
            NDT.NDT1_CMD1();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}

The NDT.NDT1_CMD1() in the called form gives me the following error in the build -

Error   1   The name 'NDT' does not exist in the current context    

The use of the variable SWVolt within the NDTClass works fine but the use of the method in the class is not possible.

What have I done wrong ??

Thanks, George. (I hope the formatting comes out corrrectly too).

+1  A: 

Form1.NDTClass.SWVolt = 99; works because you are referencing a public static field in the Form1.NDTClass class. It is not tied to any instance. However for `*NDT.NDT1_CMD1();*, that doesn't work because:

  1. NDT1_CMD1() is an instance method and you need an instance of that class to call it.
  2. There is no instance of your class named NDT in Form2. Form1 contains a private field named NDT, but that is not accessible from Form2. You need to declare Form1.NDTClass NDT = new Form1.NDTClass() in Form2.

I think you need to make sure you understand variable scope.

Let me attempt an alternate explanation: SWVolt as a public static field, has global scope. It is accessible from anywhere in the application. NDT as a private instance field has the scope of a single instance of Form1. That is it is only accessible from inside an instance of Form1. Any code that is not part of Form1 cannot see it, which of course includes the code in Form2.

shf301
Hi,Thanks for the update, makes sense of course.I do understand how scope works but I dont see why you cant access a method defined as Public when you can access a variable defined as Public in the same class.
George
It doesn't really have to do with you trying to access a Public method. You're trying to reference a variable that doesn't exist. There is no variable named NDT in scope in Form2.
shf301
Ok, NDT does not exist but the method NDT1_CMD1() does exist in Form1, so why cant u access it in the same way you can access the public variable ?
George
Because one is static and the other is not. `SWVolt` is a static field; it is not tied to an instance of the class so you can reference it using the class name `Form1.NDTClass.SwVold`. `NDT1_CMD1()` is an instance method, it can only be called if you have an instance (that is you have new'd) an NDTClass object. Static exist with the class, so they can be referenced without a variable, but anything else (everything non-static) is tied to a particular instance (that is variable) and must be called from an instance/variable.
shf301
Ah ok, I see, so methods cannot ever be static. So what is the reasoning behind that ?
George
No methods can be static, but the method you're trying to call is not static.
shf301
A: 

code containing BLL_projectMastre Class it inherit from employeeMaster is it possible

using System; using System.Collections.Generic; using System.Text;

namespace CompanyLogo { class BLL_ProectMaster:BLL_EmployeeMaster {

    private int _projectid;
    private string _projectname;

    public int ProjectID
    {
        get
        {
            return _projectid;
        }
        set
        { _projectid = value; }

    }
    public string ProejectName
    {
        get
        {
            return _projectname;
        }
        set
        {
            _projectname = value;
        }
    }
}

}

code for BLL_EmployeeMaster class

sing System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient;

namespace CompanyLogo { class BLL_EmployeeMaster { #region private variable declaration private int _employeeid; private string _employeename; private string _email; private string _username; private string _password; private string _usertype; #endregion

   /*
    * Business Logic Layer code for Employee
     written by sudeesh I.G.
     Padmam technologies

    */

    #region accessor methods

    public int Employeeid
    {
        get { return _employeeid; }
        set { _employeeid = value; }

    }
    public string EmployeeName
    {
        get { return _employeename; }
        set { _employeename = value; }

    }
    public string Email
    {
        get { return _email; }
        set { _email = value; }
    }
    public string Username
    {
        get { return _username; }
        set { _username = value; }
    }
    public string Password
    {
        get { return _password; }
        set { _password = value; }
    }

    public string Usertype
    {
        get { return _usertype; }
        set { _usertype = value; }
    }

    #endregion

    #region userdeined function for inserting employees

    public int InsertIntoEmployeeMaster()
    { 
        ClsCon obj=new ClsCon();
        try{

            SqlCommand cmd = new SqlCommand("USP_EMPLOYEEMASTER_INSERT", obj.FnOpen());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@employeename", SqlDbType.VarChar, 50).Value = this.EmployeeName;
            cmd.Parameters.Add("@email", SqlDbType.VarChar, 50).Value = this.Email;
            cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value = this.Username;
            cmd.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = this.Password;
            cmd.Parameters.Add("usertype", SqlDbType.VarChar, 50).Value = this.Usertype;
            return Convert.ToInt32(cmd.ExecuteScalar());
           }
        catch
        {
            throw;
        }
        finally
        {
            obj.FnClose();
        }
    }

endregion

}

}

for project asign class BLL_ProjectAsignMast inherited from ProjectMaster for accepting projectid, employeeid

using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; using System.Data;

namespace CompanyLogo.BLL { class BLL_ProjectAsignMast:BLL_ProectMaster ///*BLL_EmployeeMaster,*/BLL_Interface_PojectMaster { private int _projectasignid;

    public int ProjectAsignID
    {
        get
        {
            return _projectasignid;
        }
        set
        {
            _projectasignid = value;
        }
    }

    public List<BLL_ProjectAsignMast> Fn_SelectProjectAsign()
    {
        ClsCon obj=new ClsCon();

        SqlCommand cmd = new SqlCommand("USP_PROJACTASIGN", obj.FnOpen());
        cmd.CommandType = CommandType.StoredProcedure;

        SqlDataReader dr = cmd.ExecuteReader();
        List<BLL_ProjectAsignMast> lst = new List<BLL_ProjectAsignMast>();
        while(dr.Read())
        {
            BLL_ProjectAsignMast pjtasignobj = new BLL_ProjectAsignMast();
            pjtasignobj.ProjectAsignID = Convert.ToInt32(dr.GetValue(0));
            pjtasignobj.EmployeeName = dr.GetValue(1).ToString();
            pjtasignobj.ProejectName = dr.GetValue(2).ToString();
            lst.Add(pjtasignobj);
        }

        return lst;

    }

}

}

i can't create object of a class BLL_ProjectAsignMas please give me the solution for this and the following

may i use like this type of code?

this type of inheritance is possible?

Is this type coding is good? if no why? is it satsfy oops?

have any advantage using reader like these type of using?

may i use dataset instead of reader here?

is reader / dataset better?

how can i use interfacehere?

if any one can help me, please correct me and give a good solution for these.and please mail into my id givel below>

sudeesh.i.g [email protected]

Name
Please ask a question instead of posting it as an answer!
SDX2000