473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Can someone help me debug this snippet of code?

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);
}

}
}
Aug 27 '06 #1
10 1453
Are the two pieces of code a part of the same project?

--
Happy Hacking,
Gaurav Vaish | http://www.mastergaurav.org
http://www.edujini.in | http://webservices.edujini.in
-------------------
So technically, I should be able to call the method
normal_dist.normDensity from Main(). However, I cannot see this method.

Any ideas?


Aug 27 '06 #2
Gaurav Vaish (www.EduJini.IN) wrote:
Are the two pieces of code a part of the same project?
Yes. They are.
Aug 27 '06 #3
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Schizoid Man" <sc***@sf.comwrote in message
news:ec**********@geraldo.cc.utexas.edu...
>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);
}

}
}

Aug 27 '06 #4

"Schizoid Man" <sc***@sf.comwrote in message
news:ec**********@geraldo.cc.utexas.edu...
|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.

Aug 27 '06 #5
Nicholas Paldino [.NET/C# MVP] wrote:
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

Aug 27 '06 #6
Willy Denoyette [MVP] wrote:
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.
Aug 27 '06 #7
Schizoid Man wrote:
Willy Denoyette [MVP] wrote:
>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.
You may want to make your class as public, since by default, those
classes are defined as private.
Aug 27 '06 #8
john sun wrote:
Schizoid Man wrote:
>Willy Denoyette [MVP] wrote:
>>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.

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. :(
Aug 27 '06 #9
Willy Denoyette [MVP] wrote:
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! :)
Aug 27 '06 #10
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);
}

}
}

"Schizoid Man" <sc***@sf.comwrote in message
news:ec**********@geraldo.cc.utexas.edu...
john sun wrote:
>Schizoid Man wrote:
>>Willy Denoyette [MVP] wrote:
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.

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. :(

Aug 27 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: marcus | last post by:
I have this class A that contains a method A_method that opens a file and does fgets. I also have a template class B that contains a method B_method that takes a class A object as template type....
6
by: Helmut Giese | last post by:
Hello out there, I am a rather experienced C programmer. However, today I got a javascript assignment because someone left (something like: "You're a great programmer - you'll handle this.") and I...
6
by: MLH | last post by:
I have this little snippet running whenever I want to list my table field names in the debug window, along with their field types... For i = 0 To TempRecordset.Fields.Count - 1 Debug.Print...
10
by: Scott | last post by:
I have a simple asp.net app which works fine in debug mode, but crashes on the following line when I run it on the production server: Dim dt As DataTable I have tried the following variations...
1
by: Bob Day | last post by:
Consider the two code snippets below. a) During development, if you hold your cursor over AKA.SomeDateTime in Snippet 2, it will indicate a value of #1/1/1950# as expected. b) If you...
2
by: kevinding | last post by:
Hi All, We meet an evil condition for our project. Our project has 3 layers. A C# layer to do some business logic, and Managed C++ layer translate managed values to native ones or vice...
3
by: psree | last post by:
Hi Below is the snippet of the code C/C++ Prototype extern “C” short FAR PASCAL RP1210_ClientConnect ( HWND hwndClient, short nDeviceID, char far* fpchProtocol,
10
by: Peter Morris | last post by:
In Delphi I used to use Assert as a development aid. During debug it would ensure that certain conditions were met but in the Release build the Asserts were not compiled into the application. I...
4
by: tshad | last post by:
I was watching a video that used a code snippet to create a property and when you type "prop" tab tab, it would create the private variable as well as the property definitions with the private...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.