|
Can someone help me!? I know in JavaScript I could use eval() to achieve this but I am not sure how to accomplish the same things in C#. Let's say I had a method in which one of the arguments was a string or any variable for that matter, and based on that variable passed to the method another method of the given name in the variable would run...(see example below), I am just getting into C# and any help with this would be much appreciated...!
public static void RunAMethod(string methodname)
{
methodname(); //method to run
}
///---- so I could do this from another method
RunAMethod("runwhatevermethodIwant");
///-----
Is this possible???
|
|
|
|
|
|
Reflection is way slow. You should consider to use delegates, which are quite similar to C's function pointers. The topic is not very simple, but delegates are very powerful and elegant. Here[^] is an introductory article on MSDN Magazine.
If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker
My Blog - My Photos - ScrewTurn Wiki
|
|
|
|
|
Delegates seem very interesting to me also...although I am having a little difficulty visualizing their usage properly from the MSDN article, is there anyway you could write out a little snippet showing how I could use delegates to call a method whose name is based on a string I enter?
Thanks!
|
|
|
|
|
Can you call a delegate with a method name defined in a string?!? Because this seems to be what he wants.
-----
If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
|
|
|
|
|
No. Not directly at least.
The point is that invoking methods at runtime by their name is not very clean, in my opinion. Since I guess the methods he wants to invoke are parameterless and with no return value, using delegates would be very easy and would improve the robustness of the code.
If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker
My Blog - My Photos - ScrewTurn Wiki
|
|
|
|
|
I agree with you in the way that he should consider using an interface instead instead of calling a method by a name specified in a string.
-----
If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
|
|
|
|
|
Thank you very much for both of your replies, I had a hunch that Reflection might be something I needed to look into, and I'll definitely give it a shot. 
|
|
|
|
|
Is there a way of using a 2D array as a DataSource to controls such as DataGridView or ListBox?
|
|
|
|
|
I seem to recall doing something similar to this with .NET Framework 1.0, or 1.1 (not sure which). I believe I used an ArrayList of ArrayLists for initial data population of the datagrid for a quick test. I never tried manipulating the data of the ArrayList after that. For data manipulation after the initial population I believe I used the native datagrid methods.
|
|
|
|
|
Hi all
At work we have a database that is read only that keeps track of the work we do ( who completed what ticket and so on) I need to take a chunk of the database for the work done by my group then count up certain types of information like who has done how many tickets or how many of one type of problem there was during a certain time.
question 1) can i use the fact that this is read only to my advantage in terms of speed or memory i saw something called sqlreader but i haven't had time to play with it yet
question 2) given the senerio above what would be the most efficient work flow? should i read the database into a dataset then do my filtering from there or something else
unfortunately this is not my strongest subject so all suggestions are appreshated
|
|
|
|
|
I would choose solution 1, coupled with the use of stored procedure. As a piece of advice, if you are only reading from the tables, use the with (nolock) lock hint in your queries.
-----
If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
|
|
|
|
|
since your saying "chunk of data" i would prefer using datasets because..if your using datareaders each time u connect to the database whenever u execute a query..but datasets you can work in disconnected mode..
aneef
|
|
|
|
|
Hi!
I have 2 forms:
Form1 and Form2, how I can write the text of a textbox in Form1 in another textbox in form2?
Thank
Bye.
|
|
|
|
|
There are a lot of ways to do this. The easiest ones are:
- If form B is created by form A, and A has the initiative (e.g. because there is an
event firing in A), let A do the work using a property of B.
- if the initiative is reverse (the slave form B decides when), then the easiest is by
giving it a public event, so the master form A can wire a delegate to it and
un its update method whenever required.
Hope this helps.
|
|
|
|
|
Luc Pattyn wrote: - If form B is created by form A, and A has the initiative (e.g. because there is an
event firing in A), let A do the work using a property of B.
Thaks, but how i can do that?
Any example??
By Windows M
|
|
|
|
|
How does form A change form B's size ? by setting a new value to its Size property.
SImilar for any other property you can imagine (and hence add to a class):
in FormTypeA class:
FormTypeB formB=new FormTypeB();
formB.Show();
...
private void myTextBoxA_TextChanged(object sender, EventArgs e) {
formB.Size=new Size(500, 600);
formB.TextBoxText=myTextBoxA.Text;
}
In FormTypeB class:
public string TextBoxText {
set {
myTextBoxB.Text=value;
}
}
May I suggest you work your way through a general C# book...
|
|
|
|
|
Bind the same datasource to both textboxes. That way a change in one will show up immediately in the other.
|
|
|
|
|
Hi All,
The project i'm working on at the moment needs to draw information on top of a photo. The content of the photo varies massively - the colour and brightness vary a lot.
Can anyone think of a way of colouring the lines and text so that they are visible whatever the background image? and even if a line starts in a bright area and proceeds into a dark area?
My only idea is to use a "negative" approach, but i'm not sure how that can be implemented (easily) in GDI+
Any ideas greatfully received.
Thanks
Jon
|
|
|
|
|
You could probably draw two texts/lines varying the pen width and color, so that each element actually has a small visible outline. For the text, you have to add it to a Path object and use the DrawPath method.
Here you can find some help: http://www.bobpowell.net/faqmain.htm[^]
If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker
My Blog - My Photos - ScrewTurn Wiki
|
|
|
|
|
Hi,
I know of no general solution. Here are some ideas though:
- for lines, one could draw two parallel lines in different colors
- for text, use a heavy font (either bold or black) and make it sufficiently large
- or for text, draw text twice, same font, same size, slightly offset (x+2, y+2)
using different colors
- smart color selection: built a histogram on your image (pick 100 pixels, convert them
to hue value, and now choose a color with the opposite hue = add 180 degrees, full
saturation, full brightness (unless histogram shows image is bright, then use black).
- on a display, use blinking (500 msec on/off)
- on a display, let the added info be drawn differently (other color, wider, ...)
while the user holds down CTRL key
Hope this helps
|
|
|
|
|
Hi,
How can the default OS warning message "CAPS Lock is ON" can be suppressed on Windows XP for a password field in C# (.Net FW 2.0).
Note: Windows XP automatically shows the warning message when the CAPS Lock key is turned on for a password field.
Thanks in advance.
Subrahmanyam K
|
|
|
|
|
You cannot do that in C#. Maybe with some interop, but I don't know.
If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe - but not a personality. - Charlie Brooker
My Blog - My Photos - ScrewTurn Wiki
|
|
|
|
|
Hi,
I've got the following code which reads from an image that has information encoded in as pixels. My job is to scan through the image, and set the value of an array to true if a pixel is black, and false if it is any other color. I have the following bit of unsafe code that performs it:
BitmapData bmpdata = AOIImg.LockBits(new Rectangle(0, 0, WIDTH, HEIGHT), ImageLockMode.ReadOnly, AOIImg.PixelFormat);
IntPtr scan0 = bmpdata.Scan0;
int stride = bmpdata.Stride;
int imgOffs;
try
{
unsafe
{
byte* imgPtr;
for (int y = 0; y < HEIGHT; ++y)
{
imgOffs = y * stride;
imgPtr = &((byte*)scan0)[imgOffs];
for (int x = 0; x < WIDTH; ++x)
{
if (imgPtr[0] == 0 && imgPtr[1] == 0 && imgPtr[2] == 0)
{
AOIMask[y * WIDTH + x] = true;
}
else
{
AOIMask[y * WIDTH + x] = false;
}
imgPtr += 3;
}
}
}
}
finally
{
AOIImg.UnlockBits(bmpdata);
#if DEBUG
AOIImg.Save(@"C:\temp.png", ImageFormat.Png);
#endif
AOIImg.Dispose();
}
However, I was wondering if I could turn this into managed code using InteropsServices.Marshall to copy the pixel data to an array and read through it in a safe context. I was wondering if anyone had any warnings about using this method, such as the stride not being correctly accounted for in the marshalled array (I'm not sure if I have to do this manually or not at all!).
Thanks in advance,
Phil
|
|
|
|
|
You'll have to account for stride manually, but there is nothing to this.
The Marshal.Copy() method just copy blocks of data.
If Stride and (Width * BytesPerPixel) are not equal, then the data at the end of each image row is padded with zeros.
This doesn't matter. Make sure you allocate a byte [] array of size = height * stride, and use marshal.copy to fill it.
Use nested loops to avoid any stride difference:-
for (y=0; y<height; y++)
{
for (x=0; x < width; x++)
{
int offset = (y * stride) + (x * BytesPerPixel);
if (pixels[offset] == black)
}
}
|
|
|
|