none
How to pass a parameter from one class to another RRS feed

  • Question

  • I want to get a value (1 or 0) from windows from when a button in pressed back into main but it comes up with an error when run saying "parameter is not valid". I am not sure what I am doing wrong apart from the obvious that there is something wrong with how I am implementing the parameter.

    using System;
    using System.Windows.Forms;
    
    //Imaging
    using System.Drawing.Imaging;
    using System.Drawing;
    
    namespace DataCollection
    {
        public partial class SortpBoundaryImages : Form
        {
            public int tempFlag = 0; 
    
            public int TmpFlag
            {
                get
                {
                    return tempFlag;
                }
                set
                {
                    tempFlag = value;
                }
            }
    
    
            public SortpBoundaryImages()
            {
                InitializeComponent();
                //Load up the image into the form
                Bitmap bmp = new Bitmap(@"C:\ELEC Project\temp.bmp");
                pictureBox1.Image = bmp;
                bmp.Dispose();
            }
    
            //public class ResultFromFrmMain
            //{
            //    public DialogResult Result { get; set; }
            //    public int Getvalue { get; set; }
            //}
    
                private void button1_Click(object sender, EventArgs e)
            {
                //If the users classifies the image as an impotant point then store it in the PboundaryIntrest folder
                int fileNum = Data.ImageCount.ImageSum("PboundaryIntrest");
    
                Bitmap bmp = new Bitmap(@"C:\ELEC Project\temp.bmp");
    
                bmp.Save(@"C:\ELEC Project\PboundaryIntrest\"+ fileNum +".bmp");
    
                bmp.Dispose();
    
                Close();
            }
    
            private void pictureBox1_Click(object sender, EventArgs e)
            {
    
            }
    
            private void pointOther_Click(object sender, EventArgs e)
            {
                //If the user classifies the point as not important then store it as a normal P boundary
                var tFalg = new SortpBoundaryImages();
                tFalg.tempFlag = 1;
    
                Close();
            }
        }
        class DataCollection
    { public static void main() { //add in temp image taker var Psort = new SortpBoundaryImages(); Psort.Show(); var Tflag = Psort.tempFlag; string gt = "\nboolvaluer is " + Tflag; Application.ShowAlertDialog(gt); } } }

    Friday, January 4, 2019 6:18 AM

All replies

  • Try at least these changes:

    private void pointOther_Click(object sender, EventArgs e)

    {

       TmpFlag = 1;

       Close();

    }

    . . .

    Psort.ShowDialog();

    var Tflag = Psort.TmpFlag;

    Friday, January 4, 2019 6:46 AM
  • I'll just mention auto properties.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

    Friday, January 4, 2019 7:25 AM
  • I want to get a value (1 or 0) from windows from when a button in pressed back into main but it comes up with an error when run saying "parameter is not valid". I am not sure what I am doing wrong apart from the obvious that there is something wrong with how I am implementing the parameter.



    I understood you want to pass something from a class to another.

    Try something like this:

    public class ThatSendsTheValues{
    
    //pass through constructor
    
    //supposing that click the event
    var tr= new ThatRecieves(0);
    
    }
    
    
    
    public class ThatRecieves{
    
    private int _theValue=default;
    public ThatRecieves()
    {
    
    }
    
    
    public ThatRecieves(int theValue)
    {
    _theValue=theValue;
    }
    
    
    }

     You can also use properties:

    public class ThatSendsTheValues{
    
    //pass through constructor
    
    //supposing that this inside of the click event
    var tr= new ThatRecieves{ TheValue=0 };
    
    //or
    var tr2= new ThatRecieves();
    tr2.TheValue=0;
    }
    
    
    
    public class ThatRecieves{
    
    public int TheValue{get;set;}
    
     
    
    }


    BP-LP 2005/2016 @ll rights reserved

    Saturday, January 5, 2019 9:16 AM
  • Hi Kokoda144,

    Thank you for posting here.

    According to your description, I have recreated your problem,we could not release the bmp resource in SortpBoundaryImages, because we can use it below.

    Please make the following change.

    Change

     
     public SortpBoundaryImages()
    
            {
    
                InitializeComponent();
    
                //Load up the image into the form
    
                Bitmap bmp = new Bitmap(@"C:\ELEC Project\temp.bmp");
    
                pictureBox1.Image = bmp;
    
                bmp.Dispose();
    
            }

    Into

       
    public SortpBoundaryImages()
    
            {
    
                InitializeComponent();
    
                //Load up the image into the form
    
                Bitmap bmp = new Bitmap(@"C:\ELEC Project\temp.bmp");
    
                pictureBox1.Image = bmp;
    
            }
    

    Best regards,

    Jack 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, January 7, 2019 2:40 AM
    Moderator