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);
}
}
} 10 1453
Gaurav Vaish ( www.EduJini.IN) wrote:
Are the two pieces of code a part of the same project?
Yes. They are.
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);
}
}
}
"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.
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
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.
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 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. :(
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! :)
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. :(
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
| |