Hello I'm using the Microsoft.VisualBasic.PowerPacks Name space to create shapes on a windows form. I've used an array to store all the objects so that i can generate new shapes and modify the properties of a collection of objects, dependent on given scenarios.
I'm attempting to perform a look-up on the array an find the lowest index that contains an Oval Shape. After trawling the internet for quite some time, I've only found statements that will accept fixed values, since every location within the array contains the same value I can't do that.
I'm looking for something along the lines of the statement below. Where i can find either the first entry that is not null or contains an "Microsoft.VisualBasic.Powerpacks.Ovalshape" the object not the type. Thanks.
// ** Object declaration
Microsoft.VisualBasic.PowerPacks.OvalShape shape = new Microsoft.VisualBasic.PowerPacks.OvalShape();
Microsoft.VisualBasic.PowerPacks.OvalShape[] shapes;
**//
int myIndex = Array.IndexOf(shapes, != null);
Simply use:
Array.FindIndex(shapes, s => s != null)
Related
I have a chunk class and I want it to contain references(represented by a int identifier) to objects that are occupying certain coordinates in the chunk. Since many objects don't have collisions, the same position may be occupied by multiple objects. The chunks have 2 dimensions (x,y). Here is what I was thinking to achieve this:
public class Chunk{
public List<int>[] objPresence;
}
This would make an array of lists( in which size = chunksizeX * chunksizeY) where the index would be the flattened x,y coordinates, and the list would contain all objects on that position. ( each int in the list would be my identifier of a given object )
So to find what objects are currently in a given position I would do:
listOfObjectsOnPosition = objPresence[ flattenIndex(x,y) ];
So far so good, but now if I want to check if one specific object is in a certain position I have to iterate through the list, and I would like to be able to have O(1) access on this.
So here is the alternative using dictionary:
public class Chunk{
public Dictionary<int,byte>[] objPresence;
}
and for access, to find a certain object:
objPresence[ flattenIndex(x,y) ].ContainsKey(objToSearch)
I can also iterate through the dictionary keys if I just want to get all objects in the position.
However in this method since I am using Dictionary, and the int key is the identifier for the object, the byte value on the dictionary is wasting memory since i dont need that value for anything.
Is there a way to implement what I have here without that wasted byte, or is there another, better way to do this altogether?
Thanks!
I have a canvas where I will add some rectangles onto. I want to know how many children of only rectangles I have. A lot like array.length.
I have tried this code but have had no luck
int count = plain.Children.OfType<Rectangle>.Count;
Plain is the name of the Canvas.
But get the error:
'Queryable.OfType(IQuerable)' is a method, which is not valid
in the given context. code CS0119.
You need to add parentheses when calling OfType and Count methods:
int count = plain.Children.OfType<Rectangle>().Count();
Let us assume that I have defined a function named computeValue (double x):
whose input is a double value
that returns a value obtained by performing a certain set of operations using the elements of an array, to be described below.
Also, we have the above mentioned array that
is some class's member
contains only 4 positions.
The 1st position contains a certain value, the 4th contains another value that will be the input to our function
(here comes the tricky bit), the 2nd and 3rd values of the array should be the result of a linear interpolation between positions 1 and 4 of the array. That is, if we modify the position 1 or 4 of the array, then positions 2 and 3 should change their values automatically according to the interpolation method.
My aim is to invoke a root-finding algorithm (such as Newton-Raphson, Secant Method, etc) that will aim to minimize the following expression:
f = CONSTANT - computeValue(array[4])
As you may have already observed, the problem is that every time my root-finding routine modifies the 4th element of my array to obtain a new solution, the positions 2 and 3 of my array should be modified accordingly given that they are the result of a interpolation (as mentioned in point 4 above), thus changing the result of computeValue.
What is a possible way of making the values of the array change dynamically as the root finding algorithm works its way towards the root? Maybe something to do with an array storing lambda expressions defining the interpolation?
It cannot be done with a classic array, but you can implement your own type that solves the problem. This type internally uses an array of length four and offers access by
public int this[int index]
{
// get and set accessors
}
Here, you can write your own getters and setters, so that you can recalculate values as soon as the others were changed.
Instead of confusing yourself with array indices, this seems like an excellent time to create your own object. You can use JF Meier's approach and manually create or augment an array class. But I suggest just creating a new object entirely. You can make your interpolated points get only properties that return the proper interpolation. Your object might look like this:
public class Interpolator
{
public double Constant {get; set;} //same as your array[0]
public double Value {get; set;} //same as your array[3]
public double Interpolation1 { get { return CalculateInterpolation1(); } }
public double Interpolation2 { get { return CalculateInterpolation2(); } }
private double CalculateInterpolation1()
{
//define your interpolation here
}
private double CalculateInterpolation2()
{
//define your interpolation here
}
}
A brief .Net Fiddle demo
My app scan Bluetooth devices and show in a table. But, sometimes it scan the same device two or more times and show the same device many times in the table.
I need to filter it. When the Name or UUID of the device is repeated the table will show just one time.
EDIT:
This What I tried, but isn't work...
CBPeripheral peripheral = this._peripherals [indexPath.Row];
List<string> filter = new List<string>();
filter.Add (peripheral.Identifier.AsString());
string[] array = {};
foreach (var c in filter) {
if (!ReferenceEquals (c, array)) {
int x = array.Length;
filter.CopyTo (0, array, 0, x);
}
}
foreach (string i in array) {
Console.WriteLine ("ARRAY: "+i.ToString());
}
Assuming your data is List<string>, you either need to
a. check for duplicates when you add new item to your list
if (!data.Contains(bt_id)) {
data.Add(bt_id);
}
b. remove duplicates after the fact
// requires System.Linq
var display_data = data.Distinct();
While browsing the devices in the area, store every device in an array.
Then you can either
clean the array of doubles and use that to populate the tableview
Add every object from the array to the tableview, and check if what you're adding to the tableview doesn't already exist in the array.
Or if you can, simply store their UDID or any unique code that you can use in a dictionary, and use the same technique as above before filling your tableview with the devices.
Ask me if i'm unclear or if you need more help
EDIT 1 :
You can find a way to clean an array of doubles on this post :
The best way to remove duplicate values from NSMutableArray in Objective-C?
You can also use sort descriptors to sort the array and then compare every element to the next one and delete if necessary. you can find information about sorting arrays easily on the internet, as well as cleaning arrays.
If you can store you values in an NSSet (There is a Mutable Variant as well) sets are unique in that they don't allow duplicates. Be aware though that an NSSet is unordered so there's also a variant for that NSOrderedSet. Have a play around with sets i think they'll fit your purpose quite nicely and they can be always converted back to an array once you've got rid of the dupes.
I'm trying to embed a very basic AI in a game of Tic Tac Toe and I'm running into a problem with the arrays that I use to discern the most suitable square to claim.
I have one array that represents the game grid in its current state and another that represents the way it would look if a certain square were claimed.
The AI kept making weird decisions, so I decided to have it report various information at certain milestones.
string[,] currentGrid;
string[,] hypotheticalGrid;
MessageBox.Show(currentGrid);
hypotheticalGrid = currentGrid;
hypotheticalGrid[x, y] = "O";
MessageBox.Show(currentGrid);
Bear in mind that the above is a very simplified version of the actual code.
The arrays are assigned values along the line and the message boxes are set to display currentGrid as a sequence of its contained values.
Everything works properly except the code between the message boxes.
The problem is that the two message boxes display different results even though the latter is a copy of the former and no changes to currentGrid are specified in the space between the two.
The two lines in between are copied directly from the source code and should only affect the hypothetical grid. As should be obvious, these lines are supposed to reset the hypothetical grid and then add an "O" to the square in question.
However, the "O" also gets placed in the current grid for some reason.
What am I doing wrong?
The line hypotheticalGrid = currentGrid; does not make a copy of currentGrid, instead both variables point to the same array. So any change made in hypotheticalGrid will be seen in currentGrid too.
You should have hypotheticalGrid be a copy of currentGrid:
hypotheticalGrid = (string[,])currentGrid.Clone();
In this case, Clone() will work, because strings are immutable in .NET. In other cases, Clone might not be good enough, just something to be aware of.
The problem is that your assignment:
hypotheticalGrid = currentGrid;
is merely a reference assignment, meaning that now they both point to the same array in memory, so any changes to the hypothetical grid will affect the current grid as well.
You'd need a full item copy in order for them to not interfere with each other.
Keep in mind, that arrays are reference types. Your assignment copies the reference but does not create a duplicate of the array. After the assignment both array variables point to the same array!
You can create an array duplicate like this:
string[] hypotheticalGrid = new string[currentGrid.Length];
Array.Copy(currentGrid, hypotheticalGrid, currentGrid.Length);
Note that this does not create a duplicate of the items contained in the array, but this is not a problem, since strings are immutable. I.e. if you manipulate a string in one of the arrays, this creates a new string and therefore has no effect on the second array.
EDIT:
By looking at the other posts I see that it can be done easier with the Clone method:
string[] hypotheticalGrid = currentGrid.Clone();