I'm quite new to C# and have been running some conversion from VB.NET to C#.
I understand indexing doesn't work with Arrays in C#, which is causing me a bit of trouble.
For example, the line:
myUserName = Conversions.ToString(myUserNameFields[0]);
Gives an error that one cannot apply indexing with [] to an expression of type array
I was hoping this would be a quick fix but I've been struggling to fix it. I've found similar problems from Googling:
Cannot apply indexing with [] to an expression of type 'System.Array' with C#
...but struggling to directly apply this to my code without completely overhauling it.
Is there a quick fix to something like this?
internal static string getCurrentUserName()
{
string myUserName;
myUserName = MyProject.User.Name;
Array myUserNameFields = myUserName.Split('\\');
if (myUserNameFields.Length == 1)
{
myUserName = Conversions.ToString(myUserNameFields[0]);
}
else
{
myUserName = Conversions.ToString(myUserNameFields[1]);
}
return myUserName;
}
Array, declare it asstring[]or just usevar.String.Splitreturns astring[]array in VB.NET and C#. Don't cast it to anArray