|
Thanks for your suggestion
|
|
|
|
|
You're welcome - trust me, you only make extra work for yourself by not having an ID column in every table!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
That did not run at all. Nowhere did you state a command.ExecuteNonQuery(). It will crash: that's terribly malformed SQL, so much that I cannot guess what you plan to do.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
Thanks,
after command.ExecuteNonQuery() this line I am getting error called
a local variable name connection is already defined in this scope and I checked my code only once I used the variable connection. kindly help me on this.
For the below code
SqlCommand command = new SqlCommand("truncate table clean"), connection);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
MessageBox.Show("Cleared All data");
I am getting error
a local variable name connection is already defined in this scope
If the sql commanand to
SqlCommand command = new SqlCommand("truncate table clean)", connection);
getting an error
Incorrect syntax near ')'.
|
|
|
|
|
Shankar M wrote: getting an error
Incorrect syntax near ')'. Remove the ) after clean.
This space for rent
|
|
|
|
|
Thanks for your suggestion
|
|
|
|
|
Which unit testing framework for C# can create unit tests automatically?
|
|
|
|
|
What do you mean by creating unit tests automatically? Are you referring to tests like PEX and MOLES or Intellitest?
This space for rent
|
|
|
|
|
I mean that I have a code that contains some methods...
I'm looking for a tool that would go over my code and create unit tests for the methods automatically... Does a tool like that exist in the market?
|
|
|
|
|
To a certain extent, yes it does (see the links in my original post to PEX and Moles from Microsoft research). The real question is, how much value do these tools provide? For instance, it's simple enough to automatically check whether or not a value is null but what about range checks? Would a tool know that your code expected a value between 0 and 5? Don't fall into the trap of just blindly adding tests, which is the problem with this idea. Your tests should really be there to exercise the expected logic of your code; for instance, if you changed the acceptable range from 0 to 6 and passed 6 into the code expecting the test to follow the fail path, it would suddenly follow a different path through your code so you would know that your test needs to change.
This space for rent
|
|
|
|
|
No framework can predict your expected results; so there's no such thing as creating tests automatically.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Is it possible to test several cases of the method under test?
|
|
|
|
|
Yes, you can throw random values at it.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
You could try some Property-Based Testing[^]. Frameworks like FsCheck automaticaly generate random values to verify your properties. But you have to come up with properties that you need to verify yourself.
Such approach is more reliable then unit-testing against magic-numbers and is helpful to spot bugs but can only be used in the limited cases
Also again I'm not 100% sure whether this is what you're looking for
|
|
|
|
|
Not really a question but I would like some feedback.
I have been programming in C# for years now and until today I never could find a way to use the ref keyword in a syntax-correct way. I'm writing code to build XML based on data classes for what must be the thousandth time. The simplest way to generate the data is in plain old strings but I needed a fast way to check for reserved XML characters and update the string only if necessary. Strings are immutable and I don't want to have to copy the string if necessary as this just adds overhead so I came up with this:
public static bool ContainsReservedXmlCharacters(string value)
{
char[] reservedCharacters = new char[] { '<', '>', '&', '%' };
for (int i = 0; i < value.Length; ++i)
{
if (Array.Exists(reservedCharacters, delegate (char c) { return c.Equals(value[i]); }))
return true;
}
return false;
}
public static void ValidateForXml(ref string value)
{
if (ContainsReservedXmlCharacters(value))
{
value = String.Format("<![CDATA[{0}]]>", value);
}
}
This way, I can check the string if contains characters that it shouldn't and only replace the string if necessary. Experience has taught that reserved characters do sneak into XML from time to time but not that often. This should save some parsing time as the string is copied infrequently. I could even write the function to escape the reserved characters if needed.
If there is still a use for ref , it is with handling immutable types such as strings in functions where they may or may not be replaced with a new value.
What does the community think about this?
if (Object.DividedByZero == true) { Universe.Implode(); }
Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016
|
|
|
|
|
Foothill wrote: What does the community think about this?
I think you should be using System.Xml.Linq[^] to build your XML documents.
If you build your own XML generator, you're bound to run into edge cases that you hadn't considered.
For example, if your input contains "Test]]>" , your "valid" string will contain "<![CDATA[Test]]>]]>" , which is not valid XML.
Using XLinq:
new XCData("Test]]>") gives:
<![CDATA[<![CDATA[ which looks odd, but is valid.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
At some point, once my understanding of XML is good enough that is, I can take off the training wheels and move over to Linq XML. I would like to gain a deeper knowledge of XML and how to manipulate it first before I do.
Still, I was so surprised that I found a use for the ref keyword that I had to share.
if (Object.DividedByZero == true) { Universe.Implode(); }
Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016
|
|
|
|
|
The framework will not make a copy of the string when entering a new method; it is not a value-type, so no copy. It will create a new immutable once you manipulate it.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
That's news to me. Do you happen to have a reference to that? I was operating on the assumption that immutable types are passed by copy and not by pointer, hence the need for ref so that it could be passed by pointer and replaced if necessary.
if (Object.DividedByZero == true) { Universe.Implode(); }
Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016
|
|
|
|
|
I'll be googling for a reference after the groceries; I'm out of coffee and that's a priority.
I vaguely remember something about string.Clone that doesn't return a clone, because it is "immutable" and the string will only exist in memory once.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Coffee works miracles. Only "interned" strings are kept in memory in that way, and if your string doesn't come from a literal but from user-input, this doesn't happen automatic.
The string is still a reference-type, and its value 'should' not be duplicated as with a value-type. Passing it with the ref keyword means that you are modifying the data of supplied variable. Since the alternative does return a new immutable string, what you said makes sense
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I agree with the previous commentator that for this specific case System.Xml.Linq is the best tried and true solution.
However, if we take a look at the broader case
Quote: If there is still a use for ref, it is with handling immutable types such as strings in functions where they may or may not be replaced with a new value.
then how about working with mutable type instead of immutable? i.e. StringBuilder[^]
|
|
|
|
|
Since I was building this to process inputs from users into an XML document and the statistically low incidence of invalid characters, StringBuilder was not the best option in this case. The data exists as a bunch of strings already and copying them all to a StringBuilder on the off chance they will be changed seemed wasteful. I wanted to combine the validation and replacement operations into a single step but without the need to create copies of a string in memory unnecessarily. The code itself is run-of-the-mill. I was just surprised to actually find a language-correct appllicaton for ref as I have never been able to put it to use before.
if (Object.DividedByZero == true) { Universe.Implode(); }
Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016
|
|
|
|
|
Perhaps "ref" has more of a place when it comes to "inter-op".
It seems there is frequent use "ref" in C# when talking to MS Word, for example:
Globals.ThisDocument.CheckSpelling(
ref missing, ref ignoreUppercase, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
That's not so much of an issue since C# 4 was released. You can omit all of the ref missing arguments, and use named arguments for the ones you want to pass.
And I believe you may be able to omit the ref on the other arguments as well (for Interop calls only); but don't quote me on that.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|