How to pass data from a class data member to that class' display method?

S

sherifffruitfly

Hi all,

I've got a class with a private DataSet member, and a void display()
method.

The display() method is *supposed* to take the DataSet for the class
(instance), new up a predefined for which has a DataGrid, slap the
DataSet onto the DataGrid, and set the form.visible to true.

I'm having trouble with a very early part of the process. The first
line of display() gives a runtime error:

int numRows = this.ClassDataSetMember.Tables["tbl"].Rows.Count;


Primary exception:
"An unhandled exception of type 'System.NullReferenceException'
occurred in ***.dll"

Inner exception:
"Additional information: Object reference not set to an instance of an
object."


This runtime error occurs when the main code calls the object.display()
method.

What am I doing wrong? I thought "this" was the way to say "that owns
the line of code currently running" (or something like that).

How do I get my object's display() method to make use of it's own
object's data?

I'm sure I'm confused over something really stupid - thanks for any
suggestions!

cdj
 
G

Guest

Sorry if I duped a post, MS's web-based thingy has been uncooperative lately.

The "this" keyword references the current instance of a type. So for example
if you have a field in class-level scope, in your case, "ClassDataSetMember",
you could refer to it as this.ClassDataSetMember to disambiguate it from a
variable of the same name that is in a lower scope.

It seems from your description that you simply have not populated the field
with an instance of a DataSet. Here is a short example:

public class MyClass
{

public DataSet ClassDataSetMember;

public MyClass(DataSet myDataSet )
{
this.ClassDataSetMember = myDataSet;
}

}

Hope that helps.
Peter
 

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