|
I have a class say Categories . This class has a singleton property say Current . Here is how that property code looks like
public static Categories Current
{
get
{
if (_categories == null)
LoadCategories();
return _categories;
}
} LoadCategories() method opens a XML file and creates Categories instance for the data given there. I would like to use a separate XML file when this class is running on test mode. For that I wrote the below code
const string defaultCategoryFile = "MainFileName";
const string defaultTestFile = "TestFileName";
public static bool IsTesting = false;
private static void LoadCategories()
{
FileStream stream = null;
if (IsTesting)
stream = new FileStream(defaultTestFile, FileMode.Open);
else
stream = new FileStream(defaultCategoryFile, FileMode.Open);
.............
} In the test class, I will make the IsTesting flag as true to load the test file. Is this a good approach or any other better methods available for this scenario ?
|
|
|
|
|
N a v a n e e t h wrote: Is this a good approach or any other better methods available for this scenario ?
No. The test is invalid because the code you are testing is not the code you will be running in the live system. The class under test should never know if it is in a testing state or not. It should run the same way regardless of it being in a test or live.
You may want to have some way of determining which file is opened from outside the class. e.g. The name is in some config information. You may want to consider using mock objects to mock out the file access and provide your own (although it is probably still easier to provide a test file).
Also, you may find that you have a specific bug that requires something specific in the file. You need the ability to run the test against many different files.
|
|
|
|
|
Thanks Colin - Mock objects are new to me. I will take a look at that. BTW, are you using NMock[^] for mocking objects ?
|
|
|
|
|
I've been using RhinoMocks - But it is really a matter of personal or company preference. As far as I can see most are all pretty much of a muchness.
|
|
|
|
|
I have Report named TopCPU.rpt which has embedded queries.
I want to generate this Report in PDF format based on the Data available in the database Say "REPORTER" on SQL Server. I wrote a below code but while runing i am getting runtime error
An unhandled exception of type 'CrystalDecisions.CrystalReports.Engine.LogOnException' occurred in CrystalDecisions.ReportAppServer.DataSetConversion.dll
Additional information: Logon failed.
Details: IM002:[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Error in File C:\DOCUME~1\Server1\LOCALS~1\Temp\topcpu {C68B19D5-D303-4666-8008-D8586708315E}.rpt:Unable to connect: incorrect log on parameters.
Here is the code which i wrote.
**********----------------------************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace AgainRepGeneration
{
class Conn
{
private Database crDatabase;
private Tables crTables;
private Table crTable;
private TableLogOnInfo crTableLogOnInfo;
private ConnectionInfo crConnectionInfo = new ConnectionInfo();
private ReportDocument crReportDocument ;
//private string _reportFile;
ExportOptions crExportOptions;
DiskFileDestinationOptions crDiskFileDestinationOptions;
public void ReporterConnection()
{
String exportPath = "C:\\Exported\\";
if (!System.IO.Directory.Exists(exportPath))
{
System.IO.Directory.CreateDirectory(exportPath);
}
crConnectionInfo.ServerName = "SERVER1\\OPS_MR";
crConnectionInfo.DatabaseName = "Reporter";
crConnectionInfo.UserID = "sa";
crConnectionInfo.Password = "hello";
//Get the table information from the report
crDatabase = crReportDocument.Database;
crTables = crDatabase.Tables;
for (int i = 0; i < crTables.Count; i++)
{
crTable = crTables[i];
crTableLogOnInfo = crTable.LogOnInfo;
crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
crTable.ApplyLogOnInfo(crTableLogOnInfo);
}
crDiskFileDestinationOptions = new DiskFileDestinationOptions();
crDiskFileDestinationOptions.DiskFileName = exportPath;
//'Set the exporting information
crExportOptions = crReportDocument.ExportOptions;
crExportOptions.DestinationOptions = crDiskFileDestinationOptions;
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
//Export the report
crReportDocument.Export(); //Getting error here
}
public Conn(string FileName)
{
ReportDocument newDocument = new ReportDocument();
newDocument.Load(FileName);
crReportDocument = newDocument;
}
}
class Program
{
static void Main(string[] args)
{
Conn RepConnection = new Conn( "C:\\DOTNET_Report\\topcpu.rpt");
RepConnection.ReporterConnection();
}
}
}
***********-----------------------************************
Could any one tell me what mistake i am making.....
I am not C# developer........ have only experience of VC++.
Just jumped to this lanaguage 3 days back.....
modified on Friday, May 23, 2008 6:05 AM
|
|
|
|
|
Hi,
Can anyone explain to me why in the below code, the List that I added to the dictionary is being cleared by the line of code after it.
NOTE: This method is running in a seperate thread, and allAdaptersAndPorts is a volatile instance variable.
<br />
DSPortAdapter adapter = null;<br />
List<string> portsList = new List<string>();<br />
String portBuff = null;<br />
<br />
try<br />
{<br />
for (Enumeration adapterEnum = OneWireAccessProvider.enumerateAllAdapters();<br />
adapterEnum.hasMoreElements(); )<br />
{<br />
adapter = (DSPortAdapter)adapterEnum.nextElement();<br />
<br />
for (Enumeration portEnum = adapter.getPortNames();<br />
portEnum.hasMoreElements(); )<br />
{<br />
portBuff = (String)portEnum.nextElement();<br />
portsList.Add(portBuff);<br />
}<br />
<br />
this.allAdaptersAndPorts.Add(adapter.getAdapterName(), portsList);<br />
portsList.Clear();<br />
}<br />
}<br />
<br />
catch (OneWireException ex)<br />
{<br />
DebugHelper.LogError(ex, true);<br />
}<br />
</string></string>
Cheers,
|
|
|
|
|
MarkBrock wrote: Can anyone explain to me why in the below code, the List that I added to the dictionary is being cleared by the line of code after it.
MarkBrock wrote: portsList.Clear();
Because you are calling Clear on this list and Clear method clears the list.
I guess you thought that clearing portsList wouldn't clear the list in the dictionary but you have forgotten that List class is reference type so what's actually stored in portsList is reference to the list so when you clear the reference, it gets cleared in the dictionary too. To avoid this you can add a copy of the list to the dictionary.
Giorgi Dalakishvili
#region signature
my articles
#endregion
|
|
|
|
|
Yup that did the trick thanks for your help.
I hadnt realised it was a reference being stored in the dictionary
Cheers,
|
|
|
|
|
Glad to help you
Giorgi Dalakishvili
#region signature
my articles
#endregion
|
|
|
|
|
Like so:
try
{
for (Enumeration adapterEnum = OneWireAccessProvider.enumerateAllAdapters();
adapterEnum.hasMoreElements(); )
{
DSPortAdapter adapter = (DSPortAdapter)adapterEnum.nextElement();
List<string> portsList = new List<string>();
for (Enumeration portEnum = adapter.getPortNames();
portEnum.hasMoreElements(); )
{
portsList.Add((String)portEnum.nextElement());
}
this.allAdaptersAndPorts.Add(adapter.getAdapterName(), portsList);
}
}
catch (OneWireException ex)
{
DebugHelper.LogError(ex, true);
}
What I've done:
1. Moved variable declaration into the smallest scope (set of curlys) possible. In most cases that means you can initialize them in the declaration as well. Neither has any effect on performance whatsoever, the compiler will see to that, but it makes the code more compact and more readable. Good for maintainability.
2. For each adapter, I create a new list, which I add to the dictionary (the reference in portsList gets dropped at the closing curly, but as it is now held by the dictionary element, all is as it should be). No need to copy the list elements into a new list (which is very expensive - it is an extra loop over all the elements even if you let the framework do the job), and no need to clear it.
Should work, should be faster than copying the list.
By the way, what is the Enumeration thingy? Seems cumbersome... I would expect to see something like this:
try
{
foreach (DSPortAdapter adapter in OneWireAccessProvider.AllAdapters)
{
List<string> portsList = new List<string>();
foreach (DSPort port in adapter.Ports)
{
portsList.Add(port.Name);
}
allAdaptersAndPorts.Add(adapter.Name, portsList);
}
}
catch
{
DebugHelper.LogError(ex, true);
}
Assuming you have control over the classes concerned, of course. The "get" prefixes look like someone hasn't understood properties properly, and some healthy use of generics could save you some casts here and there...
Peter the small turnip
(1) It Has To Work. --RFC 1925[^]
|
|
|
|
|
Hi Peter,
Thanks for the help, ive changed a few things from your recommendations.
PeterTheSwede wrote: By the way, what is the Enumeration thingy? Seems cumbersome... I would expect to see something like this:
Yea I tryed foreach, but the class library i'm using to access the 1-wire stuff is written in java, it wont let me cast to a C# enumerator.
PeterTheSwede wrote: Assuming you have control over the classes concerned, of course. The "get" prefixes look like someone hasn't understood properties properly, and some healthy use of generics could save you some casts here and there...
where abouts did you have in mind? If your talking about in the DSPortAdaapter class I have no control over it (its a reference).
Cheers,
|
|
|
|
|
MarkBrock wrote: ... the class library ... is written in java ...
That explains the explicit Enumeration usage and possibly the strange naming conventions...
MarkBrock wrote: If your talking about in the DSPortAdaapter class I have no control over it
Exactly what I meant and was afraid could be the case. Never mind my second rewrite then...
Peter the small turnip
(1) It Has To Work. --RFC 1925[^]
|
|
|
|
|
Hi all
Is exists a way to bind property to control through (this[string])
Ex:
<br />
<br />
protected override void OnLoad(EventArgs e)<br />
{<br />
base.OnLoad(e);<br />
<br />
DataSrc ds = new DataSrc();<br />
<br />
this.DataBindings.Add("Text", ds, "Prop1");
<br />
}<br />
<br />
...<br />
...<br />
...<br />
<br />
public class DataSrc <br />
{<br />
public object Prop1 { get { return "Prop1"; } }<br />
}<br />
<br />
But i realy need to bind control not throug explicit defined property (in case above Prop1) but through this[string PropName] property
Ex:
<br />
<br />
protected override void OnLoad(EventArgs e)<br />
{<br />
base.OnLoad(e);<br />
<br />
DataSrc ds = new DataSrc();<br />
<br />
this.DataBindings.Add("Text", ds, "Prop1");
<br />
}<br />
<br />
...<br />
...<br />
...<br />
<br />
public class DataSrc <br />
{<br />
public object this[string PropName] <br />
{<br />
get <br />
{ <br />
switch(PropName)<br />
{<br />
...<br />
case "Prop1":<br />
return "Prop1"; <br />
}<br />
} <br />
}<br />
}<br />
<br />
Is exists way to do such way????
THANK
|
|
|
|
|
How would you define the PropName? Isn't the whole point of "Prop1" in
this.DataBindings.Add("Text", ds, "Prop1") is to let you define in string which property that you want to bind to?
|
|
|
|
|
Obviously i was not very clear.
this.DataBindings.Add("Text", ds, "Prop1") - for my purpose "Prop1" - should be not property name, but parameter what should pass to this[string PropName] method. The same way as this act when datat source object is DataRow.
|
|
|
|
|
You were very clear, obviously I was not very clear.
You can't just take an indexer and use it as property because they look roughly the same. If you really really want to do that, you probably should create a property for each of the indexer.
|
|
|
|
|
-you probably should create a property for each of the indexer
Good News Evryone (c) futurama
It's realy hard when you have about 50 fields.
Probably had to implement this throught DataRow class.
|
|
|
|
|
ORM code generator, map the fields in datarows into data objects.
BTW, if you map them to data objects, remember to implement OnPropertyChanged or OnXXXXXXChange, or the updating won't be bi-directional.
modified on Friday, May 23, 2008 7:38 AM
|
|
|
|
|
ORM code generator - what is this???
Actualy for my objective realy convient to use indexer, since all this invokation i suppose
to delegate for internal object what also have same indexer.
public System.Drawing.Color MyStubObject::Indexer[string FieldName]
{
if (ContainedDataRow[FieldName, Original] != ContainedDataRow[FieldName])
{
return System.Drawing.Color.LightGreen;
}
else
{
return System.Drawing.Color.White;
}
}
....
control1.DataBindings.Add("EditValue", dataRow, "CLIENT");
control1.DataBindings.Add("BackColor", dataRow.stubObject, "CLIENT");
control2.DataBindings.Add("EditValue", dataRow, "ACCOUNT");
control2.DataBindings.Add("BackColor", dataRow.stubObject, "ACCOUNT");
This way i have all modifyed fields on form with lighted beckground.
|
|
|
|
|
ORM Wiki[^]
Basically you will be working on an object that is mapped to your data, instead of working on datatable, datarow, datacolumn, etc.
You may want to look at whether you can write your own DataBinding for binding to the columns of datarow, good luck.
|
|
|
|
|
Hi ,
I am using the following code to merge a number of word docs.
Word._Document wordDocument = wordApplication.Documents.Add(
ref missing
, ref missing
, ref missing
, ref missing);
Word.Selection selection = wordApplication.Selection;
foreach (string file in filesToMerge)
{
selection.InsertFile(
file
, ref missing
, ref missing
, ref missing
, ref missing);
if (insertPageBreaks)
{
selection.InsertBreak(ref pageBreak);
}
}
selection.
wordDocument.SaveAs(
ref outputFile
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing);
Testing showed that for certain documents all formatting was preserved yet for some documents it changed the font to Times New Roman size 12.
Any ideas to what would cause this as I am stumped.
Thanks in advance.
Ryan.
|
|
|
|
|
Hi ,
I am facing a problem . I have a windows application, from that
application i want to connect to one running instance of Mozilla
browser. I also want the event notification back from Mozilla browser
to windows application.
Please suggest some ideas .
Thanks & Regards
Mihir
|
|
|
|
|
|
Hello,
I have a .xsl document and I need to retrieve the data row by row (I would like to have the name of the column and the value for each record to use these information later)
Please help me, if you now how it is possible to do
Thanks in advance,
Bye
|
|
|
|
|
There are two ways to read xls files:
1.Excel Object model
2.Ole db adapter for excel
There are many sample on the web and codeproject. If you searched, you would find them.
Giorgi Dalakishvili
#region signature
my articles
#endregion
|
|
|
|