Signup/Sign In

Answers

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

***
select *
into existing table database..existingtable
from database..othertables....
***
If you have used select * into tablename from other tablenames already, next time, to append, you say select * into existing table tablename from other tablenames
4 years ago
You must not use StreamReader for binary files (like gifs or jpgs). StreamReader is for text data. You will almost certainly lose data if you use it for arbitrary binary data. (If you use Encoding.GetEncoding(28591) you will probably be okay, but what's the point?)

Why do you need to use a StreamReader at all? Why not just keep the binary data as binary data and write it back to disk (or SQL) as binary data?

EDIT: As this seems to be something people want to see... if you do just want to copy one stream to another (e.g. to a file) use something like this:
***
///
/// Copies the contents of input to output. Doesn't close either stream.
///

public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[8 * 1024];
int len;
while ( (len = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, len);
}
}
***
To use it to dump a stream to a file, for example:
***
using (Stream file = File.Create(filename))
{
CopyStream(input, file);
}
***
4 years ago
When a subscriber unsubscribes you are changing the contents of the collection of Subscribers during enumeration.

There are several ways to fix this, one being changing the for loop to use an explicit .ToList():
***
public void NotifySubscribers(DataRecord sr)
{
foreach(Subscriber s in subscribers.Values.ToList())
{
^^^^^^^^^
...
***
4 years ago
How about something like this:
***
public static Object GetPropValue(this Object obj, String name) {
foreach (String part in name.Split('.')) {
if (obj == null) { return null; }

Type type = obj.GetType();
PropertyInfo info = type.GetProperty(part);
if (info == null) { return null; }

obj = info.GetValue(obj, null);
}
return obj;
}

public static T GetPropValue(this Object obj, String name) {
Object retval = GetPropValue(obj, name);
if (retval == null) { return default(T); }

// throws InvalidCastException if types are incompatible
return (T) retval;
}
***
This will allow you to descend into properties using a single string, like this:
***
DateTime now = DateTime.Now;
int min = GetPropValue(now, "TimeOfDay.Minutes");
int hrs = now.GetPropValue("TimeOfDay.Hours");
***
You can either use these methods as static methods or extensions.
4 years ago
In .NET Core and .NET Framework ?4.0 there is a generic parse method:
***
Enum.TryParse("Active", out StatusEnum myStatus);
***
This also includes C#7's new inline out variables, so this does the try-parse, conversion to the explicit enum type and initialises+populates the myStatus variable.

If you have access to C#7 and the latest .NET this is the best way.
4 years ago
uses Extension Methods for Encoding class. The rationale is that someone may need to support different types of encodings (not only UTF8).
Another improvement is failing gracefully with the null results for null entry - it's very useful in real-life scenarios and supports equivalence for X=decode(encode(X)).
Remark: Remember that to use Extension Method you have to (!) import the namespace using keyword (in this case using MyApplication.Helpers.Encoding).

Code:
***
namespace MyApplication.Helpers.Encoding
{
public static class EncodingForBase64
{
public static string EncodeBase64(this System.Text.Encoding encoding, string text)
{
if (text == null)
{
return null;
}

byte[] textAsBytes = encoding.GetBytes(text);
return System.Convert.ToBase64String(textAsBytes);
}

public static string DecodeBase64(this System.Text.Encoding encoding, string encodedText)
{
if (encodedText == null)
{
return null;
}

byte[] textAsBytes = System.Convert.FromBase64String(encodedText);
return encoding.GetString(textAsBytes);
}
}
}
***
4 years ago
C also does a good job at not making anything ambiguous.

Sure the dot could be overloaded to mean both things, but the arrow makes sure that the programmer knows that he's operating on a pointer, just like when the compiler won't let you mix two incompatible types.
4 years ago
Yes, there are software algorithms for calculating sin too. Basically, calculating these kind of stuff with a digital computer is usually done using numerical methods like approximating the Taylor series representing the function.

Numerical methods can approximate functions to an arbitrary amount of accuracy and since the amount of accuracy you have in a floating number is finite, they suit these tasks pretty well.
4 years ago
Actually, register tells the compiler that the variable does not alias with anything else in the program (not even char's).

That can be exploited by modern compilers in a variety of situations, and can help the compiler quite a bit in complex code - in simple code the compilers can figure this out on their own.

Otherwise, it serves no purpose and is not used for register allocation. It does not usually incur performance degradation to specify it, as long as your compiler is modern enough.
4 years ago
Bus errors are rare nowadays on x86 and occur when your processor cannot even attempt the memory access requested, typically:

using a processor instruction with an address that does not satisfy its alignment requirements.
Segmentation faults occur when accessing memory which does not belong to your process, they are very common and are typically the result of:

1.using a pointer to something that was deallocated.
2.using an uninitialized hence bogus pointer.
3.using a null pointer.
4.overflowing a buffer.
4 years ago
This is a more readable version:
***
while( a[ 0xFULL ? '\0' : -1 ] >>= a[ !!0X.1P1 ] )
***
and an even more readable version, replacing the expressions in the [] for the values they resolve to:
***
while( a[0] >>= a[1] )
***
Replacing a[0] and a[1] for their values should make it easy to figure out what the loop is doing, i.e. the equivalent of:
***
int i = 10;
while( i >>= 1)
***
which is simply performing (integer) division by 2 in each iteration, producing the sequence 5, 2, 1.
4 years ago
There are 2 options you can go with HTTP URL Hits : GET / POST

GET Request :-
***
HttpURLConnection.setFollowRedirects(true); // defaults to true

String url = "https://name_of_the_url";
URL request_url = new URL(url);
HttpURLConnection http_conn = (HttpURLConnection)request_url.openConnection();
http_conn.setConnectTimeout(100000);
http_conn.setReadTimeout(100000);
http_conn.setInstanceFollowRedirects(true);
System.out.println(String.valueOf(http_conn.getResponseCode()));
***
POST request :-
***
HttpURLConnection.setFollowRedirects(true); // defaults to true

String url = "https://name_of_the_url"
URL request_url = new URL(url);
HttpURLConnection http_conn = (HttpURLConnection)request_url.openConnection();
http_conn.setConnectTimeout(100000);
http_conn.setReadTimeout(100000);
http_conn.setInstanceFollowRedirects(true);
http_conn.setDoOutput(true);
PrintWriter out = new PrintWriter(http_conn.getOutputStream());
if (urlparameter != null) {
out.println(urlparameter);
}
out.close();
out = null;
System.out.println(String.valueOf(http_conn.getResponseCode()));
***
4 years ago