Signup/Sign In

Answers

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

For a sample app.config file like below:
***







***
You read the above application settings using the code shown below:
***
using System.Configuration;
***
You may also need to also add a reference to System.Configuration in your project if there isn't one already. You can then access the values like so:
***
string configvalue1 = ConfigurationManager.AppSettings["countoffiles"];
string configvalue2 = ConfigurationManager.AppSettings["logfilelocation"];
***
4 years ago
To split on a string you need to use the overload that takes an array of strings:
***
string[] lines = theText.Split(
new[] { Environment.NewLine },
StringSplitOptions.None
);
***
4 years ago
1.Close Visual Studio
2.Delete the hidden .vs folder
3.Reopen Visual Studio and rebuild the solution.
4 years ago
From OP's example:
***
public static int Method1(string mystring)
{
return 1;
}

public static int Method2(string mystring)
{
return 2;
}
***
You can try Action Delegate! And then call your method using
***
public bool RunTheMethod(Action myMethodName)
{
myMethodName(); // note: the return value got discarded
return true;
}

RunTheMethod(() => Method1("MyString1"));
***
Or
***
public static object InvokeMethod(Delegate method, params object[] args)
{
return method.DynamicInvoke(args);
}
***
Then simply call method
***
Console.WriteLine(InvokeMethod(new Func(Method1), "MyString1"));

Console.WriteLine(InvokeMethod(new Func(Method2), "MyString2"));
***
4 years ago
Use the following code
***
new System.IO.DirectoryInfo(@"C:\Temp").Delete(true);

//Or

System.IO.Directory.Delete(@"C:\Temp", true);
***
4 years ago
To get the type of T from the member of a generic class or method use
***
Type typeParameterType = typeof(T);
***
4 years ago
Use the following JavaScript money formatter :
***
Number.prototype.formatMoney = function(decPlaces, thouSeparator, decSeparator) {
var n = this,
decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces,
decSeparator = decSeparator == undefined ? "." : decSeparator,
thouSeparator = thouSeparator == undefined ? "," : thouSeparator,
sign = n < 0 ? "-" : "",
i = parseInt(n = Math.abs(+n || 0).toFixed(decPlaces)) + "",
j = (j = i.length) > 3 ? j % 3 : 0;
return sign + (j ? i.substr(0, j) + thouSeparator : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thouSeparator) + (decPlaces ? decSeparator + Math.abs(n - i).toFixed(decPlaces).slice(2) : "");
};
***
4 years ago
The for-in statement by itself is not a "bad practice", however, it can be misused, for example, to iterate over arrays or array-like objects.

The purpose of the for-in statement is to enumerate over object properties. This statement will go up in the prototype chain, also enumerating over inherited properties, a thing that sometimes is not desired.

Also, the order of iteration is not guaranteed by the spec., meaning that if you want to "iterate" an array object, with this statement you cannot be sure that the properties (array indexes) will be visited in the numeric order.

For example, in JScript (IE <= 8), the order of enumeration even on Array objects is defined as the properties were created:
***
var array = [];
array[2] = 'c';
array[1] = 'b';
array[0] = 'a';

for (var p in array) {
//... p will be "2", "1" and "0" on IE
}
***
Also, speaking about inherited properties, if you, for example, extend the Array.prototype object (like some libraries as MooTools do), that properties will be also enumerated:
***
Array.prototype.last = function () { return this[this.length-1]; };

for (var p in []) { // an empty array
// last will be enumerated
}
***
As I said before to iterate over arrays or array-like objects, the best thing is to use a sequential loop, such as a plain-old for/while loop.

When you want to enumerate only the own properties of an object (the ones that aren't inherited), you can use the hasOwnProperty method:
***
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
// prop is not inherited
}
}
***
And some people even recommend calling the method directly from Object.prototype to avoid having problems if somebody adds a property named hasOwnProperty to our object:
***
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
// prop is not inherited
}
}
***
4 years ago
The typeof operator will check if the variable is really undefined.
***
if (typeof variable === 'undefined') {
// variable is undefined
}
***
The typeof operator, unlike the other operators, doesn't throw a ReferenceError exception when used with an undeclared variable.

However, do note that typeof null will return "object". We have to be careful to avoid the mistake of initializing a variable to null. To be safe, this is what we could use instead:
***
if (typeof variable === 'undefined' || variable === null) {
// variable is undefined or null
}
***
4 years ago
***
if (s && typeof s.valueOf() === "string") {
// s is a string
}
***
Works for both string literals let s = 'blah' and for Object Strings let s = new String('blah')
4 years ago
The interface to standard classes becomes extensible. For example, you are using the Array class and you also need to add a custom serializer for all your array objects. Would you spend time coding up a subclass, or use composition or ... The prototype property solves this by letting the users control the exact set of members/methods available to a class.

Think of prototypes as an extra vtable-pointer. When some members are missing from the original class, the prototype is looked up at runtime.
4 years ago
On Debianish platforms, if libfoo is missing, you can frequently install it with something like
***
apt-get install libfoo-dev
***
The -dev version of the package is required for development work, even trivial development work such as compiling source code to link to the library.
4 years ago