6 Replies - 2026 Views - Last Post: 24 April 2012 - 02:37 PM Rate Topic: -----

#1 tekirodevil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-September 11

converting javascript class to C#

Posted 24 April 2012 - 12:22 PM

not sure if I should post this is java or C forum but here goes..
trying to convert this class to C# but it takes in parameters and I'm not sure how to work around that..

function Rectangle(x,y,w,h)
{
    this.x = x;
    this.y = y;
    this.width = w;
    this.height = h;
    this.right = x + w;
    this.bottom = y + h;
...//there's more but just function definitions



any assistance would be appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: converting javascript class to C#

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6037
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: converting javascript class to C#

Posted 24 April 2012 - 12:24 PM

No, you should have posted to C#. Moved.
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4436
  • View blog
  • Posts: 7,707
  • Joined: 08-June 10

Re: converting javascript class to C#

Posted 24 April 2012 - 12:35 PM

Quote

not sure if I should post this is java or C forum


Java is not Javascript, and they have nothing to do with each other.

C is not C#, and they have nothing to do with each other either.

So, neither is appropriate for this question.



Show us what you've tried first, please. We're not just going to do it for you.

Remember to use a Constructor for your class. That'll handle the parameters.
Was This Post Helpful? 0
  • +
  • -

#4 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2531
  • View blog
  • Posts: 4,631
  • Joined: 24-September 10

Re: converting javascript class to C#

Posted 24 April 2012 - 12:38 PM

this javascript class is using a function to create the object... first you have to convert that. Create a class of the same name and create a constructor with the params of that function. I don't know what types the numeric values of x,y,w,h are, I'm going with float just because... you can go with whatever you want.

public class Rectangle
{

	public Rectangle(float x, float y, float w, float h)
	{
		
	}

}



And those properties need to be defined. I'm just going to go with public fields because it's short and simple, you can make them private with getter/setters if you want:

public class Rectangle
{

	public float X;
	public float Y;
	public float Width;
	public float Height;
	public float Right;
	public float Bottom;

	public Rectangle(float x, float y, float w, float h)
	{
		X = x;
		Y = y;
		Width = w;
		Height = h;
		Right = x + w;
		Bottom = y + h;
	}

}



Then bring your functions in... noting that in C# functions are written slightly different:

js
function foo(value)
{
	//blah
}



C#
public void Foo(Object value)
{
	//blah
}



Lastly, consider that the .Net framework already has a 'Rectangle' structure defined (oh and you could make your class a struct instead if you wanted... making it a value type instead of a ref type). Creating this class might just be redundant in relation to said existing structure:

Rectangle (of ints)
http://msdn.microsof....rectangle.aspx

RectangleF (of floats)
http://msdn.microsof...rectanglef.aspx
Was This Post Helpful? 1
  • +
  • -

#5 tekirodevil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-September 11

Re: converting javascript class to C#

Posted 24 April 2012 - 12:40 PM

[quote name='lordofduct' date='24 April 2012 - 12:38 PM' timestamp='1335296302' post='1607624']
this javascript class is using a function to create the object... first you have to convert that. Create a class of the same name and create a constructor with the params of that function. I don't know what types the numeric values of x,y,w,h are, I'm going with float just because... you can go with whatever you want.

public class Rectangle
{

	public Rectangle(float x, float y, float w, float h)
	{
		
	}

}



ahh, I had completely blanked over a constructor function, keep thinking that C# is far different then C++ for some reason, thanks.
Was This Post Helpful? 0
  • +
  • -

#6 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: converting javascript class to C#

Posted 24 April 2012 - 01:30 PM

Well, much of C# is quite different from C++, but many things are still the same. A large thing for C# is that it no longer requires a destructor.
Was This Post Helpful? 0
  • +
  • -

#7 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: converting javascript class to C#

Posted 24 April 2012 - 02:37 PM

C# is an object orientated language. Unsurprisingly there is already a Rectangle class for you to use.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1