dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] new operator problem?

  1. #1
    Join Date
    Jan 2011
    Location
    Vilnius/Utena, Lithuania
    Posts
    14

    [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?
    Last edited by Migeria; May 17th, 2011 at 05:28 PM.

  2. #2
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    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.

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  4. #4
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    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];
    }
    }

  5. #5
    Join Date
    Jan 2011
    Location
    Vilnius/Utena, Lithuania
    Posts
    14

    Unhappy 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.

  6. #6
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: [RESOLVED] new operator problem?

    Glad to be of help

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)