C# Creating a class and assigning properties to it.
-
I am just starting to learn C# so my question will likely seem stupid to most people looking at this. I am just getting to the part about creating classes and turning them in to objects. My questions have to do with the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace circle
{
class circle
{
int center_x;
int center_y;
double Radius;public int x { get { return center\_x;} set { center\_x = value;} } public int y { get { return center\_y;} set { center\_y = value;} } public double radius { get { return Radius;} set { Radius = value;} } } class MyApp { public static void Main() { circle myCircle = new circle(); myCircle.x = 10; myCircle.y = 10; myCircle.radius = 8; Console.WriteLine("Center: = {0},{1}", myCircle.x, myCircle.y); Console.WriteLine("Radius: = {0}", myCircle.radius); Console.WriteLine("Circumference: = {0}", (double)(2 \* Math.PI \* myCircle.radius)); } }
}
My first question is with the variable Radius why is it that where it is declared as public double radius followed by the properties that you have to use a small r when all the other instances of Radius use a cap? When you use a cap R it returns the error message "ambiguity between circle.circle.Radius and circle.circle.Radius" How do you compare two things exactly the same? Also why is this rule not true for the x and the y? My other question (the big one) is what does any of the code down to class MyApp have to do with the code in class MyApp? Obviously I'm not going to get far in C# if I can't understand the most important part of it. Thanks for any help anyone can give me.
-
I am just starting to learn C# so my question will likely seem stupid to most people looking at this. I am just getting to the part about creating classes and turning them in to objects. My questions have to do with the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace circle
{
class circle
{
int center_x;
int center_y;
double Radius;public int x { get { return center\_x;} set { center\_x = value;} } public int y { get { return center\_y;} set { center\_y = value;} } public double radius { get { return Radius;} set { Radius = value;} } } class MyApp { public static void Main() { circle myCircle = new circle(); myCircle.x = 10; myCircle.y = 10; myCircle.radius = 8; Console.WriteLine("Center: = {0},{1}", myCircle.x, myCircle.y); Console.WriteLine("Radius: = {0}", myCircle.radius); Console.WriteLine("Circumference: = {0}", (double)(2 \* Math.PI \* myCircle.radius)); } }
}
My first question is with the variable Radius why is it that where it is declared as public double radius followed by the properties that you have to use a small r when all the other instances of Radius use a cap? When you use a cap R it returns the error message "ambiguity between circle.circle.Radius and circle.circle.Radius" How do you compare two things exactly the same? Also why is this rule not true for the x and the y? My other question (the big one) is what does any of the code down to class MyApp have to do with the code in class MyApp? Obviously I'm not going to get far in C# if I can't understand the most important part of it. Thanks for any help anyone can give me.
There are some conventions: - public properties/methods: CamelCase - protected/private data: pascalCase hence "private int radius" and "public int Radius { get; set;}" IMO you should buy and study a book to learn a new language and get the basics right; it is the one efficient way to get good results. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
There are some conventions: - public properties/methods: CamelCase - protected/private data: pascalCase hence "private int radius" and "public int Radius { get; set;}" IMO you should buy and study a book to learn a new language and get the basics right; it is the one efficient way to get good results. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Thank you for your reply. I did use the code block to insert the code. I am using a book - Sams Learn C# in 21 Days by Bradley Jones. It basically seems well written and I have managed fine to this point which isn't all that far. At this point he has given me a listing and the analysis at the end says "Lines 14 - 22 create the property capabilities for the variables int center_x;, int center_y;" What he hasn't bothered to explain is what these property capabilities are and how they apply to the variables after class MyApp. This brings up another point: the variables Mycircle.x =10 , etc. are assigned values but I can't see any where that this variable was declared. Obviously I am in way over my head without some instruction. Again, thank you for your time. Darrall :)
-
Thank you for your reply. I did use the code block to insert the code. I am using a book - Sams Learn C# in 21 Days by Bradley Jones. It basically seems well written and I have managed fine to this point which isn't all that far. At this point he has given me a listing and the analysis at the end says "Lines 14 - 22 create the property capabilities for the variables int center_x;, int center_y;" What he hasn't bothered to explain is what these property capabilities are and how they apply to the variables after class MyApp. This brings up another point: the variables Mycircle.x =10 , etc. are assigned values but I can't see any where that this variable was declared. Obviously I am in way over my head without some instruction. Again, thank you for your time. Darrall :)
1. if that is code from a book, the book is no good and isn't worth spending many hours at. 2.
myCircle.x = 10;
is how you set a new value to either a data member (that would be a "public int x;") or the set part of a property (which should start with a capital!). I suggest you read the chapter on properties again; if that does not help, there's another reason to scrap that book. 3. the PRE tags thingy is part of my sig, it gets added at the bottom to remind everyone about code formatting, it wasn't there as a reaction on your message. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
I am just starting to learn C# so my question will likely seem stupid to most people looking at this. I am just getting to the part about creating classes and turning them in to objects. My questions have to do with the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace circle
{
class circle
{
int center_x;
int center_y;
double Radius;public int x { get { return center\_x;} set { center\_x = value;} } public int y { get { return center\_y;} set { center\_y = value;} } public double radius { get { return Radius;} set { Radius = value;} } } class MyApp { public static void Main() { circle myCircle = new circle(); myCircle.x = 10; myCircle.y = 10; myCircle.radius = 8; Console.WriteLine("Center: = {0},{1}", myCircle.x, myCircle.y); Console.WriteLine("Radius: = {0}", myCircle.radius); Console.WriteLine("Circumference: = {0}", (double)(2 \* Math.PI \* myCircle.radius)); } }
}
My first question is with the variable Radius why is it that where it is declared as public double radius followed by the properties that you have to use a small r when all the other instances of Radius use a cap? When you use a cap R it returns the error message "ambiguity between circle.circle.Radius and circle.circle.Radius" How do you compare two things exactly the same? Also why is this rule not true for the x and the y? My other question (the big one) is what does any of the code down to class MyApp have to do with the code in class MyApp? Obviously I'm not going to get far in C# if I can't understand the most important part of it. Thanks for any help anyone can give me.
I suggest that you read this CodeProject article[^] to get a grasp of object oriented programming. (Wait until you find out that C# lets you define a a type and a property with exactly the same. That'll fry your brain.)
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
I am just starting to learn C# so my question will likely seem stupid to most people looking at this. I am just getting to the part about creating classes and turning them in to objects. My questions have to do with the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace circle
{
class circle
{
int center_x;
int center_y;
double Radius;public int x { get { return center\_x;} set { center\_x = value;} } public int y { get { return center\_y;} set { center\_y = value;} } public double radius { get { return Radius;} set { Radius = value;} } } class MyApp { public static void Main() { circle myCircle = new circle(); myCircle.x = 10; myCircle.y = 10; myCircle.radius = 8; Console.WriteLine("Center: = {0},{1}", myCircle.x, myCircle.y); Console.WriteLine("Radius: = {0}", myCircle.radius); Console.WriteLine("Circumference: = {0}", (double)(2 \* Math.PI \* myCircle.radius)); } }
}
My first question is with the variable Radius why is it that where it is declared as public double radius followed by the properties that you have to use a small r when all the other instances of Radius use a cap? When you use a cap R it returns the error message "ambiguity between circle.circle.Radius and circle.circle.Radius" How do you compare two things exactly the same? Also why is this rule not true for the x and the y? My other question (the big one) is what does any of the code down to class MyApp have to do with the code in class MyApp? Obviously I'm not going to get far in C# if I can't understand the most important part of it. Thanks for any help anyone can give me.
-
1. if that is code from a book, the book is no good and isn't worth spending many hours at. 2.
myCircle.x = 10;
is how you set a new value to either a data member (that would be a "public int x;") or the set part of a property (which should start with a capital!). I suggest you read the chapter on properties again; if that does not help, there's another reason to scrap that book. 3. the PRE tags thingy is part of my sig, it gets added at the bottom to remind everyone about code formatting, it wasn't there as a reaction on your message. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
I suggest that you read this CodeProject article[^] to get a grasp of object oriented programming. (Wait until you find out that C# lets you define a a type and a property with exactly the same. That'll fry your brain.)
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
I notice that Luc suggests your book may not be the best teaching aid. I used .Net Book Zero[^] by Charles Petzold, to learn C# fundamentals and can recommend it for any beginner.
-
I am just starting to learn C# so my question will likely seem stupid to most people looking at this. I am just getting to the part about creating classes and turning them in to objects. My questions have to do with the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace circle
{
class circle
{
int center_x;
int center_y;
double Radius;public int x { get { return center\_x;} set { center\_x = value;} } public int y { get { return center\_y;} set { center\_y = value;} } public double radius { get { return Radius;} set { Radius = value;} } } class MyApp { public static void Main() { circle myCircle = new circle(); myCircle.x = 10; myCircle.y = 10; myCircle.radius = 8; Console.WriteLine("Center: = {0},{1}", myCircle.x, myCircle.y); Console.WriteLine("Radius: = {0}", myCircle.radius); Console.WriteLine("Circumference: = {0}", (double)(2 \* Math.PI \* myCircle.radius)); } }
}
My first question is with the variable Radius why is it that where it is declared as public double radius followed by the properties that you have to use a small r when all the other instances of Radius use a cap? When you use a cap R it returns the error message "ambiguity between circle.circle.Radius and circle.circle.Radius" How do you compare two things exactly the same? Also why is this rule not true for the x and the y? My other question (the big one) is what does any of the code down to class MyApp have to do with the code in class MyApp? Obviously I'm not going to get far in C# if I can't understand the most important part of it. Thanks for any help anyone can give me.
I don't understand your question completely but this may be help :) 1- C# is case sensitive , mean that
double Radius;
andpublic double radius
are not same. 2- Properties is not variable, it's a way for set or get a variable , and variable can be different type from it's properties like:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace circle
{
class circle
{
double Radius;
public string radius
{
get
{
return Radius.ToString();
}
set
{
double i = -1;
if (double.TryParse(value, out i))
{
Radius = i;
}
}
}public string Circumference { get { return (2 \* Math.PI \* Radius).ToString("N6"); } } } class MyApp { public static void Main() { circle myCircle = new circle(); myCircle.radius = Console.ReadLine(); Console.WriteLine("Radius: = {0}", myCircle.radius); Console.WriteLine("Circumference: = {0}", myCircle.Circumference); } }
}
3- Properties is not only for return existing variable, it can do process for calculating or formatting return value like Circumference properties in above code 4- Properties can be readonly like Circumference properties which you can not set a value for it Hope helps with this ugly sample code ;)
-
I don't understand your question completely but this may be help :) 1- C# is case sensitive , mean that
double Radius;
andpublic double radius
are not same. 2- Properties is not variable, it's a way for set or get a variable , and variable can be different type from it's properties like:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace circle
{
class circle
{
double Radius;
public string radius
{
get
{
return Radius.ToString();
}
set
{
double i = -1;
if (double.TryParse(value, out i))
{
Radius = i;
}
}
}public string Circumference { get { return (2 \* Math.PI \* Radius).ToString("N6"); } } } class MyApp { public static void Main() { circle myCircle = new circle(); myCircle.radius = Console.ReadLine(); Console.WriteLine("Radius: = {0}", myCircle.radius); Console.WriteLine("Circumference: = {0}", myCircle.Circumference); } }
}
3- Properties is not only for return existing variable, it can do process for calculating or formatting return value like Circumference properties in above code 4- Properties can be readonly like Circumference properties which you can not set a value for it Hope helps with this ugly sample code ;)