trouble passing reference to instance.

J

jkv

Hi,
I have recently migrated from Python to C# and i am having some
trouble passing reference when creating a instance.
My problem is that when i create a instance in the MainWindow
constructor i cannot access that instance (testing) from outside the
constructor.
If i move the "DataReader testing = new DataReader(this);" outside the
constructor i cannot pass the reference to 'this' anymore.

Any hints?

regards,
Johnny

namespace ms_csharp
{

public partial class MainWindow : Form
{

public MainWindow()
{
InitializeComponent();
DataReader testing = new DataReader(this);
}
}





AnotherForm.cs:

namespace ms_csharp
{

public class DataReader
{

public DataReader(MainWindow bah)
{
_MainWindow = bah;
}
}
 
G

Gregory A. Beamer

Hi,
I have recently migrated from Python to C# and i am having some
trouble passing reference when creating a instance.
My problem is that when i create a instance in the MainWindow
constructor i cannot access that instance (testing) from outside the
constructor.
If i move the "DataReader testing = new DataReader(this);" outside the
constructor i cannot pass the reference to 'this' anymore.

Any hints?

regards,
Johnny

namespace ms_csharp
{

public partial class MainWindow : Form
{

public MainWindow()
{
InitializeComponent();
DataReader testing = new DataReader(this);
}
}





AnotherForm.cs:

namespace ms_csharp
{

public class DataReader
{

public DataReader(MainWindow bah)
{
_MainWindow = bah;
}
}

namespace ms_csharp
{

public partial class MainWindow : Form
{
protected DataReader testing;

public MainWindow()
{
InitializeComponent();
testing = new DataReader(this);
}
}

If using it elsewhere is the goal, that should solve the problem.

peace and grace,


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
D

David Anton

Coming from an environment where there are no variable declarations must be
tough. Just separate the declaration and initialization:

public partial class MainWindow : Form
{
private DataReader testing;

public MainWindow()
{
InitializeComponent();
testing = new DataReader(this);
}
}
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB
 
W

Wyrm

Hi,
I have recently migrated from Python to C# and i am having some
trouble passing reference when creating a instance.
My problem is that when i create a instance in the MainWindow
constructor i cannot access that instance (testing) from outside the
constructor.
If i move the "DataReader testing = new DataReader(this);" outside the
constructor i cannot pass the reference to 'this' anymore.

Any hints?

regards,
Johnny

namespace ms_csharp
{

    public partial class MainWindow : Form
    {

        public MainWindow()
        {
            InitializeComponent();
            DataReader testing = new DataReader(this);
        }

}

AnotherForm.cs:

namespace ms_csharp
{

    public class DataReader
    {

        public DataReader(MainWindow bah)
        {
           _MainWindow = bah;
        }



}- Masquer le texte des messages précédents -

- Afficher le texte des messages précédents -

Your problem is the variable scope.
When declaring your "testing" variable in a method, it is no longer
referenced when the method ends.

Hence:
namespace ms_csharp
{
public partial class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
DataReader testing = new DataReader(this);
} //<= Here the "testing" variable no longer exists
}


So you have to declare it as a class field and intialize it in the
constructor.

namespace ms_csharp
{
public partial class MainWindow : Form
{
private DataReader testing; // <= declaring it here will make
it persist as long as your class instance exists;

public MainWindow()
{
InitializeComponent();
testing = new DataReader(this);
}
}

Note that "private" makes it available in this class instance only.
"protected" or "public" will extend its range.
 
J

jkv

namespace ms_csharp
{

    public partial class MainWindow : Form
    {
          protected DataReader testing;

        public MainWindow()
        {
            InitializeComponent();
            testing = new DataReader(this);
        }

}

If using it elsewhere is the goal, that should solve the problem.

peace and grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog:http://gregorybeamer.spaces.live.com

*******************************************
|      Think outside the box!             |
*******************************************

namespace ms_csharp
{

public partial class MainWindow : Form
{
protected DataReader testing;

public MainWindow()
{
InitializeComponent();
testing = new DataReader(this);
}

}

If using it elsewhere is the goal, that should solve the problem.

Thanks for all for the suggestions. However i am still having problems
calling methods from the test instance. When i call testing.testmethod
() (see 'private void button1_Click') i get a 'Object reference not
set to an instance of an object' exception, which indicates, to me at
least, that i missed a vital point somewhere - the case might very
well be that i am still to pythonic :)

MainWindow.cs
namespace WindowsFormsApplication5
{
public partial class MainWindow : Form
{
protected DataReader testing;
public MainWindow()
{
InitializeComponent();
DataReader testing = new DataReader(this);
}

private void MainWindow_Load(object sender, EventArgs e)
{ }
private void button1_Click(object sender, EventArgs e)
{
testing.testmethod();
}
}
}

DataReader.cs:

namespace WindowsFormsApplication5
{
public class DataReader
{
protected MainWindow _MainWindow;
public DataReader(MainWindow bah)
{
_MainWindow = bah;
}
public void testmethod()
{
// just testing
}
}

}

Regards,
jkv
 
J

jkv

    public partial class MainWindow : Form
    {
        protected DataReader testing;
        public MainWindow()
        {
            InitializeComponent();
            DataReader testing = new DataReader(this);

Gee man, figured it out as soon as i hit 'send'. Im actually
redeclaring testing in the Constructor here - which is wrong. The
right this is:
testing = new DataReader(this);

Again many thanks to all who helped out!

Regards,
jkv
 
G

Gregory A. Beamer

Gee man, figured it out as soon as i hit 'send'. Im actually
redeclaring testing in the Constructor here - which is wrong. The
right this is:
testing = new DataReader(this);

Again many thanks to all who helped out!

Regards,
jkv

Just as a side note: you can chain constructors, like so:

public MyClass()
{
//Do something ALWAYS here
}

public MyClass(int id) :this()
{
_id = id;
}

BTW, I often hit send and then get the answer in a few minutes. It is
normal, as your brain relaxes and is then allowed to search the memory
banks to come up with the answer. Most times, the answer is simple, but
the stress of finding it was blinding you to the answer. ;-)

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

My vacation and childhood cancer awareness site:
http://www.crazycancertour.com

*******************************************
| Think outside the box! |
*******************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top