Is there a way to use C++/CLI managed Enums as array subscripts? - c++-cli

i have an enum declared as
enum class AccessLevel : int
{
ReadOnly = 0,
Excluded = 1,
ReadWrite = 2,
};
and an Array declared as
static array<String^>^ _accessMap = gcnew array<String^> { "R", "X", "W" };
I want to do something like this:
AccessLevel^ access = access::ReadOnly;
String^ foo = _accessMap[access];

public enum struct AccessLevel
{
ReadOnly = 0,
Excluded = 1,
ReadWrite = 2,
};
AccessLevel access = access::ReadOnly;
you might need to cast to an int
String^ foo = _accessMap[(int)access];

Related

Update random class attribute in Kotlin

I have a class with some attributes:
class DonutBox {
var glaze: Int = 0
var chocolate: Int = 0
var maple: Int = 0
var etc: Int = 0
}
fun addDonuts() {
val omNom = DonutBox()
}
How can I increment a random attribute of the instantiated class?
For instance, if the randomly selected attribute is chocolate, then effectively:
omNom.chocolate += 1
Because Kotlin's properties are statically declared, and you want to use them dynamically, most of the methods to do that will involve reflection, which can get pretty messy and difficult to understand.
When you want dynamic data, it's probably better to use a map:
val donutBox = mutableMapOf(
"glaze" to 0,
"chocolate" to 0,
"maple" to 0,
"etc" to 0,
)
val randomKey = donutBox.keys.random()
donutBox[randomKey] = donutBox.getValue(randomKey) + 1
println(donutBox)
Output:
{glaze=0, chocolate=0, maple=1, etc=0}
That said, if you really want to use reflection, you can do it like this:
data class DonutBox(
var glaze: Int = 0,
var chocolate: Int = 0,
var maple: Int = 0,
var etc: Int = 0,
)
fun addDonuts() {
val omNom = DonutBox()
val randomProperty = omNom::class.declaredMemberProperties.random() as KMutableProperty1<DonutBox, Int>
val existing = randomProperty.get(omNom)
randomProperty.set(omNom, existing + 1)
println(omNom)
}
fun main() {
addDonuts()
addDonuts()
addDonuts()
}
Output:
DonutBox(glaze=0, chocolate=1, maple=0, etc=0)
DonutBox(glaze=0, chocolate=0, maple=0, etc=1)
DonutBox(glaze=0, chocolate=1, maple=0, etc=0)

C++ CLI Passing an array to class

As the title says I have to create class that can accept arrays as a parameter.
Here is my current version of the header file:
public ref class MyClass {
public:
MyClass() {};
MyClass(array<int, 2> ^(&A1), const int &i2) : A1(A1), I2(i2) {};
String^ Method();
~MyClass() {};
private:
array<int, 2>^ A1 = gcnew array<int, 2>(3, 3) {
{ 1, 1, 1 },
{ 1, 1, 1 },
{ 1, 1, 1 },
};
int I2 = 5;
};
String^ MyClass::Method() // Simple output for debugging
{
String^ OutputText;
int sum=10;
OutputText= "OutputText = " + sum;
return OutputText;
}
As of now I'm getting the following error:
'$S1': global or static variable may not have managed type
'cli::array ^'
If I change my array to static I'll get:
"A1" is not a nonstatic data member or base class of class "MyClass"
Class has to have both constructors. I can accept a solution with vector, but i experience pretty much the same issues with it.
Here's what I meant under moving the initialization of the array to a constructor:
public ref class MyClass {
public:
MyClass() {
A1 = gcnew array<int, 2>(3, 3) {
{ 1, 1, 1 },
{ 1, 1, 1 },
{ 1, 1, 1 },
};
};
MyClass(array<int, 2> ^(&A1), const int &i2) : A1(A1), I2(i2) {};
String^ Method();
~MyClass() {};
private:
array<int, 2>^ A1;
int I2 = 5;
};

Assigning values to ArrayList using mapTo

Previously I was using this code:
private val mItems = ArrayList<Int>()
(1..item_count).mapTo(mItems) { it }
/*
mItems will be: "1, 2, 3, 4, 5, ..., item_count"
*/
Now, I am using a class instead of Int, but the class has Int member with name id.
class ModelClass(var id: Int = 0, var status: String = "smth")
So how can I use this method to fill the ArrayList in similar way?
//?
private val mItems = ArrayList<ModelClass>()
(1..item_count).mapTo(mItems) { mItems[position].id = it } // Something like this
//?
From the mapTo documentation:
Applies the given transform function to each element of the original collection and appends the results to the given destination.
Therefore, you just need to return the elements you want:
(1..item_count).mapTo(mItems) { ModelClass(it) }
If you are OK with any MutableList (which is often ArrayList or similar):
val mItems1 = MutableList(item_count) { i -> i }
val mItems2 = MutableList(item_count) { ModelClass(it) }

How to use GroupFormatter with ObjectListView control

I cannot seem to find anywhere, any examples on how to make use of the GroupFormatter delegate to allow me to add footers to my groups when using the ObjectListView control.
Does anyone have any examples that could demonstrate this? I want to remove the text from the group header and add a footer (different text per footer). As well as changing font, etc.
Any examples would be very helpful.
You can analyze the code for the
public void MakeGroupies<T>(T[] values, string[] descriptions, object[] images, string[] subtitles, string[] tasks)
method of the ObjectListView class. That explicitly sets the GroupKeyGetter, GroupKeyToTitleConverter and GroupFormatter property delegates.
This is C# but your VB adaptation should be straightforward. I am using this small test class as the object type to bind to the list view.
public class TestClass
{
private readonly string _s;
private readonly float _f;
public TestClass( string p1, float p2 )
{
this._s = p1;
this._f = p2;
}
[OLVColumn(DisplayIndex = 1, Name="S", Title="String")]
public string S {get {return this._s;}}
[OLVColumn( DisplayIndex = 2, Name = "F", Title = "Float" )]
public float F {get {return this._f;}}
}
So as not to manually define column traits I am using attributes inside the bound object and a
BrightIdeasSoftware.Generator.GenerateColumns( this.olv, typeof( TestClass ) );
call in the form/user control where I am using the list view. In fact here is the method that completely isolates ObjectListView configuration:
void SetData( TestClass[] objects )
{
// build list columns
Generator.GenerateColumns( this.olv, typeof( TestClass ) );
// use groups and make current column the priimary sort column
this.olv.ShowGroups = true;
this.olv.SortGroupItemsByPrimaryColumn = false;
// loop through columns and set properties
foreach( OLVColumn col in this.olv.Columns )
{
col.Groupable = true;
col.Sortable = true;
if( col.Name == "F" )
{
col.MakeGroupies<float>( new float[] { 10f, 100f, 1000f }, new string[] { "<10", "10-100", "100-1000", ">1000" } );
}
else if( col.Name == "S" )
{
col.UseInitialLetterForGroup = false;
//
col.GroupKeyGetter = ( obj ) =>
{
TestClass tc = (TestClass)obj;
switch( char.ToLower( tc.S[0] ) )
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': return true;
default: return false;
}
};
//
col.GroupKeyToTitleConverter = ( o ) => { bool b = (bool)o; return b ? "vowel" : "consonant"; };
//
col.GroupFormatter = ( /*OLVGroup*/ group, /*GroupingParameters*/ parms ) =>
{
string s = string.Format ("{0} {1}", group.GroupId, group.Id);
//group.BottomDescription = "BottomDescription: " + s;
//group.TopDescription = "TopDescription: " + s;
group.Footer = "Footer: " + s;
};
}
}
//
this.olv.RebuildColumns();
//
this.olv.SetObjects( objects );
}
You will definitely have one different footer per each group.

WlanEnumInterfaces don't return Guid C# Whit P/Invoke

Hy Guy i have a a problem whit c# to call a native function wlanenuminterfaces
the function return ok = (0) but i don't know why... the Guid of interface is all {000000000000000000}
why? someone can help me ? thanks so much i pos the code...
[DllImport("wlanapi.dll")]
internal static extern int WlanEnumInterfaces([In]IntPtr HandleClient, [In,Out]IntPtr Reserved, [In,Out] WLAN_INTERFACE_INFO_LIST ine);`
[StructLayout(LayoutKind.Sequential)]
internal class WLAN_INTERFACE_INFO_LIST
{
internal int dwNumberOfItems;
internal int dwIndex;
internal WLAN_INTERFACE_INFO[] InterfaceInfo;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal class WLAN_INTERFACE_INFO
{
internal Guid interfaceGuid;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
internal string strInterfaceDescription;
internal WLAN_INTERFACE_STATE Istate;
}
internal enum WLAN_INTERFACE_STATE
{
wlan_interface_state_not_ready = 0,
wlan_interface_state_connected = 1,
wlan_interface_state_ad_hoc_network_formed = 2,
wlan_interface_state_disconnecting = 3,
wlan_interface_state_disconnected = 4,
wlan_interface_state_associating = 5,
wlan_interface_state_discovering = 6,
wlan_interface_state_authenticating = 7
}
info = new WLAN_INTERFACE_INFO_LIST();
list = new WLAN_INTERFACE_INFO();
IntPtr info1 = Marshal.AllocHGlobal(Marshal.SizeOf(info));
IntPtr list1 = Marshal.AllocHGlobal(Marshal.SizeOf(list));
int enuminterface = Win32Intereop.WlanEnumInterfaces(handle, IntPtr.Zero, info);
if (enuminterface != 0)
{
MessageBox.Show("Problema Enum Interface");
}
IntPtr num = (IntPtr)info.dwNumberOfItems;
info.dwNumberOfItems = Marshal.ReadInt32(num);
info.InterfaceInfo = new WLAN_INTERFACE_INFO[info.dwNumberOfItems];
for (int i = 0; i < info.dwNumberOfItems; i++)
{
info.InterfaceInfo[i] = list;
}