[RESOLVED] new operator problem?
Code:
namespace ...
{
public partial class Form1 : Form
{
.
.
private void textBox1_DragDrop(object sender, DragEventArgs e){
string[] failas = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string failas_ in failas){
Failas cfailas = new Failas(); //////////////////////////////////////////////////
cfailas.setfile(failas_); //////////////////////////////////////////////////
label2.Text = cfailas.failas; ////////////////////////////////////////////////// <<<<<<< EVERYTHING IS OK
textBox1.Text = cfailas.split_filename(failas_);
}}
.
.
string password = "";
private void button2_Click(object sender, EventArgs e) {
if (password == "") {
password = textBox2.Text;
textBox2.Clear();
label2.Text = "Pakartokite slaptažodį:";
}
else if (label2.Text == "Pakartokite slaptažodį:" && password == textBox2.Text){
Failas cfailas = new Failas(); //////////////////////////////////////////////////
label1.Text = cfailas.failas; ////////////////////////////////////////////////// <<<<<<< IS EMPTY
textBox2.Clear();
password.Remove(0);
}}
}
class Failas
{
public string failas; //////////////////////////////////////////////////
public void setfile(string a){ failas = a;} //////////////////////////////////////////////////
public string split_filename(string a){
string[] b;
b = a.Split('\\');
return b[b.Length - 2] + "\\" + b[b.Length - 1];
}
}
}
Why variable "failas" is empty in "Failas" class when I set it in one function, but read from other?
Re: new operator problem?
Place a breakpoint in your textBox1_DragDrop() method and F10 your way through to see what string values you are getting and passing around.
I can say that initially, I don't see where you are storing one of your objects in such a scope that it's values would be available in both the drag-drop event and the button-click event.
Re: new operator problem?
I only see one variable named "failas" in a single method and it is local to that method. I suggest you brush up on the basics, specifically variable scope and lifetime.
Re: new operator problem?
Look at this and how I moved your Failas object into scope where it's values will be retained and available throughout your form class. I did not try to understand what you are trying to accomplish completely, just wanted to give an example of making your object available throughout the class rather than just in the method it was created in. Best of luck.
public partial class Form1 : Form
{
private Failas _recentFailas;
private string _password = "";
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
string[] failas = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string failas_ in failas)
{
if (_recentFailas == null)
this._recentFailas = new Failas();
this._recentFailas.setfile(failas_);
label2.Text = this._recentFailas.failas;
textBox1.Text = this._recentFailas.split_filename(failas_);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (this._password == "")
{
this._password = textBox2.Text;
textBox2.Clear();
label2.Text = "Pakartokite slaptažodį:";
}
else
if (label2.Text == "Pakartokite slaptažodį:" && this._password == textBox2.Text)
{
label1.Text = this._recentFailas.failas;
textBox2.Clear();
this._password = this._password.Remove(0);
}
}
}
class Failas
{
public string failas; //////////////////////////////////////////////////
public void setfile(string a) { failas = a; } //////////////////////////////////////////////////
public string split_filename(string a)
{
string[] b;
b = a.Split('\\');
return b[b.Length - 2] + "\\" + b[b.Length - 1];
}
}
Re: new operator problem?
I did try to use global variable,everything was correct, but I ended up failing 'cause of my royal stupidity.
I was coding one part, but inputing information through other way ~_~
Thanks Fcronin, 'cause of your last post with code, I noticed that I was looking at totally different part.
Re: [RESOLVED] new operator problem?