|
Hello experts,
imagine an abstract base class CBase and two derived classes CChildOne and CChildTwo. The children represent some kinds of devices. They inherit all the stuff they have in common from CBase.
What I'm trying is to use CBase as often as possible to prevent lots of case differentiations.
I need the class at hand to tell me which icon to draw for an instance of that class. I would like CBase to have a property DeviceIcon, which is static. That way I wouldn't have to create an instance every time I need the icon. And I also would like to override that property in CChildOne and CChildTwo to always get the correct icon.
Visual Studio tells me that a property cannot use both abstract and static keywords. Anyone an idea how to get that going?
Or will I have to bite the bullet and forget about the static part?
Ciao,
luker
|
|
|
|
|
lukeer wrote: cannot use both abstract and static keywords
On the class or the field?
Either way, that wouldn't make sense. An abstract class may have a static field, but it would be shared by all its children. Are you trying to avoid that?
Do you want each child class to have its own icon to be shared among the instances of that child class?
You could probably use a static Dictionary<Type,Icon> to do that. And there may be other techniques as well.
|
|
|
|
|
PIEBALDconsult wrote: Do you want each child class to have its own icon to be shared among the instances of that child class?
That's exactly what I'm trying.
Ciao,
luker
|
|
|
|
|
something like this:
abstract class BaseClass
{
protected static readonly Icon BaseIcon = new Icon("...");
public abstract Icon DisplayIcon
{
get;
}
}
class ChildClassOne : BaseClass
{
static readonly Icon ChildIcon = new Icon("...");
public override Icon DisplayIcon
{
get { return ChildClassOne.ChildIcon; }
}
}
class ChildClassTwo : BaseClass
{
public override Icon DisplayIcon
{
get { return BaseClass.BaseIcon; }
}
}
|
|
|
|
|
Hi,
I would use a static member holding the icon for the class, and an instance member holding the actual icon for the instance, like so:
public class Base {
private static Icon baseIcon=...;
protected Icon myIcon=baseIcon;
...
}
public class Child : Base {
private static Icon childIcon=...;
public Child() {
myIcon=childIcon;
}
...
}
|
|
|
|
|
Here's a way to do it with generics -- I used strings to save time working it up.
(I think it's a bit kludgey.)
public class Base<T>
{
protected static string icon ;
public string
Icon
{
get
{
return ( icon ) ;
}
}
}
public class Child1 : Base<Child1>
{
static Child1
(
)
{
icon = "Child1" ;
return ;
}
}
public class Child2 : Base<Child2>
{
static Child2
(
)
{
icon = "Child2" ;
return ;
}
}
|
|
|
|
|
What's up with the syntax colouring?
|
|
|
|
|
the PRE tags says lang="TEXT" which disables all syntax coloring.
|
|
|
|
|
Syntax doesn't have colour.
|
|
|
|
|
Thanks a lot to everyone.
I finally added a static icon variable to each of the child classes.
The base class got an abstract Image GetDeviceIcon() method that each child class overrides returning its own child class icon. That way the icon is shared between all instances of a child class and can differ between sibling classes.
The drawback is, I have to create an instance of each class once to display the possible icons. But as I said, it's only once.
Thanks again,
luker
Ciao,
luker
|
|
|
|
|
Hi all,
I am communicating to a COM port where I send a string to the device and it returns a string. A button event triggers a string to be written and a text box is populated with the read value.
What I ultimately am trying to do is to have the text box automatically update with the returned value from the device.
I initially thought a timer event should be employed, but I'm not sure if this would be the most robust way to do this.
Thanks.
|
|
|
|
|
The same TextBox? What if the user is typing in it when the result comes along?
I would wrap the serial port in a class that provides an event for response and update the TextBox when the event fires.
(Being me, I'd write a class that I can use with my CommScript[^].)
If it's one TextBox, I might also disable the TextBox and Button when the Button is clicked, and re-enable them when the event fires.
|
|
|
|
|
I'll be using a label instead. Sorry for the confusion. Presently there is a text box, but I won't be allowing text input in the application. The device will require that a string be written to it as it doesn't transmit continuously.
|
|
|
|
|
Well, at least this gives me an excuse to write a SerialCommunicator...
|
|
|
|
|
I have pasted below the code that I am now using. As I am new to this, I thought a timer could be relatively easy to implement instead of the btnStart_Click event. Also, I know timing could really mess things up, but not sure how to deal with that either...
private void btnStart_Click(object sender, EventArgs e)
{
try
{
if (!sp1.IsOpen)
sp1.Open();
sp1.Write("*X01\r");
}
catch (Exception ex)
{
MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
}
}
private void Form1_Load(object sender, EventArgs e)
{
sp1 = new SerialPort("COM1", 9600, Parity.Odd, 7, StopBits.One);
sp1.Handshake = Handshake.None;
sp1.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
sp1.ReadTimeout = 500;
sp1.WriteTimeout = 500;
sp1.Open();
}
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(100);
string data = sp1.ReadExisting();
this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
}
private void si_DataReceived(string data)
{
char[] charsToTrim = {'X','0','1'};
textBox1.Text = data.TrimStart(charsToTrim);
}
|
|
|
|
|
If it's woking that way, then leave it alone.
But now I'm confused as to what you're writing and how.
|
|
|
|
|
The device I am using (a Newport meter) requires a string *X01\r in order to return the value that is on the display. When the button is clicked, it writes that string to the com port and then returns a series of characters, some of which are the actual value on the display. The problem is the button needs to be clicked. I want to have a start button and a stop button that begins and ends a thread used for automatically reading and writing to the com port, displaying the read value in a form label. Since I'm just learning C#, this is not an easy task. If anyone has any pointers, that would be greatly appreciated.
Thanks,Mike
|
|
|
|
|
I would likely use a System.Timers.Timer; the Elapsed event handler would handle the writing, the code you have above would deal with the incoming data.
|
|
|
|
|
I tried that without success. If I move the button click event to a timer tick event, nothing happens. I have even set the timer tick time to be >>Timer.Sleep (100) to make sure that there are no timing issues, but still nothing happens. However, if it did "work", seems that there would be a better way than a Timer.Sleep method....., but I'd take a successful timer function, if it would work. Thoughts?
|
|
|
|
|
I've never had trouble with timers.
You start the timer?
And set it to AutoReset?
Does it elapse at least once?
|
|
|
|
|
I checked the timer again.....Would help if it were enabled. Sometimes I wonder.
Seems that a stopwatch, as Rajesh suggest, may be the better way to handle this?
|
|
|
|
|
mprice214 wrote: enabled
Yes, but that's what Start does anyway.
I prefer the Timer Elapsed handler for sending and the event handler for receiving.
|
|
|
|
|
why are you going for timer to read the comport buffer,
i used stopwatch for this one , see the below code this may help u
Stopwatch ObjStopWatch = new Stopwatch();
ObjStopWatch.Start();
long StDelay = ObjStopWatch.ElapsedMilliseconds;
try
{
string FromModem = "";
Comport.ClearInputBuffer();
Comport.Write(Command);
Comport.Write(new byte[] { 0x0D, 0x0A });
do
{
if (Comport.InBufferCount>0)
{
Comport.Read(Comport.InBufferCount);
FromModem += Comport.InputStreamString;
if (FromModem.Contains("OK"))
break;
}
} while ((StDelay + 5000) > ObjStopWatch.ElapsedMilliseconds);
Frommodem holds the value of the comport buffer. here my timeout is 5000 milliseconds..
Thanks
Rajesh B --> A Poor Workman Blames His Tools <--
|
|
|
|
|
Hi Rajesh,
The issue I'm now having is that using a timer doesn't seem to be the most deterministic approach. In other words, I have a string that is written to the device at certain intervals. The serial data received method (SerialDataReceivedEventArgs) then Invokes the function that writes the returned string to a text box. However, to make sure the entire string is sitting in the buffer to be read, there is the Thread.Sleep. This seems very ineffecient and I'm trying to verify if that assumption is correct. What I would like to do is to write and read as fast as possible without the thread getting all tied up with timing issues. The device I'm talking to sends a carriage return at the end of the string. It seems that the fastest way to do this would be if I have a loop that writes a command string/waits for the string ending with a cr/writes that read value to whatever (textbox, etc) and writes the command string again.
Since I am new to this, I'm not sure if the stopwatch is the way to go, but I think a timer/thread.sleep combination is asking for crashing problems when I'm trying to do this a fast as possible.
Thoughts?
|
|
|
|
|
Here is what I have:
private void DisplayText(object sender, EventArgs e)
{
textBox1.Text = RxString;
serialPort1.DiscardInBuffer();
while (serialPort1.BytesToRead == 0)
serialPort1.WriteLine("*X01\r");
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = "";
try
{
RxString = serialPort1.ReadTo("\r");
}
catch (Exception Exception)
{
return;
}
this.Invoke(new EventHandler(DisplayText));
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
}
I want to get rid of the timer and have a while loop that writes the ("*X01\r") once the buffer is empty. Then I won't need the timer. However, I can't get a while loop to work.
|
|
|
|