0

I have the following code in C# below:

short* gArray = stackalloc short[3 * 256];

would like the equivalent in VB.net

I tried to do the conversion, with some different online tools, but it only returns me as a type of Invalid Syntax and / or does not exist for the VB.net syntax structure.

If it is possible to chat between syntaxes, as I do in a simplified way (any other types of code, such as Integers, Booleans, among others)


This is the complete code used in C#

public static unsafe bool SetBrightness(int brightnessValue)
{
    if (brightnessValue > 255)
        brightnessValue = 255;
    if (brightnessValue < 0)
        brightnessValue = 0;
    short* gArray = stackalloc short[3 * 256];
    short* idx = gArray;
    for (int j = 0; j < 3; j++)
    {
        for (int i = 0; i < 256; i++)
        {
            int arrayVal = i * (brightnessValue + 128);
            if (arrayVal > 65535)
                arrayVal = 65535;
            *idx = (short)arrayVal;
            idx++;
        }
    }
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
gleisin-dev
  • 150
  • 1
  • 15
  • 3
    That's `unsafe` code. There is no such thing in VB.NET. – Robert Harvey Mar 09 '21 at 18:40
  • 2
    Does this answer your question? [How can I use unsafe code in VB.Net?](https://stackoverflow.com/questions/5915874/how-can-i-use-unsafe-code-in-vb-net) – gunr2171 Mar 09 '21 at 18:43
  • There is some alternative, at least reading this https://social.msdn.microsoft.com/Forums/en-US/d8451bf7-f9bd-49d5-939e-030d7d1e70f8/what-is-equivalent-of-stackalloc-in-vbnet but if it works for you probably depends on your exact context – Steve Mar 09 '21 at 18:44
  • In general, you shouldn't care about where memory is allocated, either in C# or VB.NET. Unsafe code in C# only exists for very specific use cases. – Robert Harvey Mar 09 '21 at 18:47
  • I updated my question by inserting the context code using it in my application, so that you can better understand what I want ... only if **conversion** is possible or not, use the same function as ```C#``` in ```VB.net``` – gleisin-dev Mar 09 '21 at 19:04
  • It's not. Not directly, anyway. Does this C# code satisfy some *measurable* performance constraint? – Robert Harvey Mar 09 '21 at 19:05
  • This code looks like it could be replaced with shifts and boolean bit logic. If so, you could eliminate the loop. It might actually run faster than the `unsafe` code in that form. It would be helpful if we knew what this code actually does, e.g. is it converting to RGB via some well-known algorithm? – Robert Harvey Mar 09 '21 at 19:08
  • @RobertHarvey No, no, just comparing and multiplying the values to reach a certain percentage (between 0 and 65535) – gleisin-dev Mar 09 '21 at 19:12
  • If there's no performance constraints, you can just rewrite it without the `stackalloc`. But you do need to mind the pointer arithmetic; you don't get that in VB.NET. – Robert Harvey Mar 09 '21 at 19:13
  • Did you get that code from [here](https://stackoverflow.com/a/48657889/102937)? – Robert Harvey Mar 09 '21 at 19:13
  • @RobertHarvey That, with some changes, but yes! – gleisin-dev Mar 09 '21 at 19:16
  • i think you should take a look at this, we don't have that in VB.Net : https://stackoverflow.com/questions/759474/vb-net-pointers – MorenajeRD Mar 09 '21 at 19:17
  • That particular Stack Overflow post has other answers in it that do not require `unsafe` code. – Robert Harvey Mar 09 '21 at 19:22
  • @RobertHarvey just like I did not understand? – gleisin-dev Mar 09 '21 at 19:34

0 Answers0