|
See, I have used LINQ in the presentation layer for manupulating data when a sort, search, filtering is done. This makes sense because I am binding and rebinding to controls (Grids, ListBoxes, etc.) and the same data is being used. The generic list are non-persistant and in the business layer I use things like a static ProductManager class to call the ADO object, or cache, and return Product objects. Now, because my data in the business layer, Results from cache, new queries etc. are specific to the generic lists and query, how is LINQ useful there. I don't want someone to write me a book, I just want some ideas.
Thanks,
Steve
|
|
|
|
|
If you have used LINQ in one piece of code then I still don't see what the problem is in using it in another. If you are asking more of a theoretical or design/architecture question, then I can't answer how you can use in your business layer, I don't know what it is.
only two letters away from being an asset
|
|
|
|
|
i have created DLL in C which i am going to use in my C# code .
DLL file is TestLib.DLL
C# file is Test_Net.cs
-----------------TestLib.dll ------------------------------------
#include <stdio.h>
extern "C"
{
__declspec(dllexport) char* DisplayHelloFromDLL()
{
char* x="bach ke kaha jaoge";
return x;
}
}
-------------------End of file TestLib---------------------------------
-------------------------Test_Net.cs------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
namespace Test_Net
{
public partial class Form1 : Form
{
[DllImport("TestLib.dll")]
public static unsafe extern char* DisplayHelloFromDLL();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
unsafe
{
char* y = DisplayHelloFromDLL();
label2.Text = Convert.ToString(y);
}
}
}
}
-----------------------------EOF Test_Net-----------------------
While Debuging i am getting an error saying that cannot convert from char* to object .can anyone tell me how to solve this problem?please anyone can tell me how to convert char* to string or char* to char it will be very usefull .
Any help appreciated
Thanks in advance.
|
|
|
|
|
Have a look at the System.Runtime.InteropServices.Marshal class, it has several different PtrTostring methods.
Dave
|
|
|
|
|
|
As most of my programming experience is from FORTRAN times, I want to apologize for this perhaps trivial question.
I'm writing a Windows appplication with its own namespace (DD4e). In the main form (MSCMain) I create an public ArrayList (myList). This arraylist is populated by instances from a specific class (MStat).
I want a sub-form for entering new instances of class MStat and appending the new instances to myList.
My problem is that after generating a new instance of MStat in the click-event of the OK button in my sub-form, I cannot reference myList from there to append it to.
Searching the class view yields DD4e.MSCMain.myList as I had expected.
So my question is: what is wrong with my approach or what may I be missing?
As I mentioned at the beginning of my post, object-oriented programming with a cool IDE is quite new for me, so please don't assume too much knowledge on my side.
Regards
Dirk Bock
|
|
|
|
|
Your problem is that myList is a member of the class MSCMain so your sub-form needs a reference to MSCMain in order to access it. There are broadly two approaches, each with variations.
1) If you really only want to have one instance of myList you can make it static. (It would be best if the list were a private member of MSCMain with a public accessor property.) eg:
private static ArrayList myList = blaah blaah;
public static ArrayList MStatList
{
get { return myList; }
}
MSCMain.MStatList.Add(theNewMStat);
2) If you want to keep myList an instance member you need to pass your sub-form a reference either to myList or to the instance of MSCMain (which it can use to access the list). eg, in MSCMain somewhere:
SubForm subForm = new SubForm(myList);
and in SubForm:
private ArrayList list;
public SubForm(ArrayList aList)
{
list = aList;
}
list.Add(theNewMStat);
Phil
The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.
|
|
|
|
|
Both of the methods Phil posted will work fine. However, the 'recommended' (but slightly more complicated until you get used to it) way is to use delegates or delegates/events. If you search the articles on here you'll find plenty of beginner and advanced examples.
I'd suggest using a generic list here instead by the way
List<MStat> myList
Dave
|
|
|
|
|
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.
|
|
|
|