BCE0120: 'System.Collections.Hashtable.constructor' is inaccessible due to its protec

I never thought I would go to the times where I was flooding the forum with questions, but it is seems that U3 won’t let me off the hook !

The faulty line is :

The function is :

function GetEnvironmentDataCopy () : Object []
{
	if (environmentData)
	{
		var environmentDataCopy : Object [] = new Object [environmentData.length];
		for (var y : int = 0; y < environmentData.length; y++)
		{
			var rowOriginal : Hashtable [] = environmentData [y];
			var rowCopy : Hashtable [] = new Hashtable [rowOriginal.length];
			
			environmentDataCopy [y] = rowCopy;
			for (var x : int = 0; x < rowOriginal.length; x++)
			{
				var dataOriginal : Hashtable = rowOriginal [x];
				var dataCopy : Hashtable = new Hashtable (dataOriginal);
				
				rowCopy [x] = dataCopy;
			}
		}
		
		return environmentDataCopy;
	}
	else
	{
		Debug.LogError ("No environment data to clone");
		Debug.Break ();
	}
}

What should I do ?

I do not know UniJava but would this not work?

var dataCopy : new Hashtable (dataOriginal);

The MSDN says that constructor is available, but maybe it’s not implemented in Mono and/or bug and/or issue with the Unity compiler?

Regardless, that constructor only copies the elements over, so you can get around it by doing that yourself.

Raaah…sigh

@zumwalt : I highly doubt it would do the job, since you specify the type after :
not the content :confused:

AkilaeTribe, are your environment variables fixed? That is, will you always have “SkyColour”, “FloorSize”, and so on, or will you be dynamically adding unique ones at run-time? Like would the user be adding their own with their own custom name?

If they are all fixed names, then unless you have tonnes and tonnes of properties, perhaps you should consider making a custom class with those names as properties; might be easier to copy and manage.

My variables are not all fixed yet, although I managed to think about nearly 75-90 % of all possibilities I will require now or later (I think).

A custom class instead of Hashtable (whose common point is that they are given by reference) ?

Sure, why not ? I will think about it a little later, it could be interesting.

Since you can’t even write new Hashtable (toCopy) anymore grunt

Unless I meet problems when writing the constructor…

Well, if it is the type after the : and not the content, how about

I appreciate your efforts but the problem come from new Hashtable (dataOriginal) :slight_smile: it seems that Unity won’t let me create a new Hashtable and copying at the same time all the key/values of the original Hashtable.

Working as intended, or bug, I can only wait for someone to read that topic and answer :slight_smile:

using UnityEngine;
using System.Collections;

public class hashbash : MonoBehaviour {
    Hashtable ht, hclone;
    void Awake()
    {
        ht = new Hashtable();
        hclone = new Hashtable();
    }

	void Start () {
        ht.Add("1", "one");
        ht.Add("2", "two");
        ht.Add("3", "three");

        Debug.Log("ht contains " + ht.Count + " items and hclone contains " + hclone.Count + " items, performing copy from ht to hclone");
        foreach (DictionaryEntry entry in ht)
        {
            object key = entry.Key;
            object value = entry.Value;
            hclone.Add(key, value);
        }
        
        Debug.Log("ht contains " + ht.Count + " items and hclone contains " + hclone.Count + " items, should equal ht, now clearning ht");
        ht.Clear();
        Debug.Log("ht contains " + ht.Count + " items and hclone contains " + hclone.Count + " items, ht should be empty and hclone has our items");

        Debug.Log("Showing our hclone items now");
        foreach (DictionaryEntry de in hclone)
        {
            Debug.Log("Key = "+de.Key+ ", Value = " + de.Value);
        }

        Debug.Log("showing our ht items, should be empty");
        foreach (DictionaryEntry de in ht)
        {
            Debug.Log("Key = " + de.Key + ", Value = " + de.Value);
        }
	}
}

It works in under 6 lines of code, the entire copy is done in the one foreach loop, don’t know how to translate this to your UniJava code.

This is…C#… é_è …

var copy = new HashTable(myHashTable.Count);
for (var entry in myHashTable)
{
	copy[entry.Key] = entry.Value;
}

(it might be “foreach” in UnityScript, not “for”)

var ht;
var hclone;
function Awake()
{
    ht = new Hashtable();
    hclone = new Hashtable();
}

function Start()
{
    ht.Add("1", "one");
    ht.Add("2", "two");
    ht.Add("3", "three");

    Debug.Log("ht contains " + ht.Count + " items and hclone contains " + hclone.Count + " items, performing copy from ht to hclone");
    for (entry in ht)
    {
        var key = entry.Key;
        var value = entry.Value;
        hclone.Add(key, value);
    }
        
    Debug.Log("ht contains " + ht.Count + " items and hclone contains " + hclone.Count + " items, should equal ht, now clearning ht");
    ht.Clear();
    Debug.Log("ht contains " + ht.Count + " items and hclone contains " + hclone.Count + " items, ht should be empty and hclone has our items");
    Debug.Log("Showing our hclone items now");
    for (de in hclone)
    {
        Debug.Log("Key = "+de.Key+ ", Value = " + de.Value);
    }
    Debug.Log("showing our ht items, should be empty");
    for (de in ht)
    {
        Debug.Log("Key = " + de.Key + ", Value = " + de.Value);
    }
}

!

It seems to works :slight_smile:

What would be the type of entry ? Or is it better to leave it typeless ?

It should be System.Collections.DictionaryEntry.