How can I use Dim order(4) As Int16 which I have declared in public class of VB in C# format?
where,
order(0)=0;
order(1)=0;
order(2)=0;
order(3)=0;
That are default values, just initialize the array with the right size and you are finished.
Note that you want a length of 4 but
Dim order(4) As Int16
Initializes an array with the length 5. So you want this in VB.NET:
Dim order(3) As Int16
and this in C# (note that it's 4 here for the length 4):
short[] order = new short[4];
which is an alias for:
System.Int16[] order = new System.Int16[4];
It's an array, in c# it looks like this (with initializer):
var i = new short[] {0,0,0,0};
That looks like simply:
short[] order = new short[4];
You could also explicitly specify the defaults, but 0 is implicit anyway and it is cheaper not to do this, but:
short[] order = new short[] {0, 0, 0, 0};
Note that short is an alias for System.Int16. If this is a local variable (as opposed to a field), you can also simplify it further using var:
var order = new short[4];
Related
Call an older third party dll, passing parameters require an object, but the same parameter output an array, how to accomplish this? Thank you?
Array ItemValues = Array.CreateInstance(typeof(int), 1);
Array ItemServerErrors = Array.CreateInstance(typeof(int), 1);
Array ItemQuality = Array.CreateInstance(typeof(int), 1);
Array ItemtimeStamp = Array.CreateInstance(typeof(DateTime), 1);
myGroup.SyncRead(1, 1, ref RandomMoneyHandler, ref ItemValues, out ItemServerErrors, out ItemQuality, out ItemtimeStamp);
But QualityObject and TimeStampObject indeed returns an array from VB example
Dim ItemCount As Short = 1
Dim ItemServerHandles(1) As Integer
Dim ItemValues As Array
Dim ItemServerErrors As Array
Dim ItemQuality As Array
Dim ItemTimeStamp As Array
ItemServerHandles(1) = myItem.ServerHandle
myGroup.SyncRead(OPCAutomation.OPCDataSource.OPCCache, ItemCount, ItemServerHandles, ItemValues, ItemServerErrors, ItemQuality, ItemTimeStamp)
enter image description here
What are the syntax for this function call in C#?
Its clear from image you shared
SyncRead() excepts ItemValues as out parameter , you are passing it as ref
Try changing your code from
myGroup.SyncRead(1, 1, ref RandomMoneyHandler, ref ItemValues, out ItemServerErrors, out ItemQuality, out ItemtimeStamp);
to
myGroup.SyncRead(1, 1, ref RandomMoneyHandler, out ItemValues, out ItemServerErrors, out ItemQuality, out ItemtimeStamp);
I have a byte[] object that I'm using as a data buffer.
I want to "read" it as an array of a either primitive/non-primitive structs without duplicating the byte[] data in memory.
The goal would be something like:
byte[] myBuffer;
//Buffer is populated
int[] asInts = PixieDust_ToInt(myBuffer);
MyStruct[] asMyStructs = PixieDust_ToMyStruct(myBuffer);
Is this possible? If so, how?
Is it possible? Practically, yes!
Since .NET Core 2.1, MemoryMarshal lets us do this for spans. If you are satisfied with a span instead of an array, then yes.
var intSpan = MemoryMarshal.Cast<byte, int>(myByteArray.AsSpan());
The int span will contain byteCount / 4 integers.
As for custom structs... The documentation claims to require a "primitive type" on both sides of the conversion. However, you might try using a ref struct and see that is the actual constraint. I wouldn't be surprised if it worked!
Note that ref structs are still very limiting, but the limitation makes sense for the kind of reinterpret casts that we are talking about.
Edit: Wow, the constraint is much less strict. It requires any struct, rather than a primitive. It does not even have to be a ref struct. There is only a runtime check that will throw if your struct contains a reference type anywhere in its hierarchy. That makes sense. So this should work for your custom structs as well as it does for ints. Enjoy!
You will not be able to do this. To have a MyStruct[] you'll need to actually create such an array of that type and copy the data over. You could, in theory, create your own custom type that acted as a collection, but was actually just a facade over the byte[], copying the bytes out into the struct objects as a given value was accessed, but if you end up actually accessing all of the values, this would end up copying all of the same data eventually, it would just potentially allow you to defer it a bit and may be helpful if you only actually use a small number of the values.
Consider class System.BitConverter
This class has functions to reinterpret the bytes starting at a given index as an Int32, Int64, Double, Boolean, etc. and back from those types into a sequence of bytes.
Example:
int32 x = 0x12345678;
var xBytes = BitConverter.GetBytes(x);
// bytes is a byte array with length 4: 0x78; 0x56; 0x34; 0x12
var backToInt32 = BitConverter.ToInt32(xBytes, 0);
Or if your array contains mixed data:
double d = 3.1415;
int16 n = 42;
Bool b = true;
Uint64 u = 0xFEDCBA9876543210;
// to array of bytes:
var dBytes = BitConverter.GetBytes(d);
var nBytes = BitConverter.GetBytes(n);
var bBytes = BitConverter.GetBytes(b);
var uBytes = BitConterter.GetBytes(u);
Byte[] myBytes = dBytes.Concat(nBytes).Concat(bBytes).Concat(uBytes).ToArray();
// startIndexes in myBytes:
int startIndexD = 0;
int startIndexN = dBytes.Count();
int startIndexB = startIndexN + nBytes.Count();
int startIndexU = startIndexB + bBytes.Count();
// back to original elements
double dRestored = Bitconverter.ToDouble(myBytes, startIndexD);
int16 nRestored = BitConverter.ToInt16(myBytes, startIndexN);
bool bRestored = BitConverter.ToBool(myBytes, startIndexB);
Uint64 uRestored = BitConverter.ToUint64(myBytes, startIndexU);
The closest you will get in order to convert a byte[] to other base-types is
Byte[] b = GetByteArray();
using(BinaryReader r = new BinaryReader(new MemoryStream(b)))
{
r.ReadInt32();
r.ReadDouble();
r.Read...();
}
There is however no simple way to convert a byte[] to any kind of object[]
Can someone please help me understand what the following code means and how it translates to C#?
unsigned int b = 100;
std::vector<bool> a (b, false);
if it was something like this:
vector<bool> a;
I'd probably just go:
List<bool> a;
Is that correct?
But I don't don't know c++, so I don't understand how a uint and a bool are being passed to a vector of type bool?
Maybe it should be something like this?
MyGenericList<int, bool> a = new MyGenericList<int, bool>(b, false);
std::vector<bool> is special, it is implemented as a bit array in C++. In other words, 1 byte stores 8 elements of the vector, the most condense possible way to create an array of bool. That's available in .NET as well, the exact equivalent declaration is:
int b = 100;
System.Collections.BitArray a = new System.Collections.BitArray(b);
If this array only ever contains 100 elements then, meh, don't bother. If it is going to contain a million then, yes, do bother.
As the other answers say, the C++ code creates something like an array or list with 100 elements, all false. The equivalent in C# would be:
var a = new bool[100];
Or if you don't need a fixed size:
var a = new List<bool>(100);
// or, if the initial capacity isn't important
var a = new List<bool>();
Since the default bool value is false, you don't need to explicitly specify it anywhere. If you wanted true (or, e.g. in a list of ints, to default to -1), you'd have to loop through it after you created it:
var a = new bool[100];
for (var i = 0; i < a.Length; i++)
a[i] = true;
std::vector<bool> a (b, false); creates a vector (an array or kind of a C# list) that is 100 bools long and initializes these bools to false.
A note: Do not use std::vector<bool>.
The first argument is the size of vector while second is it's element (see handly C++ reference). So
std::vector<bool> a(100, false);
Create vector of 100 elements, elements of which are false.
As pointed out by Scarlet std::vector<bool> is 'strange' (it does not have full C++ container interface. I'd not go so far to say 'don't use it' but it's important to know that it might behave strange - the ling above is to 'normal' vector).
In C#, I can declare an array variable like this
object[] Parameters;
and initialize it like this:
Parameters = new object[20];
In Visual Basic, declaring and initializing an array is easy:
Dim Parameters(19) As Object
Dim Parameters As Object(19) ' Alternative syntax
How would I initialize an array variable that has already been declared in VB.NET?
Parameters = New Object(19) doesn't work.
For example, how would I translate the following to VB.NET?
int value = 20;
object[] Parameters;
if (value > 10)
{
Parameters = new Object[20];
}
Basically the same Visual Basic code as the others, but I'd use the opportunity to add a bit of style:
Dim value = 20 ' Type inference, like "var" in C#
Dim parameters() As Object ' Could also be "parameters As Object()"
If value > 10 Then
parameters = New Object(19) {} ' That's right, Visual Basic uses the maximum index
End If ' instead of the number of elements.
Local variables (parameters) should start with a lowercase letter. It's an established convention and it helps to get correct syntax highlighting in Stack Overflow (this also applies to the original C# code).
So, why are the braces {} required in Visual Basic? In Visual Basic, both method calls and array access use parenthesis (...). Thus, New X(4) could mean:
Create a new object of type X and pass 4 to the constructor,
Create a 5-element [sic] array of X.
To distinguish the two cases, you use the array initializer syntax in the second case. Usually, the braces contain actual values:
myArray = New Integer() {1, 2, 3}
Dim value As Integer = 20
Dim Parameters() as object
If value > 10 then
Parameters = new Object(value - 1){}
end if
If you are lazy like me you could use an online converter which yields:
Dim value As Integer = 20
Dim Parameters As Object()
If value > 10 Then
Parameters = New [Object](19) {}
End If
And if you are not like me and want to learn VB.NET head over to the documentation of the VB.NET syntax and start reading.
It is written
byte[][] getImagesForFields(java.lang.String[] fieldnames)
Gets an array of images for the given fields.
On the other hand, as long as I use the method in the web application project built on asp.net 2.o using c#;
the provided web method declared above, returns sbyte;
Have a look my code below;
formClearanceService.openSession(imageServiceUser);
formClearanceService.prepareInstance(formId);
byte[][] fieldImagesList = formClearanceService.getImagesForFields(fieldNames);
formClearanceService.closeSession();
thus I get the following error: Cannot implicitly convert type 'sbyte[]' to 'byte[][]'
So now,
1- should I ask the web service provider what is going on?
or
2- any other way that can use the sbyte as I was suppose to use byte[][] like following using:
byte[] ssss = fieldImagesList [0]..
Java has signed bytes, so that part is correct in some ways (although unsigned bytes are more natural) - but it is vexing that it is returning a single array rather than a jagged array. I expect you're going to have to compare some data to see what you have received vs what you expected.
But changing between signed and unsigned can be as simple as:
sbyte[] orig = ...
byte[] arr = Array.ConvertAll(orig, b => (byte)b);
or (faster) simply:
sbyte[] orig = ...
byte[] arr = new byte[orig.Length];
Buffer.BlockCopy(orig, 0, arr, 0, orig.Length);