Can someone help me debug this snippet of code?

S

Schizoid Man

I have two classes. The first one called Program contains the Main() method.

This class ideally should be able to call a method called normDensity
from another class NormalDistribution code attached below the first class.

I instantiate the NormalDistribution class as:
NormalDistribution normal_dist = new NormalDistribution();

So technically, I should be able to call the method
normal_dist.normDensity from Main(). However, I cannot see this method.

Any ideas?

Thanks in advance.
Schiz


using System;
using System.Collections.Generic;
using System.Text;

namespace Distributions
{
class Program
{

static void Main(string[] args)
{

string tempNumber;
double inputNumber;

Console.Write("Enter a number: ");
tempNumber = Console.ReadLine();
inputNumber = Convert.ToDouble(tempNumber);

Console.Write("The pdf of {0} is {1}", inputNumber, MISSING
METHOD);

}
}
}



using System;
using System.Collections.Generic;
using System.Text;

namespace Distributions
{
class NormalDistribution
{
NormalDistribution normal_dist = new NormalDistribution();
const double pi = 3.1415926535897932384;

public static double normDensity(double x)
{
return Math.Exp(-0.5 * x * x) / Math.Sqrt(2 * pi);
}

}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Schizoid Man,

No, you can't call normal_dist.normDensity because normDensity is
declared as static. You need to call:

NormalDistiribution.normDensity

A few recommendations. First, follow the public naming conventions for
your public members. normDensity should be NormDensity.

Also, why not use the PI const on the Math class in the System
namespace? It's actually more precise than your definition by one place.

Hope this helps.
 
S

Schizoid Man

Nicholas said:
Schizoid Man,

No, you can't call normal_dist.normDensity because normDensity is
declared as static. You need to call:

NormalDistiribution.normDensity

A few recommendations. First, follow the public naming conventions for
your public members. normDensity should be NormDensity.

Also, why not use the PI const on the Math class in the System
namespace? It's actually more precise than your definition by one place.

Hope this helps.

Hi Nicholas,

Thanks - you're right. NormDensity (corrected) is declared as static and
your suggestion works. However, because it is static I wouldn't need to
instantiate the class in any case.

Suppose I changed the method to:

public double NormDensity(double x)
{
return Math.Exp(-0.5 * x * x) / Math.Sqrt(2 * pi);
}

In this case, how would I call this method from Main()?

Thanks,
Schiz
 
W

Willy Denoyette [MVP]

|I have two classes. The first one called Program contains the Main()
method.
|
| This class ideally should be able to call a method called normDensity
| from another class NormalDistribution code attached below the first class.
|
| I instantiate the NormalDistribution class as:
| NormalDistribution normal_dist = new NormalDistribution();
|
| So technically, I should be able to call the method
| normal_dist.normDensity from Main(). However, I cannot see this method.
|
| Any ideas?
|
| Thanks in advance.
| Schiz
|
|
| using System;
| using System.Collections.Generic;
| using System.Text;
|
| namespace Distributions
| {
| class Program
| {
|
| static void Main(string[] args)
| {
|
| string tempNumber;
| double inputNumber;
|
| Console.Write("Enter a number: ");
| tempNumber = Console.ReadLine();
| inputNumber = Convert.ToDouble(tempNumber);
|
| Console.Write("The pdf of {0} is {1}", inputNumber, MISSING
| METHOD);
|
| }
| }
| }
|
|
|
| using System;
| using System.Collections.Generic;
| using System.Text;
|
| namespace Distributions
| {
| class NormalDistribution
| {
| NormalDistribution normal_dist = new NormalDistribution();
| const double pi = 3.1415926535897932384;
|
| public static double normDensity(double x)
| {
| return Math.Exp(-0.5 * x * x) / Math.Sqrt(2 * pi);
| }
|
| }
| }

In your sample, normDensity is a static member function, so you can't see
(and call) it using an instance reference, you need to call it using the
class like this: NormalDistribution.normDensitynormDensity(..);
another option is to make it an instace member function by removing the
static attribute and call it as you did.

Willy.


Willy.
 
S

Schizoid Man

Willy said:
In your sample, normDensity is a static member function, so you can't see
(and call) it using an instance reference, you need to call it using the
class like this: NormalDistribution.normDensitynormDensity(..);
another option is to make it an instace member function by removing the
static attribute and call it as you did.

Hi Willy,

I was just playing around with the code to make it work, hence the
static declaration.

I had noted in an earlier message (to Nicholas) that my preference would
be to not declare it as static and instantiate the class and reference
the method that way.

However, if I try to do that, then the following piece of code:
normal_dist.NormDensity

gives me the following compile error:
The name 'normal_dist' does not exist in the current context

even though I have instantiate the class as:
NormalDistribution normal_dist = new NormalDistribution();

Thanks.
 
J

john sun

Schizoid said:
Hi Willy,

I was just playing around with the code to make it work, hence the
static declaration.

I had noted in an earlier message (to Nicholas) that my preference would
be to not declare it as static and instantiate the class and reference
the method that way.

However, if I try to do that, then the following piece of code:
normal_dist.NormDensity

gives me the following compile error:
The name 'normal_dist' does not exist in the current context

even though I have instantiate the class as:
NormalDistribution normal_dist = new NormalDistribution();

Thanks.

You may want to make your class as public, since by default, those
classes are defined as private.
 
S

Schizoid Man

john said:
You may want to make your class as public, since by default, those
classes are defined as private.

John,

I've declared the class NormalDistribution as public. No dice. :(
 
S

Schizoid Man

Willy said:
In your sample, normDensity is a static member function, so you can't see
(and call) it using an instance reference, you need to call it using the
class like this: NormalDistribution.normDensitynormDensity(..);
another option is to make it an instace member function by removing the
static attribute and call it as you did.

As expected, the problem was annoyingly simple. I was instantiating the
object in the wrong class! :)
 
G

Gino

Try something like this, maybe:

using System;
using System.Collections.Generic;
using System.Text;

namespace Distributions
{
class Program
{
//THIS IS THE PROGRAM INSERTION POINT----------
static void Main(string[] args)
{

string tempNumber;
double inputNumber;

Console.Write("Enter a number: ");
tempNumber = Console.ReadLine();
inputNumber = Convert.ToDouble(tempNumber);

//YOU HAVE TO INSTANITATE THE OTHER CLASS HERE
//AND THEN CALL ITS METHOD PASSING IN THE PARAMETER

Console.Write("The pdf of {0} is {1}", inputNumber, MISSING
METHOD);

}
}
} //YOUR PROGRAM ENDS RIGHT HERE----------


//THIS DOSEN'T RUN ONCE YOU REMOVE STATIC--------
using System;
using System.Collections.Generic;
using System.Text;

namespace Distributions
{
class NormalDistribution
{
NormalDistribution normal_dist = new NormalDistribution();
const double pi = 3.1415926535897932384;

public static double normDensity(double x)
{
return Math.Exp(-0.5 * x * x) / Math.Sqrt(2 * pi);
}

}
}
 

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

Similar Threads


Top