Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Convert CPP to C#

Posted on 2014-04-12
6
531 Views
Last Modified: 2014-04-12
How do I convert the following to C#

struct fpoint : pair<double> {
    fpoint() : pair<double>() { }
    fpoint(double x, double y) : pair<double>(x, y) { }
};
0
Comment
Question by:JElster
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 100 total points
ID: 39996739
The C# equivalent could be

public struct fpoint : System.Collections.Generic.KeyValuePair<double,double> {
    fpoint() : base() { }
    fpoint(double x, double y) : base((x, y) { }
}; 

Open in new window

0
 
LVL 1

Author Comment

by:JElster
ID: 39996749
Get a bunch of compile errors,, like missing param.
Probably close... thx
0
 
LVL 86

Expert Comment

by:jkr
ID: 39996758
Soory, had my mind tilted towards C++ a bit too much - does

public struct fpoint : System.Collections.Generic.KeyValuePair<double,double> {
    fpoint() : base() { }
    fpoint(double x, double y) : base() { key = x; value = y; }
}; 
                                            

Open in new window


work better?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:JElster
ID: 39996764
Error      3      Structs cannot contain explicit parameterless constructors      

Error      2      Type 'System.Collections.Generic.KeyValuePair<...>' in interface list is not an interface
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
ID: 39996766
I disagree slightly, but I don't think it's of major concern. From what I read, the Tuple class would be closer to the exact match for pair.

e.g.

public class fpoint : Tuple<double, double>
{
    public fpoint() : base(default(double), default(double)) { }
    public fpoint(double x, double y) : base(x, y) { }
}

Open in new window


The reason I disagree is that in C# you cannot reassign struct members once you have created the instance--you have to create a second instance with the new values. This is because KeyValuePair is a value type. A Tuple is a reference type, so you can reassign its member even after the instance is created. Albeit in either case there is no swap method, so you'd need to roll your own.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39996770
Actually, never mind. The Tuple's members are read-only also. I saw "tuple" mentioned in the documentation I linked to, so I naturally thought it would be the equivalent. I think either data structure would yield the same results.

Of course, you could always write your own class/struct to be an exact match!
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Graphics 2 32
Are the two lines the same? If not what is the difference? 2 22
Gaming Software 1 19
C# Linq - Join two objects into one 3 14
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question