Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Creating a class and assigning properties to it.

C# Creating a class and assigning properties to it.

Scheduled Pinned Locked Moved C#
csharphelpquestionlinq
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Darrall
    wrote on last edited by
    #1

    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.

    L realJSOPR L H 4 Replies Last reply
    0
    • D Darrall

      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.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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


      D 1 Reply Last reply
      0
      • L Luc Pattyn

        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


        D Offline
        D Offline
        Darrall
        wrote on last edited by
        #3

        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 :)

        L 1 Reply Last reply
        0
        • D 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 :)

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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


          D 1 Reply Last reply
          0
          • D Darrall

            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.

            realJSOPR Online
            realJSOPR Online
            realJSOP
            wrote on last edited by
            #5

            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

            D 1 Reply Last reply
            0
            • D Darrall

              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.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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.

              D 1 Reply Last reply
              0
              • L Luc Pattyn

                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


                D Offline
                D Offline
                Darrall
                wrote on last edited by
                #7

                Thanks Luc. I can see where the book I am using leaves something to be desired. The information you have given me helps immensely and I think I can wade through it now. Thanks again :)

                1 Reply Last reply
                0
                • realJSOPR realJSOP

                  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

                  D Offline
                  D Offline
                  Darrall
                  wrote on last edited by
                  #8

                  Thanks for your reply John. The article you directed me to is great! Thanks again :)

                  1 Reply Last reply
                  0
                  • L Lost User

                    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.

                    D Offline
                    D Offline
                    Darrall
                    wrote on last edited by
                    #9

                    Thanks for your reply Richard. I downloaded the book and it looks like it will be a great help. Thanks again :)

                    1 Reply Last reply
                    0
                    • D Darrall

                      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.

                      H Offline
                      H Offline
                      hamed vojdani
                      wrote on last edited by
                      #10

                      I don't understand your question completely but this may be help :) 1- C# is case sensitive , mean that double Radius; and public 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 ;)

                      D 1 Reply Last reply
                      0
                      • H hamed vojdani

                        I don't understand your question completely but this may be help :) 1- C# is case sensitive , mean that double Radius; and public 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 ;)

                        D Offline
                        D Offline
                        Darrall
                        wrote on last edited by
                        #11

                        Thank you for your reply. You helped very much with your example. I am starting to understand it now. Thanks again :)

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups