Windows Develop Bookmark and Share   
 index > Windows Forms General > passing values within methods in a form
 

passing values within methods in a form


hi, i'm kind of stuck.i have 2 methods.
and i have declared a byte[] abc in menuItemSnap_Click method.
my question is : in another method, how do i get abc that is declare in
menuItemSnap_Click method???



Code Snippet
private void menuItemSnap_Click(object sender, EventArgs e)
{
CameraCaptureDialog camCapture = new CameraCaptureDialog();
camCapture.InitialDirectory = @"\My Documents\My Pictures";
camCapture.Mode = CameraCaptureMode.Still;

if (camCapture.ShowDialog() == DialogResult.OK)
{
fileName = camCapture.FileName; // Get filename and create image.
Show();
pictureBoxCamera.Image = new Bitmap(fileName);
pictureBoxCamera.SizeMode = PictureBoxSizeMode.StretchImage;
camCapture.Dispose();

}

else
{
this.Close();
}
FileStream fs = File.OpenRead(camCapture.FileName);
byte[] abc = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
}


private void SendQUERY()
{
// i want to get abc which is declared in previous method

}



thanks in advance


rookie_rockie  Monday, July 09, 2007 9:13 AM
You will need to declare abc outside of your method like so:

Code Snippet

private byte[] abc = null;

private void menuItemSnap_Click(object sender, EventArgs e)
{
// set abc here
}

private void SendQUERY()
{
if (abc != null)
{
// perform an operation
}
}


UsualDosage  Monday, July 09, 2007 12:12 PM

Try this:

Code Snippet

private byte[] abc = null;

private void menuItemSnap_Click(object sender, EventArgs e)
{
CameraCaptureDialog camCapture = new CameraCaptureDialog();
camCapture.InitialDirectory = @"\My Documents\My Pictures";
camCapture.Mode = CameraCaptureMode.Still;

if (camCapture.ShowDialog() == DialogResult.OK)
{
fileName = camCapture.FileName; // Get filename and create image.
Show();
pictureBoxCamera.Image = new Bitmap(fileName);
pictureBoxCamera.SizeMode = PictureBoxSizeMode.StretchImage;
camCapture.Dispose();

}

else
{
this.Close();
}
FileStream fs = File.OpenRead(camCapture.FileName);

abc = new byte[fs.Length]; // removed byte[]

fs.Read(bytes, 0, bytes.Length);
}


private void SendQUERY()
{
// i want to get abc which is declared in previous method
if (abc != null)

{

// do something with abc

}
}

Remember that your variable name is "abc", not "byte[] abc". The "byte[]" declares the variable. So we're moving the declaration out of the event handler, which promotes it to class scope instead of local scope.

Christopher Payne  Tuesday, July 10, 2007 10:15 PM
You will need to declare abc outside of your method like so:

Code Snippet

private byte[] abc = null;

private void menuItemSnap_Click(object sender, EventArgs e)
{
// set abc here
}

private void SendQUERY()
{
if (abc != null)
{
// perform an operation
}
}


UsualDosage  Monday, July 09, 2007 12:12 PM
hmmm. but byte[] abc needs to get the value of camcapture.filename
rookie_rockie  Monday, July 09, 2007 11:17 PM
Right, and since it's declared inside the class (private access modifier), you set it with menuItemSnap_Click. Then, if you reference it later in another method, we check to see if it's null (has not been set yet), and if it's not, we perform some other logic on it.
UsualDosage  Tuesday, July 10, 2007 9:35 PM

Try this:

Code Snippet

private byte[] abc = null;

private void menuItemSnap_Click(object sender, EventArgs e)
{
CameraCaptureDialog camCapture = new CameraCaptureDialog();
camCapture.InitialDirectory = @"\My Documents\My Pictures";
camCapture.Mode = CameraCaptureMode.Still;

if (camCapture.ShowDialog() == DialogResult.OK)
{
fileName = camCapture.FileName; // Get filename and create image.
Show();
pictureBoxCamera.Image = new Bitmap(fileName);
pictureBoxCamera.SizeMode = PictureBoxSizeMode.StretchImage;
camCapture.Dispose();

}

else
{
this.Close();
}
FileStream fs = File.OpenRead(camCapture.FileName);

abc = new byte[fs.Length]; // removed byte[]

fs.Read(bytes, 0, bytes.Length);
}


private void SendQUERY()
{
// i want to get abc which is declared in previous method
if (abc != null)

{

// do something with abc

}
}

Remember that your variable name is "abc", not "byte[] abc". The "byte[]" declares the variable. So we're moving the declaration out of the event handler, which promotes it to class scope instead of local scope.

Christopher Payne  Tuesday, July 10, 2007 10:15 PM

You can use google to search for other answers

Custom Search

More Threads

• Split container - make a panel dissapear
• Managing HTTP session in VB
• Loading report failed
• Search through PDF, TXT and HTM files .. regex
• Close dll connection with winform
• Multiple Forms each with its own ToolStrip
• Adding a control to an user control, and allowing it to be visible outside of the parent control
• Using VB 2008 How does one Move from Field to Field Using Enter Key ?
• ShowDialog() Not Modal Dialog (no parent form?)
• Remote Reflect or Help Needed