Please advise of basic problem in my code (using opendialog)



Thankyou for your time Olie, I do understand the basics of classes, but
obviously still have much to learn! I had thought that the load
function did not exit until after the closing brace, and because i
called the fileopen function before this i had beleived that the
variable would still be accesible. I have moved the string outside of
the load method and into the class and it is now working.

One thing I can't understand is why if i leave the string variable in
the load function and add 'public' in front of it, like so: public
string file - why i can't then access it in the function openfile. I
had thought that public meant that anything in the code file can access
the variable?
-----------
also here is the rest of the class, in case it helps anyone looking at
my opendialog problem.

namespace MyNewProject{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.openFileDialog1 = new
System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// Form1
//
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.OpenFileDialog openFileDialog1;
}
}

Olie wrote:

Oh dear... I think you would save allot of time by going back to basics
and reading a book on basic csharp programming. It looks like you do
not understand some of the basic concepts of classes which are
fundamental if you want to program in C#.

One of those principals is variable scope. Any variable you use has to
be declared to have a scope in a particular domain. Where and how you
declare that variable has a dramatic effect on its scope.

In your example you declare file in the function Form1_Load() but this
means it only has scope in that function and will be destroyed once the
function exits. In order to give it scope in the FileOpen() function
you need to declare it in the class. If you declare it in the class
then you have the option of three different scopes Private, Protected
and Public. Each of these will give the variable a different scope.

I can not tell you what the error is with openfiledialog1 as the code
which would tell me the answer is part of the partial class which you
have not posted here. My bet is though that it is not declared as
openfiledialog1. Did you rename it?


garyuse...@xxxxxxxxx wrote:
This is the first time i've worked with openfile dialog.
I'm getting a couple of errors with my very basic code.

Can someone point out the errors in what i've done please.
==========================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyNewProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
string file;
FileOpen();
}

private void FileOpen()
{

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
file = openfiledialog1.FileName;
}


}
}
}

Problems : -
=============
Warning 1 The variable 'file' is declared but never used
Error 2 The name 'file' does not exist in the current context
Error 3 The name 'openfiledialog1' does not exist in the current
context

TIA

Gary

.



Relevant Pages

  • Re: Please advise of basic problem in my code (using opendialog)
    ... I think you would save allot of time by going back to basics ... One of those principals is variable scope. ... In your example you declare file in the function Form1_Loadbut this ... which would tell me the answer is part of the partial class which you ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Garbage collection problem
    ... The whole point of optimization is to ... > same camp as Chris Smith on the matter. ... declare a scope for the variable I may be depending on the object remaining ...
    (comp.lang.java.programmer)
  • Re: Java needs "goto" (was Re: hi)
    ... cause they won't go out of scope. ... as if they are hit, you are COMPLETELY out to lunch, ... that exception was hit if you are using declarations ... so a "declare at the top" style should still yield to ...
    (comp.lang.java.programmer)
  • Re: Nested Declarations
    ... specifically list things that can make something go out of scope. ... Such a block is not a scoping unit. ... Looks like implicit declarations always declare things to have the scope ... Which brings me back to something else I skipped over before - COMMON ...
    (comp.lang.fortran)
  • Re: advice on package design
    ... > for following scope. ... the compiler would have to do ... the extra verbiage required just to declare X. ... Unfortunately this is also untrue in Ada. ...
    (comp.lang.ada)