|
My background is more C++ than C#, so I'll suggest you look beyond me. Google threw up a lot of good stuff on "C# composition vs inheritance", including Composition VS Inheritance[^] right here on CP.
It also seems that you may need to drag in interfaces, depending on your exact needs.
Happy hunting,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
As you have discovered, there is no real concept of private inheritance in C#. You can't hide the public properties and methods of a base class. The only real way that you could achieve this would be to have a member variable in your class, instead of using that type as a base class, and you'd just work with that.
|
|
|
|
|
AmbiguousName wrote: Now there is no private inhertence in C#.
Probably because there is so little point in using it.
I wouldn't be surprised if most uses really arise from a misuse of inheritence when someone thinks that inheritence is nothing more than a code simplification tool.
|
|
|
|
|
In a C# 2008 application, I have a foreach loop that is listed below:
for each (PkgID in PkgIDs)
{
Process eProcess = new Process();
String Process_Arguments = null;
eProcess.StartInfo.UseShellExecute = false;
eProcess.StartInfo.FileName = strConsoleAppLocation;
Process_Arguments = filedirectorypath + " 7654" + PkgID;
eProcess.StartInfo.Arguments = Process_Arguments;
eProcess.Start();
eProcess.WaitForExit(1800);
eProcess = null;
Process_Arguments = null;
}
As you can see from the code I listed above I have the following questions:
1. For each iteration of the for loop I create a new process object called eProcess and then I set
eProcess = null; near the end of the loop.
Thus can you tell me if this is code or not and why? If you have better code that I could use would you tell me what you recommend by pointing me to some code I can use as a reference?
2. You can see that I also have the following line of code,
eProcess.WaitForExit(1800);
I have this line since the process that is being called does not seem to complete its processing before the control is returned to this program.
Thus can you tell me the following:
a. Is the number of milliseconds that I specify a good amount? Can you tell me why or why not?
b. Since the code is returned to this calling program before the entire eProcess finished, can you tell me if this is a good way to wait for the eprocess to finish? I would think there is a better way to what for eProcess to finish. If so, is there a way to tell when the eProcess
is really finished executing?
|
|
|
|
|
foreach (PkgID in PkgIDs)
{
Process eProcess = new Process();
String Process_Arguments = null;
eProcess.StartInfo.UseShellExecute = false;
eProcess.StartInfo.FileName = strConsoleAppLocation;
Process_Arguments = filedirectorypath + " 7654" + PkgID;
eProcess.StartInfo.Arguments = Process_Arguments;
eProcess.Start();
eProcess.WaitForExit();
System.Threading.Thread.Sleep (1800);
eProcess = null;
Process_Arguments = null;
}
|
|
|
|
|
I m developing a CE application to read data from binary files which created by C++ progam to do item validation.
Below is the docing of the C++ program..
// File Name: Ean2an.bin which is created by struct
struct EAN2AN_TYPE
{
__int64 ean:40; // 5 bytes, up to 12 digits
__int64 rec_no:24; // 3 bytes rec no in the c_ItemMaster File, up to 16 million records
};
// After bind data to struct, wil create the binary file
bool CreateBin_EAN2AN_TYPE()
{
if(mn_RecordCount_EAN2AN_TYPE == 0) return false;
FILE *binfile;
qsort(mc_EAN2AN_TYPE, mn_RecordCount_EAN2AN_TYPE, sizeof(struct EAN2AN_TYPE), qsort_EAN2AN_TYPE);
try
{
binfile = fopen(ms_Path_EAN2AN_TYPE, "wb");
fwrite(&mc_EAN2AN_TYPE, sizeof(struct EAN2AN_TYPE), mn_RecordCount_EAN2AN_TYPE, binfile);
}
catch(Exception ^ex)
{
TaskProgramLibrary::Message::ERR("Create EAN2AN_TYPE.bin fail!\r\n " + ex->Message);
}
finally
{
fclose(binfile);
mdw_FileSize_EAN2AN_TYPE = FileSize(ms_Path_EAN2AN_TYPE);
}
return true;
. }
I tried to read the data by using binary read(based on position) and use bitconverter to convert to int64 or using Marshal.PtrToStructure, but the value return is incorrect. Then i tried to read 5 bytes instead of 8 bytes from the file, but the value return stil incorrect.
Below is C# coding wrote
//Struct created in C#
[StructLayout(LayoutKind.Sequential)]
public struct EAN2AN_TYPE
{
[MarshalAs(UnmanagedType.I8)]
public Int64 ean;
[MarshalAs(UnmanagedType.I8)]
public Int64 rec_no;
}
//HOw i read in C#
//1.Read Int64 by Binary
private void ReadByBinary()
{
using (BinaryReader b = new BinaryReader(_fs))
{
while (b.PeekChar() != 0)
{
Int64 x = b.ReadInt64();
Console.WriteLine(x.ToString());
}
}
}
//2.Using Marshal to convert the Intptr to struct's field type
private object ReadByMarshal(Type iType)
{
_oType = iType;// typeof(System.Int64);
byte[] buffer = new byte[Marshal.SizeOf(_oType)];
//byte[] buffer = new byte[5];
object oReturn = null;
try
{
_fs.Read(buffer, 0, buffer.Length);
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
oReturn = Marshal.PtrToStructure(handle.AddrOfPinnedObject(), _oType);
handle.Free();
return oReturn;
}
catch (Exception ex)
{
throw ex;
}
}
//3. Use Binary and use bit converter to convert to Int64
private void ReadByBinaryAndUseBitConverter()
{
using (BinaryReader b = new BinaryReader(_fs))
{
byte[] x = b.ReadBytes(8);
Int64 y = BitConverter.ToInt64(x, 0);
Console.WriteLine(y);
byte[] x2 = b.ReadBytes(8);
Int64 y2 = BitConverter.ToInt64(x2,0);
Console.WriteLine(y2);
}
}
//4. Use Marshal and convert to struct
public EAN2AN_TYPE GetStructValue()
{
byte[] buffer = new byte[Marshal.SizeOf(typeof(EAN2AN_TYPE)];
EAN2AN_TYPE oReturn = new EAN2AN_TYPE();
try
{
//if (EOF) return null;
_fs.Read(buffer, 0, buffer.Length);
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
IntPtr rawDataPtr = handle.AddrOfPinnedObject();
oReturn = (EAN2AN_TYPE)Marshal.PtrToStructure(rawDataPtr, typeof(EAN2AN_TYPE));
handle.Free();
if (_fs.Position >= _fs.Length)
Close();
return oReturn;
}
catch (Exception ex)
{
throw ex;
}
}
Anybody have any idea?
Thanks in advance
|
|
|
|
|
Hello i have a problem calling a PHP webservice. The following error accurs:
There is an error in XML document (2, 515). StackTrace: at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at ......
When i inspected the response from the webservice with Fidler. The error occurs at (2,515):
="1.0"="utf-8"
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tns="http://ws-test.nl/docQueue/service/index.php"
xmlns:types="http://ws-test.nl/docQueue/service/index.php/encodedTypes"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:logIn>
<input href="#id1" />
</tns:logIn>
<tns:logIn id="id1" xsi:type="tns:logIn">
<username xsi:type="xsd:string">test</username>
<password xsi:type="xsd:string">welkom</password>
</tns:logIn>
</soap:Body>
</soap:Envelope>
Is there any way to resolve this error?
Ralph.
|
|
|
|
|
Hi guys!
Maybe anyone can help me with this:
I have a object[] with values e.g.:
object[] obj = new object[]{"a",1};
Furthermore I have a dynamic method which is in reality for example a
Func<string,int,int> function;
Is there a way to use the values of the object[] as parameters for the function?
Or how can I invoke the method with the values from the object[]?
-----------------
Why is this needed?
I have classes which store Func<1..n> within a dynamic property
e.g.
class A
public dynamic Algorithm;
Algorithm = new System.Func<int,int,int>((x,y) => { return x + y; });
class B
public dynamic Algorithm;
Algorithm = new System.Func<double,double,int,double>((x,y) => { return x / y * z; }); .
By iterating through MethodInfo[] I get
Type inputType = parameter.ParameterType;
Type returnType = member.ReturnType; and can therefore create my UI.
Now I can create objects out of this by using the Activator
object inputParameterObject = Activator.CreateInstance(inputType);
In the UI I fill in the values.
inputParameterObject1 = a;
inputParameterObject2 = 1;
The inputParameterObjects are stored in an object[]
Now I need to invoke the Func<1..n> by passing the values from the UI.
Algorithm.Invoke(inputParameterObjectArray);
does not work as it is not equal to
Algorithm.Invoke("a",1);
Or in other words
Algorithm.Invoke(object[]) != Algorithm.Invoke(string,int) || Algorithm.Invoke(object,object)
Help would be appreciated!
So long,
modified 3-Oct-12 13:40pm.
|
|
|
|
|
I haven't tried it, but could you use the .DynamicInvoke method instead? Its signature looks like what you want.
|
|
|
|
|
Thank you! Seems to work (just tried it with a short piece of code - going to check it within my project later).
|
|
|
|
|
I have a C# 2010 console application that calls a web service where I am having a problem. The line of code that is commented out for the strConsoleAppLocation string field works correctly when I run the application from my workstation. However when the application is deployed the line of code that refers to strConsoleAppLocation path is changed, I get an error on the line of code, " Process1.StartInfo.Arguments = Process_Arguments;".
When the application is deployed, I make certain the executable is located in the directory path: \\server1\\DEV\\Ftest\\Esample.exe.
Here is the code I am referring to;
protected void add_to Web_service()
{
String strConsoleAppLocation = "\\server1\\DEV\\Ftest\\Esample.exe";
String strEncryptedValue = "encrypted";
String strWebServiceurl = "https://test/test1/TWebService";
Process Process1 = new Process();
String Process_Arguments = null;
Process1.StartInfo.UseShellExecute = false;
Process1.StartInfo.FileName = strConsoleAppLocation;
Process1_Arguments = strEncryptedValue + " " + strWebServiceurl + " 798 ";
Process1.StartInfo.Arguments = Process_Arguments;
Process1.Start();
}
Due to what I mentioned above, I have the following questions:
1. Can you tell me what to do to resolve this issue for now?
2. When this application goes to production, can you tell me and/or point me to a reference to display the preferred method
of how the Esample.exe executable should be called?
|
|
|
|
|
Have you even bothered to look at the documentation? You've been at this so long now that I'd have thought that just once, you'd have cracked open MSDN to see what it has to say.
You CANNOT start a remote process using the Process class. This is clearly stated.
If you want to start a remote process, you effectively have three choices:
1. Use the Task Scheduler API
2. Use PsExec.
3. Use WMI (Windows Management Instrumentation).
You now have enough information to figure this out for yourself through the judicious use of Google and documentation.
|
|
|
|
|
I think what you ate trying to do is execute an app on a remote system. Even if your code succeeds, the executable will run locally as a local process. This code must be executed on the server to start your web service, not on the client. You can probably put this as a startup item on the server.
|
|
|
|
|
On top of the other comments, you never said what the error message said. It's kind of important when asking questions about your code.
|
|
|
|
|
I get the error message on the following line of code:
" Process1.StartInfo.Arguments = Process_Arguments;".
|
|
|
|
|
But you didn't say WHAT error you got. That's the important part.
|
|
|
|
|
|
I know this is not a clear question, but this is the situation. I have a windows application which parse some file( i use TPL here).
If i minimize this application and keep idle for more than 30 minutes, then i cannot bring the application back. I can see the minimized icon, but this wont come back even if i do ALT-TAB. This is in windows 7.
Any idea?
My small attempt...
|
|
|
|
|
Can you try this ?
- Right-click on the taskbar and say "Show windows
side by side"
Another option : You may put a timer in your application with this Code:
MessageBox.Show(this.Location.ToString() + "\r\n" + this.Size.ToString() + "\r\n" + this.WindowState.ToString());
this.WindowState = FormWindowState.Normal;
timer1.Enabled = false;
and repost the result here ?
|
|
|
|
|
Is the UI thread blocked? Are you doing som long running operation on the UI thread??
Chances are good one of these two things is what's doing it.
|
|
|
|
|
Is there any reason you sig has enough blank lines to score scrollbars?
Just curious.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Hmmm...it never did before. I'll take a look.
|
|
|
|
|
Another victim of metrosexualisation? I've since noticed a couple of others in the same boat.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
It appears that way!
There were a couple of BR tags under my name that I don't remember placing there.
Anyway, all fixed. Thanks!
|
|
|
|
|
I dont think that the GUI thread is blocked, because using TPL I parse all the files. Then show the result in the grid.
I am minimizing the application after all these operations are completed. That mean app is Idle when i minimize.
If i maximize the app immediately or like after 5. 10 minutes, then i am ok. But after 30-40 minutes i am having issue.
I also use DEV Express grid o show the result.
My small attempt...
|
|
|
|