I'm converting a bit of code from the OLD m2h tutorial (UnityScript into C#), but one line isn't working for me. Here is the code that I've converted that is giving me problems:
string[] chatEntries;
class ChatEntries
{
public string name = "";
public string text = "";
}
and this use of foreach is declared illegal?
foreach ( ChatEntries entry in chatEntries)
{
}
The error that I get is that I "cannot convert a string to ChatEntries." What am I missing?
EDIT: Here is the UnityScript version from M2H:
private var chatEntries = new ArrayList();
class ChatEntry
{
var name : String= "";
var text : String= "";
}
chatEntries is an array of strings in your code. You need to change it to:
ChatEntries[] chatEntries;
Its still throwing out that same error. This didn't work for me :( The chatEntries is indeed a array of strings, but it won't put the current strings from the class into arrays?
I understand now. I thought it was supposed to be an array of strings. That threw me off. Thanks. Both of you are right, but a little more explanation would have been nice. Thank you, though!
If you want to use a array of chatEntries you need Declare it a array of ChatEntries like so:
class ChatEntries
{
public string name = "";
public string text = "";
}
.
.
.
ChatEntries[] chatEntries = new ChatEntries();
and the foreach:
foreach ( ChatEntries entry in chatEntries)
{
}
Edit
var is implicitly typed variables, typed by the compiler.
From MSDN:
Beginning in Visual C# 3.0, variables
that are declared at method scope can
have an implicit type var. An
implicitly typed local variable is
strongly typed just as if you had
declared the type yourself, but the
compiler determines the type. The
following two declarations of i are
functionally equivalent:functionally equivalent:
var i = 10; // implicitly typed
int i = 10; //explicitly typed
But that makes chatEntries an array of classes. The UnityScript version just uses an array of strings. O.o Is this really the most efficient way?
Its still throwing out that same error. This didn't work for me :( The chatEntries is indeed a array of strings, but it won't put the current strings from the class into arrays?
– SirGiveI understand now. I thought it was supposed to be an array of strings. That threw me off. Thanks. Both of you are right, but a little more explanation would have been nice. Thank you, though!
– SirGive