PC Review


Reply
Thread Tools Rate Thread

Can someone help me debug this snippet of code?

 
 
Schizoid Man
Guest
Posts: n/a
 
      27th Aug 2006
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);
}

}
}
 
Reply With Quote
 
 
 
 
Gaurav Vaish \(www.EduJini.IN\)
Guest
Posts: n/a
 
      27th Aug 2006
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?




 
Reply With Quote
 
Schizoid Man
Guest
Posts: n/a
 
      27th Aug 2006
Gaurav Vaish (www.EduJini.IN) wrote:

> Are the two pieces of code a part of the same project?


Yes. They are.
 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      27th Aug 2006
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]
- (E-Mail Removed)

"Schizoid Man" <(E-Mail Removed)> wrote in message
news:ecsqbg$1f4$(E-Mail Removed)...
>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);
> }
>
> }
> }



 
Reply With Quote
 
Schizoid Man
Guest
Posts: n/a
 
      27th Aug 2006
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

 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      27th Aug 2006

"Schizoid Man" <(E-Mail Removed)> wrote in message
news:ecsqbg$1f4$(E-Mail Removed)...
|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.



 
Reply With Quote
 
Schizoid Man
Guest
Posts: n/a
 
      27th Aug 2006
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.
 
Reply With Quote
 
john sun
Guest
Posts: n/a
 
      27th Aug 2006
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.
 
Reply With Quote
 
Schizoid Man
Guest
Posts: n/a
 
      27th Aug 2006
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.
 
Reply With Quote
 
Schizoid Man
Guest
Posts: n/a
 
      27th Aug 2006
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!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Help-Intellisense (Code Snippet) Tab Tab does not insert code (VS2008) Jeff Johnson Microsoft C# .NET 0 12th Jul 2010 10:37 PM
Code snippet tshad Microsoft C# .NET 3 22nd Oct 2008 05:13 PM
Code Snippet Add-In?? Don Microsoft VB .NET 3 24th Sep 2003 09:24 AM
Re: code snippet help please Junior Microsoft Access Form Coding 0 7th Jul 2003 02:13 PM
Re: code snippet help please Allen Browne Microsoft Access Form Coding 0 7th Jul 2003 01:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:21 PM.