467,885 Members | 1,303 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,885 developers. It's quick & easy.

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

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

Nov 23 '06 #1
  • viewed: 4443
Share:
7 Replies
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...@myway.com 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
Nov 23 '06 #2
Hi!

You declare the "file" variable in one method (Form1_Load), and try to
use it from the other method (FileOpen). The scope of your variable
declaration is the Form1_Load() method. To use it in the other method,
you need to declare it there. Or you need to make it a field for your class.

Try something like this:

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

private string FileOpen()
{
string file = null;

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

return file;
}

There are of course a lot of ways to do this, depending on how/what you
would like to do.

Hope this helps

-Lenard

ga********@myway.com 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
Nov 23 '06 #3
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...@myway.com 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
Nov 23 '06 #4
Thankyou that seems to be working much better, i have the following: -

public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
string file;
file = FileOpen();
}
private string FileOpen()
{
string file = null;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
file = openFileDialog1.FileName;
}
return file;
}
}
}

----
but the open file dialog isn't being displayed when my form is run.

I found out i had named it wrongly (wrong case which is why it wasn't
displaying).

can someone suggest why the open dialog isn't being displayed please?

thankyou

Nov 23 '06 #5
Edit:
**i found out i hadn't named it properly wrong case, which is why IT
WASNT ACCESSIBLE in my original code. BUT i still have the problem that
it isnt displaying **

Nov 23 '06 #6
Any variable declared in a function will only have scope inside that
function. For your variable to have class scope you must declare it
inside the class but outside a function.
You said that public means that it can be accessed from anywhere in the
class. Well it does allot more than that it can be accessed from
everywhere. If you are declaring a class scope variable or member
variable then you should realy only declare it as either private or
protected and the latest documentation even says you should not declare
it as protected.

The reason you can not see your dialog is that you have to tell it to
display. Call the function ShowDialog() on the object and it will popup.

Nov 23 '06 #7
Sorry you are calling ShowDialog(). It should work, you might want to
check that all the properties are set correctly and try stepping
through the code to see what is happening.

Just a bit of advice on forum etiquette, you should not post the same
or similar question in multiple threads.
Olie wrote:
Any variable declared in a function will only have scope inside that
function. For your variable to have class scope you must declare it
inside the class but outside a function.
You said that public means that it can be accessed from anywhere in the
class. Well it does allot more than that it can be accessed from
everywhere. If you are declaring a class scope variable or member
variable then you should realy only declare it as either private or
protected and the latest documentation even says you should not declare
it as protected.

The reason you can not see your dialog is that you have to tell it to
display. Call the function ShowDialog() on the object and it will popup.
Nov 23 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Question | last post: by
8 posts views Thread by TAM | last post: by
3 posts views Thread by Random Person | last post: by
4 posts views Thread by Geir Baardsen | last post: by
4 posts views Thread by Will | last post: by
2 posts views Thread by news.microsoft.com | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.