|
DaveyM69 wrote: Are they?
Of course not.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Vikram A Punathambekar wrote: I thought VB arrays were indexed from 1...
The base is zero. In VB6 that was the default, but you could use OPTION BASE 1 to change it to one.
Then I guess that you have also always declared your arrays with one unused item...
Dim arr(100) As Integer
will create an array with 101 items, not 100 items.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Guffa wrote: In VB6 that was the default, but you could use OPTION BASE 1 to change it to one.
I'm so glad I don't know VB.
Cheers,
Vikram.
"I will put my new found knolage to good use" - Captain See Sharp.
"Every time Lotus Notes starts up, somewhere a puppy, a kitten, a lamb, and a baby seal are killed." - Gary Wheeler.
|
|
|
|
|
Vikram A Punathambekar wrote: I'm so glad I don't know VB.
I can't even spell VB.
|
|
|
|
|
jester dey i coldnt even spel vishil besik, and to dey im a progremer.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Pete O'Hanlon wrote: VB
Wash your keyboard!
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
Vikram A Punathambekar wrote: I'm so glad I don't know VB.
You have no idea.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Hi,
I am trying to get my first generic method work. Here is what I want to do:
Based on diffent type of users, we need to use different DLL. The DLL is COM object wrapper, which expose a function that can be called. The original code looks like the following:
using System.Runtime.InteropServices;
using AsynchWrapper1;
using AsynchWrapper2;
using AsynchWrapper3;
....
namespace MyNameSpace
{
public class Unility
{
public static void CallAsynchWrapper(int clientType, int clientId)
{
if (clientType ==0)
{
clsAsynchWrapper1 asynchWrapper = new clsAsynchWrapper1();
asynchWrapper.XYZ(clientId);
Marshal.ReleaseComObject(asynchWrapper);
asynchWrapper = null;
}
else if (clientType ==1)
{
clsAsynchWrapper2 asynchWrapper = new clsAsynchWrapper2();
asynchWrapper.XYZ(clientId);
Marshal.ReleaseComObject(asynchWrapper);
asynchWrapper = null;
}
else
{
clsAsynchWrapper3 asynchWrapper = new clsAsynchWrapper3();
asynchWrapper.XYZ(clientId);
Marshal.ReleaseComObject(asynchWrapper);
asynchWrapper = null;
}
}
}
Where XYZ is the function that all 3 DLLs expose.
I want to make a generic method by modifying the CallAsynchWrapper function doing the following:
public static void CallAsynchWrapper(int clientType, int clientId)
{
if (clientType ==0)
{
clsAsynchWrapper1 asynchWrapper = new clsAsynchWrapper1();
genericCallAsynchWrapper<clsasynchwrapper1>(asynchWrapper, clientId);
}
else if (clientType ==1)
{
clsAsynchWrapper2 asynchWrapper = new clsAsynchWrapper2();
genericCallAsynchWrapper<clsasynchwrapper2>(asynchWrapper, clientId);
}
else
{
clsAsynchWrapper3 asynchWrapper = new clsAsynchWrapper3();
genericCallAsynchWrapper<clsasynchwrapper3>(asynchWrapper, clientId);
}
}
and add a generic method:
static void genericCallAsynchWrapper<t>(T asynchWrapper, int clientId)
{
asynchWrapper.XYZ(clientId);
Marshal.ReleaseComObject(asynchWrapper);
asynchWrapper = default(T);
}
When I compile it, I got error message” T doesn’t have a definition of XYZ.”
My first question is: Is that possible to make a generic method for what I am trying to do?
If that’s possible, how can I make compiler knows that T does have XYZ?
Does anybody know the answer?
Thanks in advance for any help!
|
|
|
|
|
You need to add a constraint to your generic function to specify that the generic type you pass in will have a XYZ method. The best way to do that is to make all your clsAsynchWrapper1, 2 and 3 implement a common interface, with that interface having the XYZ function.
interface IClsAsynchWrapper
{
void XYZ(int clientId);
}
class clsAsyncWrapper1 : IClsAsynchWrapper
{...}
static void genericCallAsynchWrapper<T>(T asynchWrapper, int clientId) where T: IClsAsyncWrapper
{
asynchWrapper.XYZ(clientId);
}
That said, I think polymorphism (through inheritance) would serve you better here than generics. How about having a base class, say ClsAsynchWrapper and have all the ClsAsyncWrapperX classes derive from it? You could then move the code in your generic method to the base class.
|
|
|
|
|
Thank you so much!
That's exactly what I need. I mean both interface and polymorphism. Thank you for enlightening me! I will try both ways and let you know how it goes.
Thanks again!
|
|
|
|
|
I sort the datatable and hence store it in dataview. I use dataView.Sort.
GridView.DataSource = SortedDataView;
GridView.DataBind();
I get the sorted grid now. When I click edit, the sort goes off.
So I try to store it in a cache
Cache["AdminGrid"] = SortedDataView;
Iam unable to retrieve it into a datatable again.
DataTable dtData = (DataTable)Cache["AdminGrid"]; results in an error saying DataView cannot be converted into DataTable.
Any help is appreciated.
Thanks
|
|
|
|
|
DataViews have a Table property to return the DataTable that is associated with it. So you would get reference to the DataTable like...
<br />
DataTable dtData = ((DataView) Cache["AdminGrid"]).Table;<br />
|
|
|
|
|
This is a wierd one.
If you set a combo box's AutoCompleteMode to anything but None, the first time (but only the first time) the control loses focus its TextChanged event is fired if the text hasn't changed!
Is there a workaround or solution for this?
(I'm using VS2008/.NET 3.5 by the way)
Edit: I've submitted a bug report to M$
Edit 2: It was pretty easy to sort a work around[^] for this which I've attached to the bug report. Apparently this has existed since VS2005/.NET 2.0 at least!
modified on Saturday, February 02, 2008 5:28:06 AM
|
|
|
|
|
Here is my scenario,
Events are occuring as follows
.....START---info--info---info---END.....START---info--info---info---END.....START---info--info---info---END and so on
Files are written out based on, end event occurance or 10 mins time out which ever occurs first
i.e
information gathered from start till end must be written in one file
or
If 10 mins elasped and end event was not recieved then write what info we have gathered and again wiat for another 10 mins expecting an end event....say again our 10 mins elasped and nothing happened we write down wht we haev and wait again...say now we got an end event so in this case we will have 3 files.... as
File 1: START---info--info
File 2: info--info--info
File 3: info---END
What I have done is as soon as start event is received I start a thread which waits for 10 mins and writes out files. This is a continuous loop. The only way to come out of this loop is by interrupting the thread (this happens when we get an end event). When a thread is interrupted it will write what ever it has gathered and end.
Is this a right approach?
any other ideas to do it?
Thanks!
|
|
|
|
|
Hi,
from what you told us I think:
1. yes, your scheme could work
2. but you really need only one thread for all the starts and ends,
just have it sit and wait for a start, do its job until an end is received, then
repeat everything in a big while(true) loop. The 10 minute periods can be obtained
from a timer or, depending on your data source, a read with timeout and DateTime.Now
observations.
If you want the app to maintain a normal GUI, everything else should be handled by
a background thread; if not, you can do it all in the main thread (not recommended).
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
could work?...hmmm the reson why I am using threads is that I have multiple event types....
each event type has a start and end event and the 10 min time out is different..hence I allocated different thread with timeouts.
Thanks
Daphne
|
|
|
|
|
I would like to request some suggestions on a problem I am having with a Windows Service I built.
It installs and runs fine, using a timer every minute to check for files, then running a process.
I have the process logging to a Windows Event Log, also, every time if does find and process a file it writes a record to a remote SQL server with file stats.
The third thing the service does is send out e-mails.
One type is a daily summary with totals and stats
The second is whenever an error is caught, it collects the error data and sends it to a support group (Also writes to the event log)
I also e-mail, write SQL & log at service Start and Stop.
The issue is that the logging works OK at Stop if I do it from the Services panel in Admin Tools. However, if the Box is re-started by doing a Start/Shutdown/Re-start, I get no logging or e-mails to notify that the box is going down. I do get the Start up messages and logging minutes later when the system restarts.
However, if someone were to do a Start/Shutdown/Shutdown, I would not be notified and it might be several hours before other processes noticed that the data stream had stopped and complain about it.
Here is the code from my OnStop section:
protected override void OnStop()<br />
{<br />
closeResources("ALL",9);<br />
<br />
eventLogOSNUCAS.WriteEntry("OSNUCAS Reader is shutting down",EventLogEntryType.Error);<br />
<br />
writeDashboardLogLine("OMAH799", "###########", 0, "OSNUCAS Reader is shutting down", 0, 0, 0, 0, "Stopped", "##########NUCASReader on " + Environment.MachineName + " has shut down");<br />
<br />
AutoEMailSender("########## NUCAS Reader is shutting down", "########### NUCAS Reader service is shutting down\n", 1);<br />
<br />
<br />
}
What do I have to do to ensure that a shutdown event is logged?
(Obviously, I cannot log a power outage, but a UPS generated forced shutdown due to low battery would also NOT be recorded)
Thanks, Don
|
|
|
|
|
Maybe you can try loging in the finalizer of your service (~MyServicename()) - this assumes of course that the finalizers of objects are called due to system shutdown. As you saw the Stop is not called the process is just closed.
|
|
|
|
|
Hello,
I can't figure this one out, so I'll be happy if someone lends me a helping hand here.
How do I instantiate an object with a generic type parameter if I don't know what the type parameter will be at run time?
Example:
namespace GenericTypeInitialization
{
class Program
{
static void Main()
{
Console.WriteLine("Press 1 for FooA, press 2 for FooB:");
ConsoleKeyInfo key = Console.ReadKey(true);
Type TypeOfFoo;
if (key.KeyChar == '1')
{
Console.WriteLine("FooA selected.");
TypeOfFoo = typeof(FooA);
}
else if (key.KeyChar == '2')
{
Console.WriteLine("FooB selected.");
TypeOfFoo = typeof(FooB);
}
Bar<TypeOfFoo> MyInstanceOfBar = new Bar<TypeOfFoo>();
}
}
class FooA {}
class FooB {}
class Bar<TFoo> {}
}
Obviously, the above code doesn't compile. I'm getting an error of
The type or namespace name 'TypeOfFoo' could not be found (are you missing a using directive or an assembly reference?)
How do I get the type parameter at run time then? I've tried various things, but none worked. Also, you can't instantiate the Bar<> class before '////'.
Thanks a lot for any input,
Michal
|
|
|
|
|
You will have to use reflection I guess
|
|
|
|
|
You can't. Generics in .NET are evaluated during runtime creating special types, [^]
The best solution to your problem is to use inheritance and base classes.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
Generics are designed to be used when you know the type at compile-time (early binding). When you don't know the type until run-time (late binding), you have to use reflection.
-----
You seem eager to impose your preference of preventing others from imposing their preferences on others. -- Red Stateler, Master of Circular Reasoning and other fallacies
If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
God is the only being who, to rule, does not need to exist. -- Charles Baudelaire
|
|
|
|
|
Ok, so how would you use reflection to make the above example work as intended?
Thanks,
Michal
|
|
|
|
|
I reexamined your example, and her is what I suggest. Since you only have 2 well-known concrete classes, you may not need reflection for now, but generics is not the way to go either.
Make an interface:
public interface IFoo
{
void MyMethod();
}
Then have some implementation, 2 in you example:
public class FooA : IFoo
{
public void MyMethod() { Console.WriteLine("Hello from FooA"); }
}
public class FooB : IFoo
{
public void MyMethod() { Console.WriteLine("Hello from FooB"); }
}
Then, using your example code:
class Program
{
static void Main()
{
Console.WriteLine("Press 1 for FooA, press 2 for FooB:");
ConsoleKeyInfo key = Console.ReadKey(true);
IFoo myFoo = null;
if (key.KeyChar == '1')
{
myFoo = new FooA();
}
else if (key.KeyChar == '2')
{
myFoo = new FooB();
}
else
{
throw new InvalidOperationException("Wrong selection, you must select 1 or 2");
}
myFoo.MyMethod();
}
}
This example is pretty simplistic, but you get the idea. In this particular case, you didn't need generics nor reflection. In real-life application, this is rarely sufficient. Read on reflection, I am sure there are quite good articles on this site.
Good luck.
-----
You seem eager to impose your preference of preventing others from imposing their preferences on others. -- Red Stateler, Master of Circular Reasoning and other fallacies
If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
God is the only being who, to rule, does not need to exist. -- Charles Baudelaire
|
|
|
|
|
Hello,
thanks for your reply. Unfortunately, this is not what I need.
Interface was my first thought on how to solve this, but I can't use the interface because I have to be able to copy the containing type by value, not by reference. If you declare the object as an interface, you can only copy it as a reference later on (unless you unbox it in which case you - again - need to know its type
I did not include the error handling in my example as I guess we are solving something else here.
Also, as I said, the type should not be instantiated before '////' mark. It's actually being instantiated much later on in the code, but I need to know the type first.
I've been working with reflection and generics before. My above example is a much simplified illustration of what I need to do, but the main issue is still finding out the type and passing it along as a parameter.
Thanks,
Michal
|
|
|
|
|