Going to work on learning (trying) again. Starting out with this tutorial, hoping to learn quickly so I can get to a point where I can write invoicing software for our shop. Which will be running in a couple of months, if things go well.

Anyways, on to learning Very Happy
Good luck! Very Happy It's always fun to learn something new!
Ok, question time, since the tutorial doesn't really go into detail on the second example.


Code:
using System;
namespace RectangleApplication
{
 class Rectangle
 {
 // member variables
 double length;
 double width;
 public void Acceptdetails()
 {
 length = 4.5;
 width = 3.5;
 }
 public double GetArea()
 {
 return length * width;
 }
 public void Display()
 {
 Console.WriteLine("Length: {0}", length);
 Console.WriteLine("Width: {0}", width);
 Console.WriteLine("Area: {0}", GetArea());
 }
 }
 
 class ExecuteRectangle
 {
 static void Main(string[] args)
 {
 Rectangle r = new Rectangle();
r.Acceptdetails();
 r.Display();
 Console.ReadLine();
 }
 }
}


The question at the moment is on 'double'. From what I can see, it sets up a variable to be 64 bit floating point, right? As per: http://msdn.microsoft.com/en-us/library/678hzkk9.aspx

The example on that page shows storing to a variable with the double inline with the variable, where as it's separate from the example above. I added it inline, and it passed errors about not being declared, yet it still ran. I'm just curious on why it's happy the one way but not the other.
What do you mean inline? Something like double length = 4.5; should be fine. What was your specific code and the errors?

Code:
using System;
namespace RectangleApplication
{
 class Rectangle
 {
 // member variables 
 public void Acceptdetails()
 {
 double length = 4.5;
 double width = 3.5;
 }
 public double GetArea()
 {
 return length * width;
 }
 public void Display()
 {
 Console.WriteLine("Length: {0}", length);
 Console.WriteLine("Width: {0}", width);
 Console.WriteLine("Area: {0}", GetArea());
 }
 }
 
 class ExecuteRectangle
 {
 static void Main(string[] args)
 {
 Rectangle r = new Rectangle();
r.Acceptdetails();
 r.Display();
 Console.ReadLine();
 }
 }
}


Is what I did. It spit out 2 errors (don't remember what they were now) but it still ran and displayed things.
If it ran, that's because it was running a previous version (it should prompt you if there are errors if you want to run the old version). Errors mean it didn't build correctly; warnings mean it built but might be wrong.

In this case, the problem is scoping, that is, you're defining the variables within the scope of Accedptdetails, so the other functions, GetArea and Display, don't know about those variables. Scope is basically the { }. Something like this will give you more what you're thinking:

Code:
using System; 
namespace RectangleApplication 

 class Rectangle 
 { 
 // member variables 
 double length = 4.5; 
 double width = 3.5; 
 public void Acceptdetails() 
 { 
 } 
 public double GetArea() 
 { 
 return length * width; 
 } 
 public void Display() 
 { 
 Console.WriteLine("Length: {0}", length); 
 Console.WriteLine("Width: {0}", width); 
 Console.WriteLine("Area: {0}", GetArea()); 
 } 
 } 
 
 class ExecuteRectangle 
 { 
 static void Main(string[] args) 
 { 
 Rectangle r = new Rectangle();
r.Acceptdetails(); 
 r.Display(); 
 Console.ReadLine(); 
 } 
 } 
}


So, remember, if you want a variable to be visible at a certain scope (i.e., within a certain set of {}), it needs to be defined in that scope or higher (i.e., within those {}, or within {} that those {} are contained).
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement