|
Hi,
the statement a = TheArray[0]; contains three operations:
1. getting an array (TheArray)
2. getting an element (TheArray[0])
3. assigning a value (to a)
the statement TheArray[1]=255; contains three operations:
1. evaluating an expression (255)
2. getting an array (TheArray)
3. setting an element (TheArray[1])
So in both cases it is the array getter that is used, that is why it works.
An array setter property would be used to assign a new array, as in TheArray=new byte[] {1,2,3,4};
If you want a property that works on a specific array element, then:
- the type should be byte, not byte[], as only one element will be operated upon
- the index must be known somehow (by being fixed, or as a parameter); when a parameter is required it no longer is a property, it becomes a method as in byte GetArrayElementAt(int index) or SetArratElementAt(int index, byte val)
There is a way to use an indexer, however that is not a general solution, as it doesn't have an identifier (it uses "this" to refer to the class), see here[^]; so for an int indexer, you can only do it once on a class (however, you can do another indexer with say a string index).
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
modified on Friday, October 16, 2009 11:55 PM
|
|
|
|
|
And this is why the Design Guidelines for Class Library Developers advice against using array type properties. Normally encapsulation will require that you clone the array before "giving it" to anyone, and since this can be expensive (if the array is large) it is important to signify the fact to the user of the class by providing a method and not a property.
byte[] bytes;
public byte[] GetBytes()
{
return (byte[])bytes.Clone();
}
|
|
|
|
|
Hi,
I am developing a Windows service in C#. I have couple of question on that.
1. Is it possible to set a Start up parameter during installation of the service? I know that I can pass a parameter while calling ServiceController.Start() method. But the service start type is Autometic and I want the service to run everytime with the same parameter.
2. Is it possible to uninstall the service from within. Say if I meet certain criteria in the service work flow, I want to uninstall the service. Is it possible?
Thanks.
|
|
|
|
|
Aryan S wrote: 1. Is it possible to set a Start up parameter during installation of the service? I know that I can pass a parameter while calling ServiceController.Start() method. But the service start type is Autometic and I want the service to run everytime with the same parameter.
Store the parameters in the Registry or in a config file somewhere.
Aryan S wrote: 2. Is it possible to uninstall the service from within. Say if I meet certain criteria in the service work flow, I want to uninstall the service. Is it possible?
I've never heard of a service uninstalling itself. Doesn't mean it's not possible though.
|
|
|
|
|
1) Store it somewhere -- I use a database
2) I doubt it; you can't uninstall a running Service
... but maybe you could Start a Process that will stop and uninstall the Service.
|
|
|
|
|
Hi,
My application has a heavy form (meaning with lots of images etc.) and after I minimize it and then maximize it the form flickers for 2-3 seconds. I've tried to set DoubleBuffered property of my form to true and tried all kinds of SetStyle combinations but nothing helped. Is there anyone out there who has any idea how I can solve this problem or workaround it?
Thanks in advance.
|
|
|
|
|
Hi,
please tell us more.
1. what are the Controls on the Form? how many? UserControls? nested UserControls?
2. what is showing the images? are they overlapping?
3. trying some transparency stuff?
4. what code is in the Resize handler? any Controls docked, or anchored on both ends so they stretch?
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Hi,
My form contains 2-3 panels, over 7 pictureboxes, lots of labels and groupboxes, one progressbar and one button. There are no overlapping images but some images have transparent backgrounds. There is no code in resize handler and my form is not resizeable. Form's FormBorderStyle is set to none.
|
|
|
|
|
OK,
so you don't have list stuff (ListBox, ListView, TreeView, DataGridView), that is good.
here are things that help in reducing flicker:
- keep the GUI simple (keep the number of Controls below say 50)
- make sure you have high-performance code in the paint handlers, if any (don't create too many objects,
Pens, Fonts, ...; and if you have those, don't forget to Dispose the ones you created)
- use the Graphics from PaintEventArgs, don't use CreateGraphics
- all the above should result in fast painting
- apply double-buffering, which almost completely hides the painting work
These work against you:
- PictureBoxes (PB is a stupid Control, I prefer to paint images myself, in a Paint handler)
- IIRC: transparancy in general, objects (e.g. Labels) on top of something transparant in particular
this normally helps:
- make the Form double-buffered by inserting this in its constructor:
SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer,true);
SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint,true);
SetStyle(System.Windows.Forms.ControlStyles.UserPaint,true);
(if such code is outside constructor it also needs a Control.UpdateStyles)
- avoid huge images, even when shown in small size (heavy rescale = CPU cycles); maybe calculate the right sized image once and keep it around.
- load the image from disk once (outside all Paint handlers) and keep it as an object.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
modified on Saturday, October 17, 2009 8:16 AM
|
|
|
|
|
Thank you for your advices. They really helped me and the form. It doesn't have to struggle anymore.
|
|
|
|
|
you're welcome.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Luc Pattyn wrote: if you have those, don't forget to Dispose the ones you created
Hi, What if my app has values need to be passed? does this.Dispose() going to erase them?
Sun
|
|
|
|
|
Hi,
IMO this.Dispose() does not make any sense, an object cannot dispose itself, as it would have to be alive to execute the next line of code.
This is what I meant:
public void Paint(object sender, PaintEventArgs e) {
Graphics g=e.Graphics;
Font myFont=new Font(...); <<<<<<<<<<<<<<<<<<<<<<<
g.DrawString(..., myFont, ...);
...
myFont.Dispose(); <<<<<<<<<<<<<<<<<<<<<<<
}
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Luc Pattyn wrote: IMO this.Dispose() does not make any sense,
Hi,
Sorry if I said it wrong, but when I mentioned this.Dispose() , this = the main form. But thank you, Luc, I got your point. Again, I have another question, what if I set a new Font for one control, for example, btn_OK, am I doing it rightly by the code below:
btn_OK.Font = new Font(....);
...
btn_OK.Font.Dispose();
Thanks in advance!;)
Sun
|
|
|
|
|
Leapsword Sun wrote: btn_OK.Font = new Font(....);
...
btn_OK.Font.Dispose();
assuming all that is code in a single method, no it does not make sense: assuming the button is visible on some form, the font is in use, so you shouldn't dispose of it at that time.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Luc Pattyn wrote: assuming the button is visible on some form,
Hi, what if I create this button btn_OK dynamically in my codes then? Thanks for your patient so much!
Sun
|
|
|
|
|
I am trying to fill a collection with the names of the colums from an Excel sheet. The inner foreach fails with
"Unable to cast COM object of type 'System.__ComObject' to class type 'System.Data.DataColumn'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface."
What am I doing wrong here?
List<string> colColumns = new List<string>();
ApplicationClass ExcelApp = new ApplicationClass();
Workbook WorkBook = ExcelApp.Workbooks.Open(Header.SourceLoc, 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
foreach (Worksheet Sheet in WorkBook.Worksheets)
{
if (Sheet.Name.Trim().ToLower() == Header.SourceTable.Trim().ToLower())
{
foreach (DataColumn Col in Sheet.Columns)
{
string s = Col.ToString();
}
break;
}
}
Everything makes sense in someone's mind
|
|
|
|
|
Kevin Marois wrote: DataColumn Col in Sheet.Columns
This thing. WorkSheet.Columns is of type Range and you are trying to make it as datacolumn. I would suggest to have a for loop and then iterate and then populate your data columns.
If you are putting excel values in DataColumn and then in datatable, you should go for OleDB approach. That would give you the datatable right away.
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
|
|
|
|
|
Looking for libraries for threaded serial communication. Anyone have some links, free or relatively inexpensive.
|
|
|
|
|
Hi,
I'm unaware of any serial library, and if one existed, I would probably not use it. I use the SerialPort class directly, and adapt architecture, protocols, and code to the various needs I may have.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Do you find that the .net component is fast enough to perform at high baudrates, and handle other application processing of data from the serialPort?
|
|
|
|
|
Hi,
I used serial ports a lot before .NET existed, using standard Win32 functions.
From my .NET serial experience, I have no reason to claim .NET has added a lot of overhead to those functions, however I do not have proof to the contrary either.
BTW: Under high communication loads, I would not be inclined to use the DataReceived event; I'd rather use a separate thread with an outstanding Read. That way I'm in charge of how much data gets read and when it gets processed, as DataReceived lacks a clear specification.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
I use the SerialPort class a lot in my work, and haven't had any issues. One of my current projects uses 921600 baud (fastest you can set in HyperTerminal) and it's been performing fine.
Dybs
|
|
|
|
|
Hi every one, i'm trying to make a dictionary in C# in my country (Ethiopia) official language Amharic that means a dictionary that converts English to Amharic. I designed all words but it is difficult for me to store all English words that are used to be searched. Please guys help me in what format do i've to store those words?
Thank YOU SO MUCH
|
|
|
|
|
Maybe you can store all the words in a StringCollection and search in the collection for the desired word?
|
|
|
|
|