Signup/Sign In

Answers

All questions must be answered. Here are the Answers given by this user in the Forum.

Slicing:
***
x = [0,1,2,3,4]
x = x[1:]
***
Which would actually return a subset of the original but not modify it.
4 years ago
As you're dealing with an array of strings, you can simply use array_filter(), which conveniently handles all this for you:
***
print_r(array_filter($linksArray));
***
Keep in mind that if no callback is supplied, all entries of array equal to FALSE (see converting to boolean) will be removed. So if you need to preserve elements that are i.e. exact string '0', you will need a custom callback:
***
// PHP 7.4 and later
print_r(array_filter($linksArray, fn($value) => !is_null($value) && $value !== ''));

// PHP 5.3 and later
print_r(array_filter($linksArray, function($value) { return !is_null($value) && $value !== ''; }));

// PHP < 5.3
print_r(array_filter($linksArray, create_function('$value', 'return $value !== "";')));
***
4 years ago
If the array is unsorted, there isn't really a better way (aside from using the above-mentioned indexOf, which I think amounts to the same thing). If the array is sorted, you can do a binary search, which works like this:

Pick the middle element of the array.
Is the element you're looking for bigger than the element you picked? If so, you've eliminated the bottom half of the array. If it isn't, you've eliminated the top half.
Pick the middle element of the remaining half of the array, and continue as in step 2, eliminating halves of the remaining array. Eventually you'll either find your element or have no array left to look through.
Binary search runs in time proportional to the logarithm of the length of the array, so it can be much faster than looking at each individual element.
4 years ago
***
my_list = [1,2,3,4,5]
len(my_list)
# 5
***
The same works for tuples:
***
my_tuple = (1,2,3,4,5)
len(my_tuple)
# 5
***
And strings, which are really just arrays of characters:
***
my_string = 'hello world'
len(my_string)
# 11
***
4 years ago
Use an anonymous type.

Eg
***
group x by new { x.Column1, x.Column2 }
***
4 years ago
It's pretty simple using Json.NET:
***
dynamic stuff = JsonConvert.DeserializeObject("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");

string name = stuff.Name;
string address = stuff.Address.City;
***
Also using Newtonsoft.Json.Linq:
***
dynamic stuff = JObject.Parse("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");

string name = stuff.Name;
string address = stuff.Address.City;
***
4 years ago
You can use the following code to get the current application directory.
***
AppDomain.CurrentDomain.BaseDirectory
***
4 years ago
***
string result = System.Text.Encoding.UTF8.GetString(byteArray);
***
4 years ago
***
String.Format("{0:n}", 1234); // Output: 1,234.00
String.Format("{0:n0}", 9876); // No digits after the decimal point. Output: 9,876
***
4 years ago
You'll need to add a reference to System.Configuration in your project's references folder.

You should definitely be using the ConfigurationManager over the obsolete ConfigurationSettings.
4 years ago
Close Visual Studio and delete the .suo file that is next to the .sln file. (It will be re-generated the next time you Save all (or exit Visual Studio)).

I've had this problem when adding new projects to the solution on another machine and then pulling the revisions in, but the .suo file can be corrupted in other cases as well and lead to very strange Visual Studio behaviour, so deleting it is one of the things I always try.

Note that deleting the .suo file will reset the startup project(s) of the solution.
4 years ago
You can use the Func delegate in .net 3.5 as the parameter in your RunTheMethod method. The Func delegate allows you to specify a method that takes a number of parameters of a specific type and returns a single argument of a specific type. Here is an example that should work:
***
public class Class1
{
public int Method1(string input)
{
//... do something
return 0;
}

public int Method2(string input)
{
//... do something different
return 1;
}

public bool RunTheMethod(Func myMethodName)
{
//... do stuff
int i = myMethodName("My String");
//... do more stuff
return true;
}

public bool Test()
{
return RunTheMethod(Method1);
}
}
***
4 years ago