- Home /
javascript > c# conversion help
i've looked through the page on syntax differences but i can't for the life of me figure out how to convert this.
public function get itemNA () : String
i assumed it would be string = get itemNA () but that and a bunch of other variations i tried aren't working.
it would be:
public string getItemNA()
{
// code
}
you cannot have spaces in function names. You also cannot assign something to a system type.
the syntax in c# goes like:
<visibility> <return type> <name>(<parameters>)
{
<function code>
}
Ah if you are looking for property declaration, @flaviusxvii's answer might help you ;)
This looks like a property declaration - take a look at @flaviusxvii answer. In JS, a property can be declared inside a class declaration like below:
class Data {
var _health: int = 100;
var _hasRifle = false;
var _hasPistol = false;
function get health(): int { return _health; }
function set health(value: int) { _health = value; }
function get hasRifle(): boolean { return _hasRifle; }
function set hasRifle(value: boolean) { _hasRifle = value; }
function get hasPistol(): boolean { return _hasPistol; }
function set hasPistol(value: boolean) { _hasPistol = value; }
}
var data: Data = new Data();
function Start(){
data.health = 50;
data.hasRifle = true;
print("Health = "+data.health+" Has Rifle = "+data.hasRifle);
}
Answer by flaviusxvii · Jul 10, 2013 at 11:17 PM
That "get" keyword essentially allows you to call a function but treat it like a member variable. Here is the equivalent setup in C#
public class Person
{
//default constructor
public Person()
{
}
private string _Name;
public string Name
{
//set the person name
set { this._Name = value; }
//get the person name
get { return this._Name; }
}
}
the way you formatted that is sort of confusing. here is the entire function im trying to convert
public function get itemNA () : String
{
return itemName;
}
i just need to know how this would look in C#. i don't think its neccessary to post the entire class though there is a "private string itemName" i declared at the top of it.
Your answer
Follow this Question
Related Questions
Create a guitexture When clicked on 3d text? 0 Answers
js to C# converstion Problem 3 Answers
.js to C# conversion 4 Answers
Convert js to C# Serializer problem 1 Answer