Stupid question about scope

How to access a variable from two places

Page 1 of 1

13 Replies - 785 Views - Last Post: 21 July 2009 - 07:46 PM Rate Topic: -----

#1 Isochron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 28-October 08

Stupid question about scope

Post icon  Posted 21 July 2009 - 09:56 AM

Sorry to bother you guys with this trivial stupidity, but:

I'm making a program that can interact with a MySQL database. Everything is working fine, but I need to be able to open a connection named connection or whatever, and use that connection from other places, instead of having to make new connections every time I do something.

Here's my code:

private void button2_Click(object sender, EventArgs e) {
			string MyConString = "SERVER=" + ServerTxt.Text + ";" +
				"DATABASE=" + DBtxt.Text + ";" +
				"UID=" + UIDtxt.Text + ";" +
				"PASSWORD=" + Pwdtxt.Text + ";";
			SQLconnection = new MySqlConnection(MyConString);
			SQLconnection.Open();
}


Which happens when a user fills out the connection info and presses the connect button.

Then in another button which lets the user run queries on a database that has been connected to, this happens:

private void queryBtn_Click(object sender, EventArgs e) {
			MySqlCommand command = SQLconnection.CreateCommand();
			MySqlDataReader Reader;
			command.CommandText = query.Text;
			Reader = command.ExecuteReader();
			while (Reader.Read()) {
				string thisrow = "";
				for (int i= 0;i<Reader.FieldCount;i++)
					thisrow+=Reader.GetValue(i).ToString() + ",";
				listBox1.Items.Add(thisrow);
			}
}


But obviously, SQLconnection doesn't exist in the current context. I've tried fiddling with classes and stuff, but as you can see I really don't know what I'm doing, and couldn't figure out the public, static, private, sealed, and all that stuff associated with them. The solution may still lie in classes, but I haven't found it.

I'm using the mysql connector/net, which I have no idea what that means, but it may help you.
Thanks for anything you guys can point out.

Is This A Good Question/Topic? 0
  • +

Replies To: Stupid question about scope

#2 FoodGood  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 97
  • Joined: 08-July 09

Re: Stupid question about scope

Posted 21 July 2009 - 10:05 AM

I'm not sure that I get it, but you should declare it right after the class deceleration:

public Class BLA
{
	MySqlConnection SQLconnection;
	public BLA()
	{
	}
	....
	....
	....
	private void queryBtn_Click(object sender, EventArgs e) {
		  MySqlCommand command = SQLconnection.CreateCommand();
		  ...
	}
}

This post has been edited by FoodGood: 21 July 2009 - 10:08 AM

Was This Post Helpful? 0
  • +
  • -

#3 T3hC13h  Icon User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Stupid question about scope

Posted 21 July 2009 - 10:07 AM

Please post the code that shows were the SQLConnection object is declared in relation to where is being used. If the connection object is a class level veriable then your code should work.
Was This Post Helpful? 0
  • +
  • -

#4 Isochron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 28-October 08

Re: Stupid question about scope

Posted 21 July 2009 - 10:07 AM

So, the button click code has to be inside the class BLA?

EDIT: T3hC13h, I did post that code, it's in the first block of code I posted in the first post. It's created in a button click and in another button click it uses it.

This post has been edited by Isochron: 21 July 2009 - 10:09 AM

Was This Post Helpful? 0
  • +
  • -

#5 FoodGood  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 97
  • Joined: 08-July 09

Re: Stupid question about scope

Posted 21 July 2009 - 10:13 AM

View PostIsochron, on 21 Jul, 2009 - 09:07 AM, said:

So, the button click code has to be inside the class BLA?

EDIT: T3hC13h, I did post that code, it's in the first block of code I posted in the first post. It's created in a button click and in another button click it uses it.


You didn't post the code that he wants..
He wants to see where is the
MySqlConnection SQLconnection;


About tour question..
C# is an OOP language, and all your code is in classes
If you did buttons and staff, you should have a class that inherits from Form
Find it and put the decleration in the right place : )
Was This Post Helpful? 0
  • +
  • -

#6 Isochron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 28-October 08

Re: Stupid question about scope

Posted 21 July 2009 - 10:16 AM

In the first block of code, this isn't what he wanted?

SQLconnection = new MySqlConnection(MyConString);
Was This Post Helpful? 0
  • +
  • -

#7 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: Stupid question about scope

Posted 21 July 2009 - 10:26 AM

View PostIsochron, on 21 Jul, 2009 - 09:07 AM, said:

So, the button click code has to be inside the class BLA?

EDIT: T3hC13h, I did post that code, it's in the first block of code I posted in the first post. It's created in a button click and in another button click it uses it.



No, the button click code can be right where you probably have it.. inside Form1 I would imagine.. But you didnt show us the Class declaration so he was using an arbitrary name of a class (You will use the class the methods you posted are declared inside).

If you declare the SQL connection inside the class, but outside any specific function, that makes it a class member variable and can be used and accessed from any function inside the class. If you need it outside the class you can either make it public static or.. you can locate it in a static class.

You may want to look up Singleton design pattern to get a good Idea on how to implement this.

Aet

View PostIsochron, on 21 Jul, 2009 - 09:16 AM, said:

In the first block of code, this isn't what he wanted?

SQLconnection = new MySqlConnection(MyConString);



Thats the Assignment of the SQLconnection variable. He wants to see the declaration of it..

they look similar, but there should be a line that declares that SQLconnection is a MySqlConnection data type. Similar to what FoodGood showed you above.

Aet

This post has been edited by Aeternalis: 21 July 2009 - 10:28 AM

Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Stupid question about scope

Posted 21 July 2009 - 10:31 AM

View PostIsochron, on 21 Jul, 2009 - 12:16 PM, said:

In the first block of code, this isn't what he wanted?

SQLconnection = new MySqlConnection(MyConString);


there is a difference between.

// this is a declaration
MySqlConnection sqlConnection = new MySqlConnection(ConnectionString);



and

// this is an assignment
sqlConnection = new MySqlConnection(ConnectionString);


Was This Post Helpful? 0
  • +
  • -

#9 Isochron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 28-October 08

Re: Stupid question about scope

Posted 21 July 2009 - 10:49 AM

Ok, so now I have this:

public partial class Form1 : Form {

		public Form1() {
			InitializeComponent();
		}

public class conn {
			public conn() {}

			public MySqlConnection SQLconnection;
		}

private void button2_Click(object sender, EventArgs e) {
			string MyConString = "SERVER=" + ServerTxt.Text + ";" +
				"DATABASE=" + DBtxt.Text + ";" +
				"UID=" + UIDtxt.Text + ";" +
				"PASSWORD=" + Pwdtxt.Text + ";";
			conn conn = new conn();
			conn.SQLconnection = new MySqlConnection(MyConString);
			conn.SQLconnection.Open();
		}

private void queryBtn_Click(object sender, EventArgs e) {
			MySqlCommand command = conn.SQLconnection.CreateCommand();
			MySqlDataReader Reader;
			command.CommandText = query.Text;
			Reader = command.ExecuteReader();
			while (Reader.Read()) {
				string thisrow = "";
				for (int i= 0;i<Reader.FieldCount;i++)
					thisrow+=Reader.GetValue(i).ToString() + ",";
				listBox1.Items.Add(thisrow);
			}
		}
}


This seems to work except for

Error 1 An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.conn.SQLconnection'

on this line:

MySqlCommand command = conn.SQLconnection.CreateCommand();

What does that mean?
Was This Post Helpful? 0
  • +
  • -

#10 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: Stupid question about scope

Posted 21 July 2009 - 11:01 AM

Ok, Your compiler may be getting confused when you do conn conn = new conn(); The C# language is case sensitive, but your not using it.. Generally Class names should be capitalized with variables being camel case.. well as far as I can remember from my coding guidelines.

so try it like


Conn conn = new Conn(); 



in other words refactor your class name to the uppercase version of the name at the very least.

this should remove an ambiguity of the compiler seeing your code as being the class name instead of the instance.. and then thinking there is no object referenced.

::crosses fingers on this one::

Aet


BTW .. programs that access databases are not classified as new guy material.. Your doing great!

This post has been edited by Aeternalis: 21 July 2009 - 11:04 AM

Was This Post Helpful? 1
  • +
  • -

#11 Isochron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 28-October 08

Re: Stupid question about scope

Posted 21 July 2009 - 03:44 PM

Thanks Aet, that removed some problems. But now I have this one.

In the second button event, queryBtn_Click, there is this line:

MySqlCommand command = conn.SQLconnection.CreateCommand();

And that line errors with : conn doesn't exist in the current context.

So I'm guessing it can't see conn declared in the button2_click event code.

Why?
Was This Post Helpful? 0
  • +
  • -

#12 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: Stupid question about scope

Posted 21 July 2009 - 03:59 PM

View PostIsochron, on 21 Jul, 2009 - 02:44 PM, said:

Thanks Aet, that removed some problems. But now I have this one.

In the second button event, queryBtn_Click, there is this line:

MySqlCommand command = conn.SQLconnection.CreateCommand();

And that line errors with : conn doesn't exist in the current context.

So I'm guessing it can't see conn declared in the button2_click event code.

Why?

You are correct. The other buttonclick code can't see it because it is declared inside the other function. It is only in scope during the execution of that function.

public partial class Form1 : Form {

		  Conn conn;

		public Form1() {
			InitializeComponent();
		  conn = new Conn();
		}

public class conn {
			public conn() {}

			public MySqlConnection SQLconnection;
		}

	



move your declaration and assignment up to the class level making the conn variable a form member variable. assign it in the constructor.. you should then be able to access it from any form1 member function.

Aet
Was This Post Helpful? 0
  • +
  • -

#13 Isochron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 28-October 08

Re: Stupid question about scope

Posted 21 July 2009 - 04:09 PM

Thanks, I'll try that and get back to you on it.
Was This Post Helpful? 0
  • +
  • -

#14 Isochron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 28-October 08

Re: Stupid question about scope

Posted 21 July 2009 - 07:46 PM

That works brilliantly Aet, thank you very much.

But don't think you're rid of me, I'll be back with more questions soon :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1