1 Replies - 386 Views - Last Post: 03 December 2012 - 07:50 PM Rate Topic: -----

#1 RangerDanger816  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-October 12

Converting Java codes into C#

Posted 03 December 2012 - 07:40 PM

Hi, I have to convert some Java codes into C# (I'm using Visual Studios)
I am really confused on C#'s arrays.

This is the java version:

public class Character {
	//non-relevant codes here
	private Skill [] skills;
	private Item [] items;
	
public Character() 
       {
		
	skills = new Skill[5];
	items = new Item[5];
	}




Oops I submitted it before I was done.

public class Character 
{
	//non-relevant codes here
	private Skill [] skills;
	private Item [] items;
	
        public Character() 
        {	
	     skills = new Skill[5];
	     items = new Item[5];
	}

        public Skill getSkill(int i) 
        {
	     if(i <= numSkills) 
             {
	          return skills[i];
	     }
	     return null;
	}
		
	public Item getItem(int i) 
        {
	     if(i <= numItems) 
             {
		return items[i];
	     }
	     return null;
	}

	public Skill[] getSkills() { return skills;}
	public Item[] getItems() { return items; }





How do i convert Java arrays into C# arrays?

Is This A Good Question/Topic? 0
  • +

Replies To: Converting Java codes into C#

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Converting Java codes into C#

Posted 03 December 2012 - 07:50 PM

public Class Character {
    private Skill[] skills;
    private Item[] items;

    public Character() {
        skills = new Skill[5];
        items = new Item[5];
    }

    public Skill GetSkill(int i) {
        if (i < skills.Length) {
            return skills[i];
        }

        return null;
    }

    public Item GetItem(int i) {
        if (i < items.Length) {
            return items[i];
        }

        return null;
    }

    public Skill[] Skills { get { return skills; } }
    public Item[] Items { get { return items; }}
}


Note: The constructor creates space to hold 5 skills/items, but it does not create those skills/items!

Note2: Providing access to internal structures (the arrays) is generally a bad thing. Someone using this class can alter the skills/items without having to go through the Character class.

Edit: fixed a typo

This post has been edited by Momerath: 03 December 2012 - 07:55 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1