Click here to Skip to main content
15,914,419 members
Home / Discussions / C#
   

C#

 
AnswerRe: Primitive types or object types what to prefer Pin
Luc Pattyn13-Jan-07 8:50
sitebuilderLuc Pattyn13-Jan-07 8:50 
GeneralRe: Primitive types or object types what to prefer Pin
ComCoderCsharp13-Jan-07 10:51
ComCoderCsharp13-Jan-07 10:51 
GeneralRe: Primitive types or object types what to prefer Pin
Kevin McFarlane13-Jan-07 11:26
Kevin McFarlane13-Jan-07 11:26 
GeneralRe: Primitive types or object types what to prefer Pin
Scott Dorman14-Jan-07 3:38
professionalScott Dorman14-Jan-07 3:38 
GeneralRe: Primitive types or object types what to prefer Pin
Luc Pattyn14-Jan-07 3:55
sitebuilderLuc Pattyn14-Jan-07 3:55 
GeneralRe: Primitive types or object types what to prefer Pin
Scott Dorman14-Jan-07 4:15
professionalScott Dorman14-Jan-07 4:15 
GeneralRe: Primitive types or object types what to prefer Pin
ComCoderCsharp14-Jan-07 4:54
ComCoderCsharp14-Jan-07 4:54 
GeneralRe: Primitive types or object types what to prefer Pin
Scott Dorman14-Jan-07 5:18
professionalScott Dorman14-Jan-07 5:18 
You are essentially correct. The benefit to defining your own struct is that you can then ecapsulate the two parts of a complex number and define the arithmetic rules (+, -, etc.) which allows you to treat the complex number as if it were a native data type.

I'm not sure why you would want to use arrays in the way you are showing. Defining a ComplexNumber struct would allow you to do something like this:

ComplexNumber c = new ComplexNumber(real, imaginary);
Console.WriteLine("Complex Number Real: {0}, Imaginary: {1}", c.Real, c.Imaginary);
 
ComplexNumber d = new ComplexNumber(real2, imaginary2);
ComplexNumber e = c + d;
 
Console.WriteLine("Complex Number Real: {0}, Imaginary: {1}", e.Real, e.Imaginary);


The benefit here is that the ComplexNumber knows how to add two complex numbers together and the calling code doesn't need that complexity.

If you wanted to use this in an array, it would be something like this:

Complex[] complexArray = new Complex[3];

complexArray[0] = new ComplexNumber(real, imaginary);
complexArray[1] = new ComplexNumber(real2, imaginary2);
complexArray[2] = new ComplexNumber(real3, imaginary3);



-----------------------------
In just two days, tomorrow will be yesterday.

GeneralRe: Primitive types or object types what to prefer Pin
ComCoderCsharp14-Jan-07 5:41
ComCoderCsharp14-Jan-07 5:41 
GeneralRe: Primitive types or object types what to prefer Pin
Scott Dorman14-Jan-07 5:47
professionalScott Dorman14-Jan-07 5:47 
AnswerRe: Primitive types or object types what to prefer Pin
Luc Pattyn14-Jan-07 12:14
sitebuilderLuc Pattyn14-Jan-07 12:14 
QuestionC# or C++ and why ? Pin
Software_Specialist13-Jan-07 5:54
Software_Specialist13-Jan-07 5:54 
AnswerRe: C# or C++ and why ? Pin
Ed.Poore13-Jan-07 7:56
Ed.Poore13-Jan-07 7:56 
AnswerRe: C# or C++ and why ? Pin
Colin Angus Mackay13-Jan-07 9:20
Colin Angus Mackay13-Jan-07 9:20 
GeneralRe: C# or C++ and why ? Pin
Software_Specialist13-Jan-07 22:43
Software_Specialist13-Jan-07 22:43 
GeneralRe: C# or C++ and why ? Pin
Nadia Monalisa13-Jan-07 23:45
Nadia Monalisa13-Jan-07 23:45 
GeneralRe: C# or C++ and why ? Pin
Colin Angus Mackay14-Jan-07 3:26
Colin Angus Mackay14-Jan-07 3:26 
QuestionA user-interface question Pin
Zerox MXI13-Jan-07 1:59
Zerox MXI13-Jan-07 1:59 
AnswerRe: A user-interface question Pin
Thomas Stockwell13-Jan-07 5:23
professionalThomas Stockwell13-Jan-07 5:23 
QuestionSomething like chat Pin
CodeItWell13-Jan-07 0:21
CodeItWell13-Jan-07 0:21 
AnswerRe: Something like chat Pin
Thomas Stockwell13-Jan-07 5:24
professionalThomas Stockwell13-Jan-07 5:24 
QuestionQuestion in C# about Interfaces...... Pin
Yanshof13-Jan-07 0:16
Yanshof13-Jan-07 0:16 
AnswerRe: Question in C# about Interfaces...... Pin
Uri Lavi13-Jan-07 5:44
Uri Lavi13-Jan-07 5:44 
GeneralRe: Question in C# about Interfaces...... Pin
Yanshof13-Jan-07 5:54
Yanshof13-Jan-07 5:54 
QuestionWhy does notifyicon disappear when stop a service [modified] Pin
l.m.k12-Jan-07 23:26
l.m.k12-Jan-07 23:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.