1. This forum is obsolete and read-only. Feel free to contact us at support.keenswh.com

Weird exception with String.Split method

Discussion in 'Programming (In-game)' started by Shabazza, Mar 1, 2015.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Shabazza

    Shabazza Junior Engineer

    Messages:
    689
    I'm trying to split a string with multiple separators.
    When I use this expression, it works, but I can't use a second parameter then.
    Code:
    string[] tokens = infoList[i].Split('\n', ':', ' ');
    
    When I use this expression, I can use the second parameter, but it throws an "Access Denied Exception" upon closing the editor.
    Code:
    Char[] separators = new Char[] { '\n', ':', ' '};
    string[] tokens = infoList[i].Split(separators, StringSplitOptions.RemoveEmptyEntries);
    
    This also happens if I omit the second parameter.

    The Exception is as follows:
    Code:
    Caught exception during execution of script: Access is denied:
    '<PrivateImplementationDetails>{232B6895-DF88-4406-8741-3146E39A067A}+__StaticArrayInitTypeSize=6'
    
    If I increase the separator count to 6, the message says
    Code:
    StaticArrayInitTypeSize=12
    
    I've no idea what's causing this.
    Can someone help me with this?
     
  2. Igneous01

    Igneous01 Apprentice Engineer

    Messages:
    266
    If you are getting access denied exception, then that means it is an illegal object / class / method / type / statement to use in Keens compiler / program.

    As to why string options are illegal - who knows, I think its an after thought and its most likely a bug. I believe char arrays also are illegal in this context as well.

    Unfortunately if its illegal to use, the only thing I can suggest is to either find another way to use the syntax, or use another method.
     
  3. Phoera

    Phoera Senior Engineer

    Messages:
    1,713
    try create array by hands.
    through new and assigning every value. dont use initializer.
     
    Last edited by a moderator: Mar 1, 2015
  4. indigodarkwolf

    indigodarkwolf Apprentice Engineer

    Messages:
    115
    This sounds strangely familiar. In fact, I'm almost positive it is the same issue.

    The StaticArrayInitTypeSize=6 versus StaticArrayInitTypeSize=12 is because a char in C# is actually a wchar_t, or wide character type. Instead of the traditional ASCII chart, it represents a unicode character point, and each can contain international characters. It is 2 bytes is size, as opposed to a C/C++ style char which is 1 byte in size. An array of 3 chars in C# is thus 6 bytes, and an array of 6 chars in C# is 12 bytes.

    Edit: If you can separately create the array as "char[] separators = new char[3]; char[0] = '\n'; char[1] = ':'; char[2] = ' ';", I would like to know. Currently, I'm using a List<> as a workaround when I need an enumerable list of primitive types, and calling Add() for each value, in order. Regardless of the game's instruction count or the utterly inconsequential scope of my scripts, my inner engineer is extremely unsatisfied with O(n) lookups.
     
    Last edited by a moderator: Mar 2, 2015
  5. Shabazza

    Shabazza Junior Engineer

    Messages:
    689
    String.Split needs a Char[]. No alternative for that. But I will try to initialize the array in a separate step then.

    @Igneous: It's not the string option which is illegal. It's the Char[] or the direct initialization of it. (I'll have to figure this out)
    The "StringSplitOptions" I wanted to use were just the reason for creating a char[] instead of providing the separator chars in the call argument directly (because the chars are comma separated there and collide with the String.Split() method overload that has a second argument (the "StringSplitOptions" in this case)).

    This compiler is surely nit-picky at times... :rolleyes:
     
    Last edited by a moderator: Mar 3, 2015
  6. indigodarkwolf

    indigodarkwolf Apprentice Engineer

    Messages:
    115
    Well, the devs may call it C#, but the more I poke at it, the more the original design shows through. It's too bad that the devs felt it was necessary to go that route, between their fears that someone might gain access to I/O and that the community wouldn't understand runtime complexity.

    I mean, I'm not going to stop poking around with it, because it's just too useful and opens up too many possibilities. But the more I run into problems, the more I wish the devs had just left well enough alone and simply blacklisted the using keyword (aside from their whitelisted auto-includes).
     
  7. Shabazza

    Shabazza Junior Engineer

    Messages:
    689
    OK I figured it out:
    This does not work:
    Code:
    Char[] separators = new Char[] {'a', 'b', 'c'}
    
    This does not work:
    Code:
    Char[] separators;
    separators = new Char[3] {'a', 'b', 'c'}
    
    This DOES work:
    Code:
    Char[] separators = new Char[3];
    separators[0] = '\n';
    separators[1] = ':';
    separators[2] = ' ';
    
    Jeez. How are we supposed to work with this? If it's C# with reduced keyword pool, fine. If it's not: Documentation please!
     
    Last edited by a moderator: Mar 3, 2015
  8. reallyLost

    reallyLost Trainee Engineer

    Messages:
    24
    I use:
    Code:
    "abc".ToCharArray()
     
Thread Status:
This last post in this thread was made more than 31 days old.