|
Hello
Is it possible to test a datagridview column to know if it is numeric ?
I see nothing, but it must be something because when entering text in a numeric column I get an exception !
Warning : My need is not to handle the exception but to know wich column are numeric in order to set the alignemeent !
Many thanks for any help !
|
|
|
|
|
If you are usimg a DataTable to fill the GridView, then you can get the column metadata in separate datatable. This table holds the datatype for each column. I am unable to recall the function now, but it was something like GetSchema.
Hope it helps
|
|
|
|
|
Thank you
But I finaly found a better way
It is to use the "ValueType" property
for (int i = 0; i < dgv_ScanStat.ColumnCount; i++)
{
System.Type Tp = dgv_ScanStat.Columns[i].ValueType;
bool isNumeric =
Tp == typeof(byte) ||
Tp == typeof(System.Int16) ||
Tp == typeof(System.Int32) ||
Tp == typeof(System.Int64) ||
Tp == typeof(sbyte) ||
Tp == typeof(System.UInt16) ||
Tp == typeof(System.UInt32) ||
Tp == typeof(System.UInt64) ||
Tp == typeof(float) ||
Tp == typeof(double) ||
Tp == typeof(decimal);
if (isNumeric)
{
dgv_ScanStat.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
}
}
|
|
|
|
|
Hi,
I need to learn how to do unit tests for certain functions. I have googled and come accross some stuff, but all the examples are easy.
I read multiple articles on code project, but it didn't help. Usually, I see examples of unit testings functions that take in 1-2 parameters and perform addition/multiplication/etc.
I need to know how to unit test functions that return nothing, take in no parameters, and so on. For example, how do I write unit tests for a function that simply executes a SQL stored procedure? Or a function that just calls other functions?
For example:
private static void DoStuff()
{
int x = 0, y = 0, z = 0;
Function1();
x = Function2();
if(x > y)
Function3();
}
Or something like:
private static void ExecProc(int vidID)
{
try
{
SqlCommand cmd = new SqlCommand("blah", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@vidID", vidID));
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine("error: " + e.ToString());
}
}
I know how to do unit tests for the simple examples, where functions take in a parameter or two, and give some output in certain cases. However, when there is no output, when there is no input, when they talk to the DB or execute stored procs...what are proper unit tests?
I am using Visual Studio 2005 (C#) and have access to nUnit. Ideally, I'd like to learn how to write unit tests (manually) for the above examples, and then learn to use nUnit.
Any help you can provide is appreciated. Also, any links my googling didn't turn up would be appreciated as well. I'm really lost, and am sick of finding the same example that uses functions performing simple arithmetic.
Thanks!
|
|
|
|
|
You don't test private methods normally. DoStuff() method is hard to test as it has dependencies with other methods. You can use Interaction testing[^] to test DoStuff() method. You should refactor the dependencies to separate classes and just test it calls the expected methods. You have to use mock objects to do this. I suggest you to look at RhinoMocks, a mocking framework.
In the ExecProc() methods you have used ADO.NET classes which is hard to test. So you need to abstract it behind and interface, mock the interface while testing. Check the following,
class ProcedureExecution
{
IDataAccess dataAccess;
public ProcedureExecution(IDataAccess dataAccess)
{
this.dataAccess = dataAccess;
}
public void ExecProc(int vidID)
{
try
{
dataAccess.Execute("blah",vidID);
}
catch (Exception e)
{
}
}
} We have abstracted the SQL execution code behind an interface named IDataAccess and we supply it through constructor. It is called dependency injection. While testing you can easily mock IDataAccess . AFAIK, there is no testing required for the above said method as there is nothing to test. Also using static will make testing tough.
If the shown code is your data layer code, then test your method itself and check the values in the database. It's tedious, but it drives out of bugs.
|
|
|
|
|
Thanks for your help.
I think I'm on the right track now.
I suppose I can do something similar for testing calls to a web service?
I have to read about Interaction testing, and look into RhinoMocks.
|
|
|
|
|
I have been making my own web browser in C# and something that isn't necessarily bad is always having to click my 'GO' button. I have gotten used to being able to just hit the 'ENTER' key and be on my way to the web site. I am fairly new to c# coding so I am thinking I am missing a solution somewhere. Any help would be appreciated.
|
|
|
|
|
Hi guys.
I try to save and get data from clipboard but this exception occur
Object reference not set to an instance of an object
This is my snippet code.
XElement data = (from node in X_Element.Descendants(nodeName)
select node).First();
Clipboard.Clear();
Clipboard.SetData("XElement", data);
XElement newXmlNode = (XElement)Clipboard.GetData("XElement");
MessageBox.Show(newXmlNode.Name.ToString());
I'm sure that there is a XElement data in data
What's wrong with it?
Thanks in advance
|
|
|
|
|
Mohammad Dayyan wrote: Object reference not set to an instance of an object
That generally means there is a missing new ....
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
But I'm sure that there is a data in it
|
|
|
|
|
Mohammad Dayyan wrote: I'm sure that there is a data in it
Are you 100% absolutely sure?
You can't assume that it is always going to put data in there. Common practice in my shop is to declare the object and instantiate it with the new , then try to populate with the data. It does save from a lot of headache.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Are you looking at this in the debugger? Which line throws the exception?
If you can't look at it in the debugger for some reason, wrap each line in a try/catch and figure out which line is throwing the exception.
|
|
|
|
|
Thanks for reply
JoeRip wrote: Which line throws the exception?
this line :
XElement newXmlNode = (XElement)Clipboard.GetData("XElement");
|
|
|
|
|
In the following struct:
public struct myStruct
{
public static int CumulativeCountOfInstances;
public myStruct(int SomeUnusedData)
{
CumulativeCountOfInstances++;
}
}
If concurrent processes are creating myStruct structs, what prevents collisions as each instance tries to increment CumulativeCountOfInstances ?
|
|
|
|
|
Nothing prevents it.
Maybe a good place for an interlocked increment
How are different processes sharing the same variable?
Or did you mean concurrent threads?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I definitely mean concurrent threads. Friday afternoons are very hard on my concentration.
So I should use Interlocked increments from my constructor, AND I should should use Interlocked exchange any time I want to read this value?
Dang, static isn't as static as I thought it was
|
|
|
|
|
If you use Interlocked.Increment() / Interlocked.Decrement()
then the new value will be returned (an increment and a read
are combined in one atomic operation).
Reading (for integer types) is atomic (except for 64-bit integers
on a 32-bit processor) so there's no Interlocked.Read except for
longs.
You could use Interlocked.Exchange() if that's what you need to do,
but it's not appropriate for just reading the value (what value would
you exchange?).
To write/modify the variable beyond the atomic operations provided by the
Interlocked class, you'll need to use some other type critical section,
like lock.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks again. I wondered why there was no Read for Int32.
|
|
|
|
|
if I do this:
Queue SynchedMessageQueue = Queue.Synchronized(new Queue());
then when I access SynchedMessageQueue, do I still need to use LOCK statements?
Or is that now being handled automatically?
|
|
|
|
|
I believe that this answers the question (from MSDN Queue.Synchronized Method[^]):
To guarantee the thread safety of the Queue, all operations must be done through this wrapper only.
Enumerating through a collection is intrinsically not a thread-safe procedure.
Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception.
To guarantee thread safety during enumeration,
you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
Mika
|
|
|
|
|
Thanks. I had read this, but for some reason I cannot now remember, I questioned it. Seems pretty straightforward now!
|
|
|
|
|
You're welcome.
Mika
P.s. Really glad to hear that you thought about this instead if just asking
|
|
|
|
|
I think what I blew off was that SPEFICICALLY, enumeration wasn't thread safe. In my mind I thought "well, if getting things from a synchronized queue isn't thread safe, what's the point?"
My brain didn't translate the part where "Only the enumeration itself needs LOCK, the rest of the Queue methods do not."
|
|
|
|
|
Hi, everyone...
I have a stream and put in a GZipStream, save in SQL 2005, after that I load this stream from sql 2005 and try to deserialize from BinaryFormatter. This will be the GOLD, for now a simple serializable-Compress-Decompress-Deserializable is very fine...
But, I´m getting a error:
"Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization."
Any help?
My Code:
public static void Main()
{
List<string> p3;
List<string> p1 = new List<string>();
p1.Add("Brazil");
p1.Add("Error");
p1.Add("Serializable");
Stream Serial = new MemoryStream();
FileStream fileStream = new FileStream("Teste.bin", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
Processo _Retorno = new Processo();
BinaryFormatter teste = new BinaryFormatter();
teste.Serialize(Serial, p1);
byte[] buffer = new byte[Serial.Length];
Serial.Read(buffer, 0, buffer.Length);
fileStream.Write(buffer, 0, buffer.Length);
fileStream.Close();
byte[] Compremido = Compressor.Compress(buffer);
byte[] Descompremido = Compressor.Decompress(Compremido);
MemoryStream msUnZip = new MemoryStream();
msUnZip.Write(Descompremido, 0, Descompremido.Length);
msUnZip.Seek(0, SeekOrigin.Begin);
BinaryFormatter Serializador = new BinaryFormatter();
p3 = (List<string>)Serializador.Deserialize(msUnZip);:mad:
}
</string></string></string></string>
public static class Compressor
{
public static byte[] Compress(byte[] data)
{
MemoryStream output = new MemoryStream();
GZipStream gzip = new GZipStream(output,
CompressionMode.Compress, true);
gzip.Write(data, 0, data.Length);
gzip.Close();
return output.ToArray();
}
public static byte[] Decompress(byte[] data)
{
MemoryStream input = new MemoryStream();
input.Write(data, 0, data.Length);
input.Position = 0;
GZipStream gzip = new GZipStream(input,
CompressionMode.Decompress, true);
MemoryStream output = new MemoryStream();
byte[] buff = new byte[64];
int read = -1;
read = gzip.Read(buff, 0, buff.Length);
while (read > 0)
{
output.Write(buff, 0, read);
read = gzip.Read(buff, 0, buff.Length);
}
gzip.Close();
return output.ToArray();
}
} Font: http://www.codeproject.com/KB/viewstate/ViewStateCompression.aspx[^]
|
|
|
|
|
JUST for awnser this...
It´s necessary to put
Serial.Seek(0, SeekOrigin.Begin);
Between:
byte[] buffer = new byte[Serial.Length];
Serial.Read(buffer, 0, buffer.Length);
|
|
|
|