|
Dewald wrote: Is there a way that I'm just not aware of?
Not really. What is happening is all just syntactic sugar. It looks like an array but you know that it isn't really an array. Therefore, if you want it to be an array you have to convert it to one.
My suggestion would be to add a method called GetAsByteArray() or similar and the contents of the method would create and return this byte array.
Returning the internal buffer should be avoided as it would be possible for something outside your class to modify the contents and your class wouldn't know about it. That would break the encapsulation aspect of OO.
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
* Reading: Developer Day 5
Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton
My website
|
|
|
|
|
Thanks Colin,
I was starting to think that I should probably use something like a GetAsByteArray() method but then it occured to me that it should be possible to override the implicit cast operator and would you believe!
I now have the following piece of code in my class:
static public implicit operator byte[](MyClass m)
{
return (byte[])m.buffer.Clone();
}
... and Bob's your uncle.
The reason I'm using the Clone() method of Array is so that I don't pass the array reference itself which, as you pointed out, would break the aspect of encapsulation.
|
|
|
|
|
Dewald wrote: it occured to me that it should be possible to override the implicit cast operator
I suppose. I'm not a fan of that sort of thing as many classes could be converted in many ways and I prefer to make it explicit what sort of conversion is going on.
The casting operator makes it look like is something as simple as a casting the class to its base, which is a fast operation. Where as a conversion is potentially quite slow.
Anyway, that's just my opinion and it is your project.
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
* Reading: Developer Day 5
Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton
My website
|
|
|
|
|
Fair point. I'm still undecided as to whether I really like this approach. For what I was trying to do, this works perfectly but I'm not sure if it is really a good programming practice.
I am busy porting code from a C++ project to C#. In the C++ project I have a structure with a number of member variables which I'm sending across a TCP socket to a server. The server then responds and the response is copied into another structure.
So what I used to do was to take a pointer of the structure and cast it to a byte array and when the response comes back to take the byte array again and cast it to a pointer to the structure. This was a very quick way of obtaining a stream of bytes from the structure or populating the structure with a stream of bytes but in itself probably not a safe programming practice to start off with. The issues of endianness, memory overflow etc. etc. had to be handled manually and C# is big step towards taking that responsibility off the programmer and managing all of it automatically.
So this "sollution" of mine is probably a step in the opposite direction of what C# have been designed to achieve.
Thanks for the help though.
|
|
|
|
|
Hi,
I dont think it is possible; you would need something like an Indexable interface,
which then also would have been implemented by an array.
BTW I think your index testing code can be omitted, it seems to serve no purpose.
|
|
|
|
|
Luc Pattyn wrote: BTW I think your index testing code can be omitted, it seems to serve no purpose.
LOL. Of course. That was a nice bit of silly coding. Thanks for pointing it out.
As for it being impossible to pass the class as an array to a function, have a look at my response to Colin's answer. It seems to be possible after all. I'm a little undecided as to whether this is really good programming practice but it works and it solves my current problem perfectly.
|
|
|
|
|
Hi,
I did consider the implicit cast approach, but I rejected the idea because
one either must pass the original array making it publicly writable (without cloning)
or copy everything (with cloning) which I consider unacceptable just to solve an
accessibility problem, for many reasons:
- the overhead involved
- the fact that a new (possibly big) object gets created without the programmer
being aware of it.
So IMO another approach is needed, such as modifying the method that consumes the array,
into consuming an interface (IIndexable), and implementing that interface in the indexable
class, plus adding a class that implements the same interface for regular arrays.
|
|
|
|
|
|
Hi
I am struggling with delegates. I am using Visual Studio 2005 and C# to develop a windows application. I have a UserControl that allows the user to input data. At run-time I instantiate UserControls and add them to the windows form. Each UserControl has a checkbox that allow the user to select a UserControl for deletion.
At the moment when I delete I loop through all the UserControls and if the checkbox is checked then I remove it. There can be many UserControls on my form (I have tested it with 100 UserControls on the form) and to first loop through all the UserControls to determine which ones are checked takes up quite a bit of time.
I thought I can perhaps use delegates and events.
I would like to have a method in my form that add a UserControl to an ArrayList or remove it from the ArrayList depending on whether the checkbox on the UserControl is checked or not.
private ArrayList selectList = null;<br />
<br />
public delegate void CheckSelectionDelegate(UserControl1 u);<br />
<br />
public void CheckSelection(UserControl1 uc1)<br />
{<br />
if (uc1.ChkBox.Checked == true)<br />
{<br />
selectList.Add(uc1);<br />
}<br />
else<br />
{<br />
selectList.Remove(uc1);<br />
}<br />
}
On my UserControl whenever the CheckedChanged event fires on the checkbox I would like the UserControl to call the CheckSelection() method.
This way when I delete the program just has to loop through the selectList ArrayList and process them.
However I cannot find clear examples how to do something like this.
If anybody can help me or point me in the right direction, it will help me a lot.
Thanks.
Kobus
|
|
|
|
|
Hello,
Here is what I would do:
The members and logic for the UserControl would look like this:
public bool RemoveMe
{
get
{
return checkBox1.Checked;
}
}
public event EventHandler CheckedChanged;
private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
{
if (CheckedChanged != null)
{
CheckedChanged(this, e);
}
}
Form:
Forms member:
private ArrayList removableUC = new ArrayList();
Forms constructor code:
public Form1()
{
this.ControlAdded+=new ControlEventHandler(Form1_ControlAdded);
InitializeComponent();
}
Forms ControlAdded Handler code:
private void Form1_ControlAdded(object sender, ControlEventArgs e)
{
UserControl1 uc = e.Control as UserControl1;
if(uc!=null)
{
uc.CheckedChanged+=new EventHandler(uc_CheckedChanged);
}
}
Forms handles CheckedChanged event
private void uc_CheckedChanged(object sender, EventArgs e)
{
UserControl1 uc = sender as UserControl1;
if(uc!=null)
{
if(uc.RemoveMe)
{
removableUC.Add(uc);
}
else
{
if(removableUC.Contains(uc))
{
removableUC.Remove(uc);
}
}
}
}
Forms handles button click of "removeallbutton"
private void buttonRemoveAll_Click(object sender, System.EventArgs e)
{
foreach(object o in removableUC)
{
UserControl1 uc = o as UserControl1;
if(uc != null)
{
uc.CheckedChanged-=new EventHandler(uc_CheckedChanged);
this.Controls.Remove(uc);
uc.Dispose();
uc=null;
}
}
removableUC.Clear();
}
I'm working with .Net1.1, so I couldn't use a generic Collection, which would be much nicer.
Hope it helps!
All the best,
Martin
|
|
|
|
|
Hello Martin
Thanks for the help and code. It works perfectly and has vastly reduced the time it took to delete some of the UserControls, especially when there are many (tested it with 200 UserControls).
I appreciate the time you took to assist me.
Kobus
|
|
|
|
|
Hello,
Glad I could help!
To make your code a little more OOP, you could use a special EventArgs class defined in the UserControl.
This would pass the Checked property of the checkbox over the event.
So there would be no more need for the property in the UserControl. (Saves a little bit of memory, specially if you have that much controls).
The other advantage is that you than only have to cast to Control and not UserControl, which takes a little more time I think.
Add a class:
public class EventArgsUserControl : System.EventArgs
{
public bool RemoveMe = false;
public EventArgsUserControl(bool removeme)
{
RemoveMe = removeme;
}
}
Changes in UserControl:
Remove:
public bool RemoveMe
{
get
{
return checkBox1.Checked;
}
}
Add new delegate:
public event UserControlEventHandler CheckedChanged;
public delegate void UserControlEventHandler(object sender, EventArgsUserControl eauc);
Change To:
private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
{
if (CheckedChanged != null)
{
CheckedChanged(this, new EventArgsUserControl(checkBox1.Checked));
}
}
Changes in Form:
link to your new delegate
private void Form1_ControlAdded(object sender, ControlEventArgs e)
{
UserControl1 uc = e.Control as UserControl1;
if(uc!=null)
{
uc.CheckedChanged+=new UserControlEventHandler(uc_CheckedChanged);
}
}
disconnect it like this
uc.CheckedChanged-=new UserControlEventHandler(uc_CheckedChanged);
New checkedchanged code, without casting to UserControl
private void uc_CheckedChanged(object sender, EventArgsUserControl e)
{
Control c = sender as Control;
if(c!=null)
{
if(e.RemoveMe)
{
removableUC.Add(c);
}
else
{
if(removableUC.Contains(c))
{
removableUC.Remove(c);
}
}
}
}
It might sound not neccessary in this special case, but you should consider it as good programming practice.
Hope it helps!
All the best,
Martin
|
|
|
|
|
I have image... How can i read character from image.........
Please any body tell me..
Thanks & Regards,
S.Sajan
S.Sajan
|
|
|
|
|
Wow...it is not so easy
I found some interesting PDFs on google using the keywords "OCR algorithm".
Russell
|
|
|
|
|
hi all
apologies first if I'm being dim here but....
i'm using XML-RPC to do send a request which is fine but the response I get back is basically a string in the format of a querystring eg ?foo=1&bar=2 is there a way I can *magically* turn this into a querystring to access the values returned to me ?
Normally it would be foo = Request.QueryString["foo"]. Can I access the string as a series of name/value pairs or do I have to make the array myself from the string to access them ?
TIA
t
|
|
|
|
|
Hi!
I want to include a created icon to my setup, so that when somebody installs my application this application has my created icon as Icon.
Does anybody know what properties I have to select
Thanks a lot
Johannes
|
|
|
|
|
Click on the setup project. Press F4. On of those properties will allow you to select an icon.
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
* Reading: Developer Day 5
Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton
My website
|
|
|
|
|
Thanks for fast answer!
Actual, the problem still exists. I know how to select an Icon for desktop and also for the created exe-file. But in the start-menu of windows the icon still does not exist and I don't know why!
Johannes
|
|
|
|
|
In the file system, in the "User's Programs Menu" or "User's Desktop" you create a new shortcut. The properties for the short cut include "Icon"
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
* Reading: Developer Day 5
Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton
My website
|
|
|
|
|
Okay, got the solution!
My icon was to big for being displayed in the start menu
Thanx
|
|
|
|
|
go to project details and there you can find the option to browse an icon!
|
|
|
|
|
Hello everyone
Working at a Intranet solution where we are gonna pull Active Directory(AD) data on the user and ofcause authenticate the user.
I have been experimenting with different approches like
* manually logging in a user(admin loging, as the ordinary user isnt allowed to search the AD). And for that matter the user him/her self for testing.
* WindowsIndenity(btw. does this work on mac, linux etc) which works just fine for the Authentication part - but not the "data part"
Now the problem accours when we dont want the User to manually login(Enter username/password) but prefer to have it done automaticly in the background - kinda like WindowsIndentity.
One solution would be to obtain the users login/initials so we can search for the user in AD after (through a admin login)
So my question: is this even possible ? and if so how? aint asking for a full written code, just a few pointers and so
My worry is ofcause that it aint possible due to Security reasons.
Best Regards
-Klaus M. Jensen
|
|
|
|
|
Hello,
I've creating some classes and collections which use generic like :
class SomeCollection<T> : ACollection where T : class
But I also have nested collections so sometimes the type of an object is like :
SomeCollection<MyCollection<MyObject>> someColl;
Does a "typedef" or alias exists in C# ?
I know aliases are used with namespaces, but I didn't find anything for types.
I'd like to have :
"typedef" SomeCollection<MyCollection<MyObject>> TheCollection;<br />
TheCollection coll = new TheCollection();
Thanks.
-- modified at 4:48 Thursday 14th June, 2007
|
|
|
|
|
Hi, how about:
using System.Collections.Generic;
using ListListInt=System.Collections.Generic.List<System.Collections.Generic.List<int>>;
class myClass {
ListListInt myListOfListsOfInts=new ListListInt();
}
-- modified at 7:18 Thursday 14th June, 2007
Luc Pattyn
[My Articles] [Forum Guidelines]
|
|
|
|
|
Thanks for the reply, but the alias ListListInt is available only in the scope of the file, not through the whole project. And I don't want to define it everywhere I use it.
I searched answers to that problem, and the only way to do it is to create a new class :
public class ListlistInt : System.Collections.Generic.List<System.Collections.Generic.List<int>>
{
}
Maybe I'll do it that way, and create a "header"-like file where I store all those kind of "alias" classes...
|
|
|
|