newbie: How to call a method from a static method?

D

deko

Is this a C# thing, or am I missing something?

public static void addProject(Array myArray)
{
//flatten out myArray into myList and
//pass it to another method in this class
ArrayList myList = New ArrayList();
//loop, flatten, nurp, etc...
someOtherMethod(myList);
}

Why am I getting this error:

"An object reference is required for the nonstatic field, method, or
property... someOtherMethod"

How do I call a method from a static method in the same class?

Thanks in advance.
 
B

Bill Butler

deko said:
Is this a C# thing, or am I missing something?

This is an object oriented THING
public static void addProject(Array myArray)
{
//flatten out myArray into myList and
//pass it to another method in this class
ArrayList myList = New ArrayList();

change New to new
//loop, flatten, nurp, etc...
someOtherMethod(myList);
}

Why am I getting this error:

"An object reference is required for the nonstatic field, method, or property... someOtherMethod"

How do I call a method from a static method in the same class?

Slow down and read the error message.

It is telling you that someOtherMethod is not static.
You can either make it static (if it makes sense to be static) or
You need to create an object and call someOtherMethod on that object

Bill
 
D

deko

You can either make it static (if it makes sense to be static) or
You need to create an object and call someOtherMethod on that object

It's obvious that I could call someOtherMethod if I instantiated an object
to do so, but it makes no sense to do that.

There is a member in the class that I need access to (an instance of the
class already exists). The reason the first method is static is because I
don't want another instance of the class in which it resides. So calling
another static method from the first method only leads to the same problem:
how to call a method in the class (from a static method) that will have
access to a private member of that class - without instantiating another
instance?

If this is not possible, I need to redesign.
 
D

Daniel

Bill was correct and it does make sense to do that as there is no other
choice due to your implementation. You have a design flaw here.

If you only ever want one instance of the object you are using use the
Singleton pattern and you will have no problems at all and no need to make
that method static. Just reading it i have no idea why you would do it that
way anyway, it's like you want to avoid using member variables or something?

As your method seems to be made to AddProject , it may be that you are
trying to use this class to spawn objects? if that is right look at the
Factory pattern as thats exactly what you need.

But as i say singleton will make your whoe class static and you can always
access that one instance like this:

public class myClass
{
public static readonly myClass Instance = new myClass();

....}

At the top of the class to be singleton. Then access it from anywhere in
your app like this:

myClass.Instance.AddProject();

And you never do a myClass m = new myClass(); because the class always uses
one instance so it never needs more to be made as the one instance is
created the moment your app starts.

Does that help? i would advise you look up design petterns, they are of
immense use once you understand them.
 
J

Jon Skeet [C# MVP]

deko said:
It's obvious that I could call someOtherMethod if I instantiated an object
to do so, but it makes no sense to do that.

There is a member in the class that I need access to (an instance of the
class already exists). The reason the first method is static is because I
don't want another instance of the class in which it resides. So calling
another static method from the first method only leads to the same problem:
how to call a method in the class (from a static method) that will have
access to a private member of that class - without instantiating another
instance?

If this is not possible, I need to redesign.

You need to ask yourself the obvious question of *which* instance you
want to call the method on. Once you've worked out which instance it
is, you need to work out how to convey that information in the code.
 
D

deko

If you only ever want one instance of the object you are using use the
Singleton pattern and you will have no problems at all and no need to make
that method static.

I will look into the singleton pattern. That seems to make sense.
Just reading it i have no idea why you would do it that way anyway, it's
like you want to avoid using member variables or something?

Well, I instantiate an object in the Data Access layer that creates a
DataSet. There are a number of forms that need to perform CRUD operations
on that DataSet. So I can't instantiate another instance of that class
whenever I need to update the DataSet.

In an attempt to keep all data access in the DA layer, I make the DataSet a
public property of class that creates it and then go through a Builder class
to get objects (Arrays mostly) from the DataSet. I pass the objects back to
the calling forms, which can then bind controls to them. The Builder class
also handles updates to the DataSet, such as that addProject method (which
is called from a form).
As your method seems to be made to AddProject , it may be that you are
trying to use this class to spawn objects? if that is right look at the
Factory pattern as thats exactly what you need.

I think my builder class woud qualify as a Factory class.
But as i say singleton will make your whoe class static and you can always
access that one instance like this:

public class myClass
{
public static readonly myClass Instance = new myClass();

...}

At the top of the class to be singleton. Then access it from anywhere in
your app like this:

myClass.Instance.AddProject();

And you never do a myClass m = new myClass(); because the class always
uses one instance so it never needs more to be made as the one instance is
created the moment your app starts.

Does that help? i would advise you look up design petterns, they are of
immense use once you understand them.

Yes, that does help. But I'm wondering if I should use a Singleton or just
keep the DataSet a public property of the class that creates it. What's the
difference?
 
D

deko

But as i say singleton will make your whoe class static and you can always
access that one instance like this:

I think a singleton is the way to go. Below is a thread-safe version (since
two threads could instantiate the MyDataSet at the same time).

public sealed class MyDataSet
{
static MyDataSet instance = null;
static readonly object objlock = new object();
MyDataSet()
{
}
public static MyDataSet Instance
{
get
{
lock (objlock)
{
if (instance == null)
{
instance = new MyDataSet();
// --logic here to get data
// --do stuff
}
return instance;
}
}
}
}

Does this look about right?
 
J

Jon Skeet [C# MVP]

deko said:
I think a singleton is the way to go. Below is a thread-safe version (since
two threads could instantiate the MyDataSet at the same time).

public sealed class MyDataSet
{
static MyDataSet instance = null;
static readonly object objlock = new object();
MyDataSet()
{
}
public static MyDataSet Instance
{
get
{
lock (objlock)
{
if (instance == null)
{
instance = new MyDataSet();
// --logic here to get data
// --do stuff
}
return instance;
}
}
}
}

Does this look about right?

Well, it's thread-safe, but it's more complicated than it needs to be.
For most cases, the following is fine:

public sealed class MyDataSet
{
static MyDataSet instance = new MyDataSet();

MyDataSet()
{
}

public static MyDataSet Instance
{
get
{
return instance;
}
}
}

It's not *quite* as "lazy" as your version, but that's rarely an issue
- and it's still fairly lazy anyway.

See http://www.pobox.com/~skeet/csharp/singleton.html for more
information.
 

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