C# Add Function Question

Hi,

Could anyone explain this function Add please?

the ..... dots too.

Thank you,

public class Vertex3d
{ 
.. .
public static Vertex3d Add(Vertex3d a, Vertex3d b)
{ 
Vertex3d result = new Vertex3d();
result. X = a. X + b. X;
result. Y = a. Y + b. Y;
result. Z = a. Z + b. Z;
return result;
}
}
The static method is called like in this example:
Vertex3d a = new Vertex3d(0, 0, 1) ;
Vertex3d b = new Vertex3d(1 , 0, 1) ;
Vertex3d sum = Vertex3d. Add( a, b);

Open in new window

Rad1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AshokSr. Software EngineerCommented:
Vertex3d a = new Vertex3d(0, 0, 1) ;
                                           x, y,  z
You are declaring Vertex3d type with x=0, y=0, z=1

Vertex3d b = new Vertex3d(1 , 0, 1) ;
                                           x,  y,  z
You are declaring Vertex3d type with x=1,y=0, z=1

Once they are declared,
a.x can be read

To write back,
you use
result.x = a.x + b.x;
result.y = a.y + b.y;
result.z = a.z + b.z;

and
return result
which is of type Vertex3d

HTH
Ashok

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AshokSr. Software EngineerCommented:
In line 16, you are passing both Vertex3d to the function
and
assigning result to sum

Ashok
AshokSr. Software EngineerCommented:
BTW, code for the constructor is

public Vertex3d(double x, double y, double z)
{
  this._x =x;
  this._y = y;
  this._z = z;
}

That is why you can access a.x, a.y and a.z
and
b.x, b.y and b.z

HTH
Ashok
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

shadow77Commented:
It looks like this is sample code and there were some errors introduced (extra spaces).

This line means nothing (note the extra space)
.. .
It just signifies that something was there but whoever posted the example considered that something to be unimportant.  Either delete it or put // in front of it.

These lines had extra spaces inserted after the periods (so they won't compile correctly):
result. X = a. X + b. X;
result. Y = a. Y + b. Y;
result. Z = a. Z + b. Z;

They should be replaced by:
result.X = a.X + b.X;
result.Y = a.Y + b.Y;
result.Z = a.Z + b.Z;

The corrected Add function is simply adding two three dimensional vectors (by adding their corresponding components) and returning the resulting vector.  In the example, the result (sum) will be { 1, 0, 2 }.

SriVaddadiCommented:
public class Vertex3d //Declare the class
{
.. .
//Declare a static method to add two Vertex3d objects. To call static methods, instance is not required
public static Vertex3d Add(Vertex3d a, Vertex3d b)  
{
 //Creating a Vertex3d object by calling default constructor - no argument constructor    
Vertex3d result = new Vertex3d();

//result.X  is accessing the X value of Vertex3d object result
//a.X  is accessing the X value of Vertex3d object a
//b.X  is accessing the X value of Vertex3d object b
//Adding the x value of object a and X value of object b and assigning it to x value of object result      
result. X = a. X + b. X;  
result. Y = a. Y + b. Y; //Same as X
result. Z = a. Z + b. Z; //Same as X
return result; //Returning the vertex3d object as a result of adding Vertex3d object a and Vertex3d object b
}
}
The static method is called like in this example:
Vertex3d a = new Vertex3d(0, 0, 1) ;   //Creating Vertext3d object a by calling 3 argument constructor
Vertex3d b = new Vertex3d(1 , 0, 1) ;  //Creating Vertext3d object b by calling 3 argument constructor
Vertex3d sum = Vertex3d. Add( a, b); //Calling static method Add of Vertex3d to add Vertex3d object a and Vertex3d object b


dot is used to access the public members of a class. Static members are accessed by <ClassName>.<MemberName> and instance members are accessed by <InstanceReference>.<MemberName>
AshokSr. Software EngineerCommented:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    public class Vertex3d
    {
        public double x {get; set;}
        public double y { get; set; }
        public double z { get; set; }

        public Vertex3d()
        {
            x = y = z = 0.0;
        }

        public Vertex3d(double X, double Y, double Z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public static Vertex3d Add(Vertex3d a, Vertex3d b)
        {
            Vertex3d result = new Vertex3d();
            result.x = a.x + b.x;
            result.y = a.y + b.y;
            result.z = a.z + b.z;
            return result;
        }

        static Vertex3d a = new Vertex3d(0, 0, 1);
        static Vertex3d b = new Vertex3d(1, 0, 1);
        Vertex3d sum = Vertex3d.Add(a, b);
    }
}

Vertex3d a = new Vertex3d(0, 0, 1) ;
                                           x, y,  z
You are declaring Vertex3d type with x=0, y=0, z=1

Vertex3d b = new Vertex3d(1 , 0, 1) ;
                                           x,  y,  z
You are declaring Vertex3d type with x=1,y=0, z=1

Once they are declared,
a.x can be read

To write back,
you use
result.x = a.x + b.x;
result.y = a.y + b.y;
result.z = a.z + b.z;

and
return result
which is of type Vertex3d

HTH
Ashok
Rad1Author Commented:
Thank you very much for all your help.

It is clear now.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.