|
How do I run constructor without calling it's method's C# ?
for example:
class A
{
private static A _instance=new A();
private A()
{
SomeSingleTonClass.Instance.Items.Add(this);
}
public static A
{
get
{
return _instance;
}
}
}
thanks.
Dima
-- modified at 18:58 Monday 29th October, 2007
|
|
|
|
|
You might want to look at this implementation of a singleton: http://blog.colinmackay.net/archive/2007/09/05/393.aspx#395[^]
The version I have in the main blog entry is fine for single-threaded applications. The version in the comment is thread-safe.
Essentally you need to call the constructor in the public static property.
|
|
|
|
|
Now you've modified your post...
Dima.HEMEHT wrote: How do I run constructor without calling it's method's C# ?
The constructor will run when it needs to. Do you want the constructor to run at an explicit time? And if so why?
Actually, what are you trying to do with this class? It doesn't make sense...
|
|
|
|
|
Colin Angus Mackay wrote: Now you've modified your post...
Oh, that's why it didn't make any sense...
---
"Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things."
-- Douglas Adams
|
|
|
|
|
Guffa wrote: Oh, that's why it didn't make any sense...
I don't think it made much sense the first time round either. I interpret these things as best I can...
|
|
|
|
|
The constructor will run when it needs to.- This is my problem.
I want to make abstract class, which will register it self in any collection.
But because Just In Time feature .Net runs constructor only when I reference to class.
public class MyTypesCollection //singleton
{
...
}
public abstract class TypeBase
{
public TypeBase(String name)
{
MyTypesCollection.items.add(name,this);
}
}
public MyNewType: TypeBase
{
private static _instance=new MyNewType("MyType");
public TypeBase(name as string):base(name)
{
....
}
}
If this code were used in JAVA it were initialize by itself , cause JAVA doesn't have JIT
|
|
|
|
|
Dima.HEMEHT wrote: I want to make abstract class, which will register it self in any collection.
But because Just In Time feature .Net runs constructor only when I reference to class.
That has nothing to do with the JIT compiler. The C# compiler compiles into IL code, and the purpose of the JIT compiler is to create the native machine code when the application is executed.
It's defined in the language when the values for the static members are created. It's only specified that the static constructor (which will create the object for the _instance variable) is executed before any members of the class is used. If your class is never used, the static constructor doesn't have to be executed at all.
Dima.HEMEHT wrote: cause JAVA doesn't have JIT
Yes, it does.
---
"Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things."
-- Douglas Adams
|
|
|
|
|
Ok i'm wrong.
What about answer?
|
|
|
|
|
I thought that was evident from my reply. It can't be done, as there is no guarantee that the static constructor will run if your class isn't used.
You could use reflection to loop through all the classes in all the assemblies, to look for classes that inherits your class.
---
"Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things."
-- Douglas Adams
|
|
|
|
|
|
Hy
I want to put a variable like DateTime to a DataSet.
Is that possible and if ... how can I do that?
Can anybody help me?
thx
|
|
|
|
|
mihksoft wrote: I want to put a variable like DateTime to a DataSet.
Is that possible and if ... how can I do that?
Yes it is possible. Surely you just ensure the column type will accept DateTime object and assign the value appropriately. What code have you written and what problems are you having with it? (I assume, of course, that you have actually tried this and somehow it doesn't work. I'd be most disappointed if you hadn't done the most basic research before asking here)
|
|
|
|
|
I did it !
|
|
|
|
|
I always get an argumentexception on the following code, and I don't know why.
bmp = new Bitmap(typeof(Form1), "toProcess.bmp"); //Read an image from hdd
MemoryStream mstream = new MemoryStream();
bmp.Save(mstream, ImageFormat.Jpeg); //Save image into MemoryStream in jpeg
jpegByteArray = new byte[mstream.Length];
mstream.Read(jpegByteArray, 0, (int)mstream.Length); //Read mstream into byte[]
MemoryStream jpegStream = new MemoryStream();
jpegstream.Write(jpegByteArray, 0, jpegByteArray.Length); //Write byte[] back to another MemoryStream
jpegstream.Position = 0;
Image newimg = Image.FromStream(jpegstream); //THIS throws an ArgumentException
I checked in debugging mode and every variable has the exact same number of bytes, and if I only save the image into a stream and read it right back, then it works. Please save me from pulling my hair out and shoot me with any help you can think of!
-- modified at 18:50 Monday 29th October, 2007
|
|
|
|
|
using System.IO;
private byte[] bImage;
private string strImagePath;
private FileStream objFileStream;
private MemoryStream objMemoryStream;
private Image objImage;
private void convertImageToByteArray()
{
strImagePath = "C:\\toProcess.bmp";
objFileStream = new FileStream(strImagePath, FileMode.OpenOrCreate, FileAccess.Read);
bImage = new byte[objFileStream.Length];
objFileStream.Read(bImage, 0, Convert.ToInt32(objFileStream.Length));
objFileStream.Close();
}
private void convertByteArrayToImage()
{
objMemoryStream = new MemoryStream(bImage);
objMemoryStream.Flush();
objImage = Image.FromStream(objMemoryStream);
objMemoryStream.Close();
}
|
|
|
|
|
Thank you! In the meantime I managed to solve my problem. Instead of using
mstream.Read(jpegByteArray, 0, (int)mstream.Length);
to read the MemoryStream's content into the byte[] I should have used
jpegByteArray = mstream.ToArray();
Don't ask me why this works and the other method doesn't, thereotically both should work. Anyway, I had to do this using only in-memory objects, because this is for realtime video stream processing.
Cheers
-- modified at 6:51 Tuesday 30th October, 2007
|
|
|
|
|
Hi
I have created dll using C# and add reference to another project that was written in VB.net.
so I have two project, one is using C#, the other one is using VB.net. Both written in Visual Studio.net 2005.
When I set break point of my function from dll project because that's what I want to debug.
So I started running CallingDll VB project in debug mode, I got following message
<br />
The following module was built either with optimizations enabled or without debug information:<br />
C:\myDll.dll<br />
<br />
To debug this module, change its poject build configuration to Debug mode. To supress this message, disalbe the "Warn if no user code on launch" debugger option.<br />
Where should I change it?
I went to Project\Properties, I couldnt' find anything that said Debug Mode.
I looked into Debug, I found StartAction, Start Options, enable debuggers.
I changed put Start external program: "c:\CallDll.exe" and then build and run the project in debug mode. but I still got the same message and I couldnt' debug myDll.dll at all neither from myDll project nor callDll project.
I also looked into Build.. I dont 'see anything that is saying debug mode.
I check the MSDN and saying the same thing.. to change it to Debug mode. But where should I go and change it so myDll.dll can be debug.
I recreated myDll.dll and CallDll.exe using visual studio 2003. I had no prolem debugging. but not with visual studio 2005.
Would anyone please tell me how to debug myDll.dll from another project like CallDll??
Thanks
|
|
|
|
|
[Build][Configuration Manager] ?? Just a guess.
"More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF
"This time yesterday, I still had 24 hours to meet the deadline I've just missed today."
|
|
|
|
|
I found [Build][Comfiguration Manager] in Vistual Studio 2003. But not in visual studio 2005. I'm using Visual Studio 2005. I need to know how to debug myCShart.dll from different program that could be written in VB.Net, C# or C++.
|
|
|
|
|
Hey ya'll,
I've got a user control where I set the background to some color and when its disabled I want to lighten the color just a tad when its disabled. I know the ARGB value. I tried just setting the alpha value and the userControl didn't care for that.
[Question]Is there a formula, given the RGB value that calculates the lightened value for that color?
Thanks
MIke
Life is not measured by the number of breaths we take, but by the
moments that take our breath away. "George Carlin"
|
|
|
|
|
To lighten a colour, just increase the R, G, and B values by a set amout (say for example 10). But of course, don't go over 255.
So, say you had blue: 0,0,255; and you wanted to make it lighter: 40,40,255
Or if you had a dark yellow: 150,150,0; a little lighter: 190,190,40;
Aha, here it is. There's also this:
ControlPaint.Light(color, percentageBrighter);
Of course, changing the values yourself gives you more control.
There's also ControlPaint.Dark
My current favourite word is: Waffle
Cheese is still good though.
|
|
|
|
|
Duh, so obvious that I completely overlooked it guess I'm gettin tired.
Thanks for the input....worked like a charm!
Mike
Life is not measured by the number of breaths we take, but by the
moments that take our breath away. "George Carlin"
|
|
|
|
|
Hi,
I have always used the following coding when formatting dates for use with SQL:
bookingMadeDateTime.ToString("yyyy-MM-dd hh:mm:ss tt") <-- or very similar
however I have now been advised by a prospective user that they are using the following formats and the sql insert is failing:
Date: dd.MM.yy
Long date: dddd, dd' de 'MMMM' de 'yyyy
Time: hh:mm tt
The error is:
There was a syntax error in the date format. [ Expression = 2007-10-29 12:32:33 p.m. ]
The dates are entered using a datetime picker and I thought that by formatting the way I have, internationalization would be an issue. Also, the actual expression looks fine to me.
Can someone help me with this please.
Thanks.
Glen Harvy
|
|
|
|
|
Glen Harvy wrote: I have always used the following coding when formatting dates for use with SQL:
bookingMadeDateTime.ToString("yyyy-MM-dd hh:mm:ss tt") <-- or very similar
however I have now been advised by a prospective user that they are using the following formats and the sql insert is failing
Why would you be formatting dates for SQL Statements. Surely you would be passing this as a parameter? If you didn't use parameters your code would be highly susceptable to SQL Injection attacks, which would not be good.
Please read SQL Injection Attacks and Tips on How to Prevent Them[^] to secure your application. Incidentally it will also solve your current date formatting problem.
|
|
|
|
|
Colin Angus Mackay wrote: Why would you be formatting dates for SQL Statements. Surely you would be passing this as a parameter? If you didn't use parameters your code would be highly susceptable to SQL Injection attacks, which would not be good.
As the input is from a datetime picker and the user has no option but to select a date, I wasn't concerned with injection attacks.
I am happy to rewrite the code as you suggest ie paramaterized however I don't see this as solving the problem at hand ie the user hast his computer to use non-US English.
Does .Net automatically tanslate date formats?
Glen Harvy
|
|
|
|