Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot access a non-static member of outer type via nested type

Status
Not open for further replies.

bind

IS-IT--Management
Dec 13, 2004
25
Hello Experts,

I'm desperately seeking assistance with this issue I'm having.

I'm getting the following error:

Cannot access a non-static member of outer type 'BindsTools.PortScanner' via nested type 'BindsTools.PortScanner.PortScannerr'

I've been banging my head against a wall, I've looked up outer and inner types examples but just can't seem to get this working properly.

Can anyone assist me with this?

Thank you much!

And the code for the C#.net form is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace BindsTools
{
public partial class PortScanner : Form
{
public PortScanner()
{
InitializeComponent();
}
public void AddHeaderInformation(string name, string value)
{
ListViewItem lvi = this.listView1.Items.Add(name);
lvi.SubItems.Add(value);
}
private void button1_Click(object sender, EventArgs e)
{
string host;
int portStart = 1, portStop = 65535, ctrThread = 200;

host = textBox1.Text;

portStart = int.Parse(textBox2.Text);
portStop = int.Parse(textBox3.Text);
ctrThread = int.Parse(textBox4.Text);



PortScannerr ps = new PortScannerr(host, portStart, portStop);
ps.start(ctrThread);
}

public class PortScannerr
{
private string host;
private PortList portList;

public PortScannerr(string host, int portStart, int portStop)
{
this.host = host;
this.portList = new PortList(portStart, portStop);
}

public PortScannerr(string host)
: this(host, 1, 65535)
{
}

public PortScannerr()
: this("127.0.0.1")
{
}

public void start(int threadCtr)
{
for (int i = 0; i < threadCtr; i++)
{
Thread th = new Thread(new ThreadStart(run));
th.Start();
}
}
public void run()
{
int port;
TcpClient tcp = new TcpClient();
while ((port = portList.getNext()) != -1)
{
try
{
tcp = new TcpClient(host, port);
}
catch
{

continue;
}
finally
{
try
{
tcp.Close();
}
catch { }
}
//PortScanner oc = new PortScanner;

//MessageBox.Show(port.ToString() + " is open");
AddHeaderInformation(port.ToString(), "open");
}
}
}
public class PortList
{
private int start;
private int stop;
private int ptr;

public PortList(int start, int stop)
{
this.start = start;
this.stop = stop;
this.ptr = start;
}
public PortList()
: this(1, 65535)
{
}

public bool hasMore()
{
return (stop - ptr) >= 0;
}
public int getNext()
{
if (hasMore())
return ptr++;
return -1;
}
}
}
}

 
AddHeaderInformation(port.ToString(), "open");
is causing the problem. PortScannerr has no information about PortScanner and therefore cannot access AddHeaderInformation.

have PortScannerr return a string and have PortScanner add the header information. option 2; pass the instance of PortScanner to PortScannerr and call AddHeaderInformation.

I wouldn't nest the classes either, there is not value with nesting.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
forgive my ignorance, would you be so kind as to show me an example of the above applied to my project?

thanks again
 
you know what, once you solve this problem you will encounter another issue. You will attempt to update the UI from a background thread. this will throw an exception because the UI can only be updated from the main thread. if you want to push work to a background thread then you will need to synchronize the background thread to the UI thread. I have an FAQ on asynchronous UI calls. the link is below in my signature.

here is an example of returning the string
Code:
public void DoSomething()
{
   var instance = new Thing();
   var result = instance.DoWork(args);
   ProcessResult(result);
}

private void ProcessResult(string text)
{
   //do something with the text
}
here is an example of passing an instance of itself to another class.
Code:
class Foo
{
public void DoSomething()
{
   var instance = new Thing(this);
   var result = instance.DoWork(args);
   ProcessResult(result);
}

private void ProcessResult(string text)
{
   //do something with the text
}
}
[code]
class Thing would look like this
[code]
class Thing
{
   readonly Foo dependency;
   pubic Thing(Foo dependency)
   {
        this.dependency = dependency;
   }

   public string DoWork(...)
   {
       return "text";
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top