|
If the size is very large and performance is very important, I suggest you create a new data structure:
class ExArray{
byte[] data;
int start;
int end;
public this[int i]
{
get { return data[(start + i) % data.Length]; }
set { data[(start + i) % data.Length = value; }
}
public Length { get { return end - start >= 0 ? end - start : end - start + data.Length; }}
public void Shift(...){...}
public void Slice(...){...}
}
April
Comm100 - Leading Live Chat Software Provider
modified 27-May-14 8:58am.
|
|
|
|
|
I need to add the ability to initiate a Skype call in a C# WinForms app. I have Googled this alot and I see lot's of examples to other things, but no example to call someone.
Anyone know how to do this?
Everything makes sense in someone's mind
|
|
|
|
|
|
Hi Allow,
Can anybody let me know how the first constructor works in below code. Whats the use in writing it.
public class c
{
public c(string a) : this()
{;};
public c() {;}
}
Thanks & Regards,
Md. Abdul Aleem
NIIT technologies
|
|
|
|
|
indian143 wrote: Whats the use in writing it.
From what you have posted none whatsoever, there is also a syntax error on the line
{;};
so it does not even compile.
|
|
|
|
|
Ok. If I removed ; from the {;}; as below whats the o/p and why?
public class c
{
public c(string a) : this()
{;}
public c() {;}
}
Thanks & Regards,
Md. Abdul Aleem
NIIT technologies
|
|
|
|
|
I;m not sure where you got that code from but it's called Contructor Chaining.
When overloading constructors, code can be greatly simplified by one constructor calling another that has the code in it for all or many constructors.
To this end, it is more normal for the constructor(s) with more parameters to contain the code and to be called by the 'lesser' constructor(s)... i.e.
public class Class
{
private string name;
public Class(string name)
{
this.name = name;
}
public Class()
: this(string.Empty)
{ }
public string Name
{
get { return name; }
}
}
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Oh. Great thanks Dave. Thanks a Lot.
Thanks & Regards,
Md. Abdul Aleem
NIIT technologies
|
|
|
|
|
Hey,
I'm working on a C# project where I'm using a boolean flag to determine if I need to recompute a value, but when I get to evaluating the boolean flag (in an if statement), if it's false, it will just flip the value of the flag to true, and enter the if block anyway. For clarification:
if (currAreaValid)
{
return area;
}
that's it. That's all the code. currAreaValid is false when it reaches the if statement, then just turns to true, and my method returns area. Now, it gets a little nuttier. If I hardcode "false" in the if statement, instead of currAreaValid, the value of currAreaValid still flips after the if block. I don't know why. I've tried cleaning and rebuilding the solution, but that didn't help. Has anyone seen a problem like this before? The only place I set the value of currAreaValid to true is in the same method that this if block is in, but at the very end of the method, which is 2 lines after the if block.
I have no idea what's going on. Can anyone help?
|
|
|
|
|
Hi,
some of the code you did not show must be relevant to this.
are you using static anywhere?
are you using timers or Threads or asynchronous handlers that call the method that contains your snippet?
|
|
|
|
|
Yeah, I know that some of the other code would be relevant, but I didn't know what to post and didn't want to post the whole project, obviously, lol.
Anyway, I am using no timers or threads (I don't think?). I'm trying out delegates for the first time in C# since I thought a functional approach would be good for some aspects of my data structure (Map and Fold functions)...for that I use static functions as delegates...but these don't call the method (which is a property, but I don't think that should matter). The method/property (called PolygonArea) makes a call to a Fold method which takes in a static delegate (area2Helper), and that static delegate does make a call to a static method in a static class (static method Area in static class Globals), but the property is only being called by my main method (which is static, but it's just my main method).
Do you think that it's something with one of my uses of static that shouldn't be static? If you need more information, let me know, and if you want me to post any code, let me know. Thanks!
~Andy
|
|
|
|
|
Aha.
1.
My debugging rule #1 is: when something goes wrong, it either is your fault (very likely for a novice, quite likely for an experienced programmer) or it is not (rather unlikely, as most bugs get discovered and reported by the thousands of other users and some/most of them get fixed before you would encounter them).
Now if it's your fault, then it could well be one or several of your assumptions is wrong. So don't assume a thing, check all of it. Step through your code and watch; or add logging statements and watch.
2.
having more than one thread (or a timer, or an asynchronous handler) may result in the same method being executed more than once at the same time; when that method operates on the same object, or on static data, it would confuse the person debugging it.
3.
Yes, in order to pinpoint anything, I would need to see more code. Maybe this:
- the declaration of the relevant variables, within their scope (method? class?)
- the method and class the snippet is part of.
- all the callers to the method behaving badly.
4.
How can your code be to large to publish? Did you type tens or hundreds of lines, and then discovered nothing really worked? Then start developing in an incremental way: create something small, observe it, and change it until what you have works the way it should; only then start expanding it a little. Repeat this process until done. Do not a big thing and then observe its failure, since by then you will have several bugs and their combination will significantly increase your debugging problems.
|
|
|
|
|
Sure. Yeah, I normally do go step by step, but with this error (which seems so bizarre), other than the delegates, I really didn't know what to check...anyway, still, I'm using no threading or timers, although there are two threads running...I can't remember if there are normally two threads running, which is why I was wondering if delegates used threads. The Debug window is printing:
The thread 'vshost.RunParkingWindow' (0x1930) has exited with code 0 (0x0).
The thread '<no name="">' (0x1e48) has exited with code 0 (0x0).
The program '[7760] Computational Geometry.vshost.exe: Managed' has exited with code 0 (0x0).
Although any time I do a call to this method it prints:
The thread '<no name="">' (0x540) has exited with code 0 (0x0).
After termination, so I guess delegates use threading after all...but shouldn't they be running synchronously, and have the rest of the code block until the delegate terminates?
Anyway, here's the code:
public double PolygonArea2
{
get{
#region invariant check
if (size < 3)
{
throw new Exception("Cannot compute area, \"Polygon\" does not have at least 3 points");
}
#endregion
if (currAreaValid)
{
return area;
}
area = (Double)(Fold(area2FoldHelper, (Object)(new Double()), this[0]).First);
currAreaValid = true;
return area;
}
}
private static Tuple<Object, Point> area2FoldHelper(Point currentPoint, Object totalArea, Point bound)
{
if (currentPoint.Next.Next == bound || currentPoint.Next == bound.Next)
{
return new Tuple<Object, Point>(totalArea, bound);
}
Double areaToAdd = ((Double)totalArea) + Globals.Area2(currentPoint, currentPoint.Next, currentPoint.Next.Next);
return new Tuple<Object, Point>((Object)areaToAdd, bound);
}
public Tuple<Object, Point> Fold(FoldDelegate fdel, Object initObjVal, Point initPoint)
{
Tuple<Object, Point> tuple = new Tuple<Object, Point>(initObjVal, initPoint);
for (int i = 0; i < size; i++)
{
tuple = fdel(vertices, tuple.First, tuple.Second);
vertices = vertices.Next;
}
return tuple;
}
That is all in class Polygon/Polytope (Polygon extends Polytope...for what it's worth, only the Fold method is in the Polytope class)
Here's from static class Global, X = 0, Y = 1, and Coords is an array that represents the coordinates:
public static double Area2(Point a, Point b, Point c)
{
return (b.Coords[X] - a.Coords[X]) * (c.Coords[Y] - a.Coords[Y]) -
(c.Coords[X] - a.Coords[X]) * (b.Coords[Y] - a.Coords[Y]);
}
The tuple implementation I think is very simple and doesn't need to be shown, but if you want it, let me know.
Thanks,
Andy
|
|
|
|
|
1.
aespielberg wrote: I normally do go step by step, but with this error (which seems so bizarre), other than the delegates, I really didn't know what to check...
It isn't a good idea to grow a program while it has known bugs, especially bugs you don't understand.
Rationale:
- old bugs make adding/testing new features more difficult;
- the eventual fix may force you to reconsider the architecture, the algorithm, the data structures, well almost everything; so when you continue to build on a bad initial choice, you either will get stuck with it, or have much more work fixing the mistake later. Fixing early puts you on the better track early on.
2.
aespielberg wrote: so I guess delegates use threading after all
Not really.
Threads execute code; each app starts with one thread, which executes Main().
apps can create additional threads, and tell them what method to execute.
threads do not magically come into existence, you create them, or you hook up to some asynchronous event: timers firing, a serial port reporting some data got available, etc.
delegates are what used to be function pointers, so executing them really executes the method they refer to, and that happens in the current context, i.e. by the current thread.
delegates don't create or otherwise cause new threads.
3.
I would like to see:
- the class declaration itself (the line holding "class" up to the next "{")
- the declaration of "area" and "currAreaValid"
4.
I would like to understand:
- what is size (inside Fold)? is it changing? can I see the declaration?
- what is tuple.First?
5.
You are aware it is good practice to have property getters always invariant, i.e. you calling them more than once must be guaranteed to always return the same result; if that is not the case, you should provide a method, not a property. There is no technical reason for this, it is a convention, and it really helps.
Example: Console.ReadLine() is a method as it returns the next line, a different one each time; don't use a property for this.
|
|
|
|
|
Sure, I can give you that. I didn't really grow the program at all past this...I know that that's a bad idea, that's why I stopped coding after I checked this and found it didn't work properly.
I'm pretty sure this should be a property...it always returns the area of the polygon. That should be invariant, unless in some way the polygon mutates, i.e. I add a point. But if I don't add a point to the polygon, then it shouldn't change the value the property returns.
Class declarations:
class Polygon: Polytope
class Polytope
fields (declared in class Polygon)
private bool currAreaValid;
private double area;
size in Fold is the size of the polygon, this should be the number of points it contains. Size only changes if I add or remove a point from the polygon (but I do not yet have a remove function for the Polygon/Polytope class) Declaration:
protected int size;
It is accessible from a property Size.
tuple.First is the first element of the tuple. In this case I am using 2-tuples, of (Object, Point).
Let me know if you want to see anything else.
~Andy
|
|
|
|
|
OK.
I haven't seen anything really suspicious so far.
I suggest you add some logging and run/debug again.
Logging could be as simple as:
public static void log(string s) {
s=DateTime.Now.ToString("HH:mm:ss.fff")+
" ["+Thread.GetCurrentThread().ManagedThreadID.ToString("X2")+"] "+s;
Console.WriteLine(s);
}
and
log("start");
early on in your app, just for checking log(), and
log("area="+area+" currAreaValid="+currAreaValid);
at the beginning of your getter.
|
|
|
|
|
Hi,
I was already printing to the Debug pane and it prints all the right values at the beginning of the getter...it's just at that one if statement, the value flips. I'm not exactly sure how the stack trace helps here...but anyway, I did find that this doesn't flip the value of the boolean flag the first time it's false, only every successive time....I'm not sure why...
~Andy
|
|
|
|
|
Okay, now this is making no sense.
If I put a break point on the if statement, and then on the internal return statement, and I run my tests, it seems to hit the right sequence of breakpoints.
HOWEVER, if I do the same thing, and this time, on the locals pane, hit the + button to see the values of the "this," it starts hitting the wrong sequence of breakpoints and flipping the value of the boolean.
I'm so confused.
~Andy
|
|
|
|
|
the threadID and the stacktrace come in handy when more than one thread is involved, so you can see how and why some code gets executed when you don't expect it to be.
if you think you are through observing things, and couldn't pinpoint the problem, it becomes time to suspect Visual Studio and/or .NET; yours wouldn't be the first problem today that disappears after a reinstall. See here[^]
|
|
|
|
|
aespielberg wrote: public double PolygonArea2
{
get{...
currAreaValid = true;
}
}
There's the line that sets the boolean to true. Remember looking at objects inside the debugger will evaluate the object's properties. So looking at this inside the debugger can indeed set currAreaValid to true.
Breakpoints aren't active during the debugger's property evaluation, so even if you have a breakpoint placed on that line, the value will change to true without triggering the breakpoint.
|
|
|
|
|
Right-click on a an arbitrary folder, choose Properties -> choose Security-tab -> Advanced -> choose Permissions-tab -> Edit... On the upper part of the window there is now a combobox saying "Apply onto" and the possible options are "This folder only", "This folder, subfolders and files", "This folder and subfolders", "This folder and files", "Subfolders and files only","Subfolders only" and "Files only". How do I modify this programmatically using C#?
|
|
|
|
|
I figured it out:
public struct PermissionInfo
{
public FileSystemRights fileSystemRight;
public AccessControlType allowOrDeny;
public PermissionInfo(FileSystemRights fileSystemRight_, AccessControlType allowOrDeny_)
{
fileSystemRight = fileSystemRight_;
allowOrDeny = allowOrDeny_;
}
}
public enum ApplyOnto
{
THIS_FOLDER_ONLY = 0,
THIS_FOLDER_AND_SUBFOLDERS_AND_FILES = 1,
THIS_FOLDER_AND_SUBFOLDERS = 2,
THIS_FOLDER_AND_FILES = 3,
FILES_AND_SUBFOLDERS_ONLY = 4,
SUBFOLDERS_ONLY = 5,
FILES_ONLY = 6
}
private static InheritanceFlags[] inheritanceFlags = new InheritanceFlags[]
{
InheritanceFlags.None,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
InheritanceFlags.ContainerInherit,
InheritanceFlags.ObjectInherit,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
InheritanceFlags.ContainerInherit,
InheritanceFlags.ObjectInherit
};
private static PropagationFlags[] propagationFlags = new PropagationFlags[]
{
PropagationFlags.None,
PropagationFlags.None,
PropagationFlags.None,
PropagationFlags.None,
PropagationFlags.InheritOnly,
PropagationFlags.InheritOnly,
PropagationFlags.InheritOnly
};
public static void setDirectoryPermission(string directory, string user, PermissionInfo[] permissionInfo, ApplyOnto applyOnto)
{
DirectoryInfo myDirectoryInfo = new DirectoryInfo(directory);
DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
for (int i = 0; i < permissionInfo.Length; i++)
{
myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(
user,
permissionInfo[i].fileSystemRight,
inheritanceFlags[(int)applyOnto],
propagationFlags[(int)applyOnto],
permissionInfo[i].allowOrDeny
));
}
myDirectoryInfo.SetAccessControl(myDirectorySecurity);
}
modified on Wednesday, January 13, 2010 4:11 AM
|
|
|
|
|
Thanks for the wonderful post
I added another parameter to your setDirectoryPermission (..) function - ApplyTo. It is equivalent to "Apply theese permissions to objects and / or containers within this container only" checkbox in the object-tab, which is also the option ApplyOnto .. I think that the code is finished
For this purpose, I changed PropagationFlags with two-dimensional array and I added one more enum "ApplyTo"
Here's how it looks:
public struct PermissionInfo
{
public FileSystemRights fileSystemRight;
public AccessControlType allowOrDeny;
public PermissionInfo(FileSystemRights fileSystemRight_, AccessControlType allowOrDeny_)
{
fileSystemRight = fileSystemRight_;
allowOrDeny = allowOrDeny_;
}
}
public enum ApplyOnto
{
THIS_FOLDER_ONLY = 0,
THIS_FOLDER_AND_SUBFOLDERS_AND_FILES = 1,
THIS_FOLDER_AND_SUBFOLDERS = 2,
THIS_FOLDER_AND_FILES = 3,
FILES_AND_SUBFOLDERS_ONLY = 4,
SUBFOLDERS_ONLY = 5,
FILES_ONLY = 6
}
public enum ApplyTo
{
ObjectAndChilds = 0,
ThisObjectOnly = 1
}
private static InheritanceFlags[] inheritanceFlags = new InheritanceFlags[]
{
InheritanceFlags.None,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
InheritanceFlags.ContainerInherit,
InheritanceFlags.ObjectInherit,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
InheritanceFlags.ContainerInherit,
InheritanceFlags.ObjectInherit
};
private static PropagationFlags[,] propagationFlags = new PropagationFlags[,]
{ {PropagationFlags.None,
PropagationFlags.NoPropagateInherit},
{PropagationFlags.None,
PropagationFlags.NoPropagateInherit},
{PropagationFlags.None,
PropagationFlags.NoPropagateInherit},
{PropagationFlags.None,
PropagationFlags.NoPropagateInherit},
{PropagationFlags.InheritOnly,
PropagationFlags.InheritOnly| PropagationFlags.NoPropagateInherit},
{PropagationFlags.InheritOnly,
PropagationFlags.InheritOnly| PropagationFlags.NoPropagateInherit},
{PropagationFlags.InheritOnly,
PropagationFlags.InheritOnly| PropagationFlags.NoPropagateInherit}
};
public static void setDirectoryPermission(string directory, string user, PermissionInfo[] permissionInfo, ApplyOnto applyOnto, ApplyTo applyTo)
{
DirectoryInfo myDirectoryInfo = new DirectoryInfo(directory);
DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
for (int i = 0; i < permissionInfo.Length; i++)
{
myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(
user,
permissionInfo[i].fileSystemRight,
inheritanceFlags[(int)applyOnto],
propagationFlags[(int)applyOnto, (int)applyTo],
permissionInfo[i].allowOrDeny
));
}
myDirectoryInfo.SetAccessControl(myDirectorySecurity);
}
|
|
|
|
|
Hi I really need a pointer in the right direction im a noob to C# and coding in general but im learning. Ive googled around and cant find anything that can really help me.
I want to capture an image from a camera attached to my PCI Capture card via composite. I can get the image in a win form Picture box by using DirectX.Capture so thats fine but.. I need to know how I send this "Capture" over the network and see this in another form on another computer on the same network.
Just a nod in the right direction please.
Thanks
|
|
|
|
|
Hi,
You have 2 objectives:
1. You need a solution to save the images from PictureBox control.
2. You need a solution to send the saved images over the network.
A solution:
1. You can save the image from PictureBox control once per second (or less time.. as you wish) with the help of Timer control;
2. You can serialize the saved image and send it over the network.
This is in big steps what you need to do, but It's a first look solution!
Cheer's,
Alex Manolescu
|
|
|
|
|