?
Solved

Convert CPP to C#

Posted on 2014-04-12
6
Medium Priority
?
550 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
[X]
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
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1600 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

765 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