|
see this line:
for(int j = 2; j < (int)Math.Ceiling(Math.Sqrt(i)); ++j)
u just forgot to add equal sing
for(int j = 2; j <= (int)Math.Ceiling(Math.Sqrt(i)); ++j)
|
|
|
|
|
Indeed you are correct - all those perfect squares just became prime with my code.
-Blake
|
|
|
|
|
yep, but ur code is still not bad
|
|
|
|
|
*chuckles* Well, it's hardly the number field sieve[^] but he only wanted primes less than 1000 so that seemed a little excessive.
-Blake
|
|
|
|
|
this is a one possible solution, u can get a prime number.
not a best way but it works
<br />
using System;<br />
<br />
namespace ConsoleApplication1<br />
{<br />
class Prime<br />
{<br />
[STAThread]<br />
static void Main()<br />
{<br />
for(int i = 2; i < 1010; ++i) if( isPrime(i) ) Console.WriteLine(i);<br />
System.Console.Read();<br />
}<br />
<br />
public static bool isPrime(int number)<br />
{<br />
if(number == 2) return true;<br />
<br />
if(number < 2 || number % 2 == 0) return false;<br />
<br />
for(int i = 3; i <= (int)Math.Sqrt(number); i += 2)<br />
if(number % i == 0) return false;<br />
<br />
return true;<br />
}<br />
}<br />
}
good luck with this code, i hope it helps u.
|
|
|
|
|
Hallo, I'd like to know how can I change windows form menu aspect:
I know how to shape a form: drawing a bmp and than use the trasparentkey and so on...
I think that to apply a skin you should draw a good bmp isn't it?
But, how can I change the graphics of menu?
Thank you very much
|
|
|
|
|
Owner drawn Windows. It's not near as easy as what you're doing for forms (which, btw, is very inefficient - you'd be better of specifying a Window region the old-school way if performance is important).
At the high-level, you can set MenuItem.OwnerDraw to true and handle painting that way. You can, of course, always go low-level and P/Invoke a lot of Win32 APIs, using the Menu.Handle property to get the HMENU for the menu item.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Anyone know if there's a way to open up a new instance of Internet Explorer within C#. I have a web app (all done in C# code, trying to avoid ASP/C# scripting and such if at all possible) that currently takes in data and runs a query on that data. *Ideally*, I'd like to take the result of that query, and have it post to a new web form.
<---signature--->
Your kid gets into Duke.
You pay the tuition.
That tuition goes into my checking account.
My money in my checking account goes into beer, porn, and other such fun. Thank you
|
|
|
|
|
1) C# is all server-side. To control the browser in such a way as to display a new form is client-side.
2) You don't even need to use any fancy javascript to do this. It's been possible since HTML 2.0 (if I remember right; 3.2 at the latest). Simply add a target attribute to your form like so:
<form runat="server" target="_blank">
<!-- blah blah blah -->
</form>
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hi,
i would like to load a singleton class in my default app domain. But also create a new domain and use the same class type with a new instance/singleton.
How to do that?
.:[Greetz from Jerry Maguire]:.
|
|
|
|
|
This shouldn't be a problem. Locking against types to create singletons (as is common for determine if an instance already exists) is supposed to be context-bound. Being that the AppDomain is a separate context, this shouldn't pose a problem.
The following code fragment (that works) should prove it:
using System;
using System.IO;
using System.Reflection;
public class Test
{
public static void Main(string[] args)
{
Singleton.SayHello();
Singleton.SayHello();
if (!Singleton.Foo)
{
Console.WriteLine("Executing entry point in same AppDomain...");
AppDomain.CurrentDomain.ExecuteAssembly(
Assembly.GetEntryAssembly().Location);
}
if (AppDomain.CurrentDomain.FriendlyName != "New")
{
Console.WriteLine("Creating new AppDomain...");
AppDomain domain = AppDomain.CreateDomain("New",
AppDomain.CurrentDomain.Evidence);
domain.ExecuteAssembly(Assembly.GetEntryAssembly().Location);
}
}
}
public class Singleton
{
private TextWriter writer;
private Singleton()
{
Console.WriteLine("Creating new singleton instance...");
writer = Console.Out;
}
private static Singleton instance;
private static Singleton Instance
{
get
{
if (instance == null)
lock (typeof(Singleton))
if (instance == null)
instance = new Singleton();
return instance;
}
}
public static void SayHello()
{
Instance.writer.WriteLine("Hello, world!");
}
private bool foo = false;
public static bool Foo
{
get
{
if (!Instance.foo)
{
Instance.foo = true;
return false;
}
return true;
}
}
}
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hi Heath,
thank you for your sample code.
Let's say i've got a WindowsApplication project. Than i have a class library project which defines the type with the singleton.
Now my WindowsApplication project references the class library assembly for "normal" use of singleton type.
Now i would like to create a new appdomain and create a new instance of my singleton class.
How to do that? The other "normal" instance is alive in the CurrentAppDomain.
.:[Greetz from Jerry Maguire]:.
|
|
|
|
|
You'll need to have an assembly with an entry point. Have another EXE assembly that also depends upon your singleton and forces it to create an instance of itself. In my sample, this was the same executable. Just make another executable that doesn't load your win app - just the singleton.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
hi,
i want to know any class in .net able to convert a byte array (eg. 4 bytes) into a 32-bytes floating point data?
if not, i need to handle by myself with bitrise operation.
any help?
thanks,
jim
|
|
|
|
|
BitConverter.ToSingle .
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hi,
I want to be able to give read/write permissions on certain AD classes to certain accounts.
For instance, I want an account (say user1) to be able to create serviceConnectionPoint child objects. Another example would be to grant an account write access to mS-SQL-SQLServer class.
Any ideas how to do this in C#?
Thanks in advance.
Nidhi
|
|
|
|
|
You can access security only via Interop of adsi32 i think. There is no file security for .NET. I think there is a port in gotdotnet somewhere but you need to fell brave to put that under production. How would you be using mS-SQL-SQLServer?
Cheers,
Erick
|
|
|
|
|
Yes, I can use ADSI interop, but how do I set permissions on a class?
I shall be pulishing some application specific info in the keywords property of mS-SQL-SQLServer.
Regards,
Nidhi
|
|
|
|
|
I'm having a wierd issue where i run a query in a thread and the while(Connection.read()) duplicates my results. Anyone else have this issue? For isntance if I have TestTable with the following values:
TestID
1
2
3
It will console.writeline
1
1
2
2
3
3
Below is sample code:
private void button3_Click(object sender, System.EventArgs e)
{
ThreadStart DBThread = new ThreadStart(StartDBDelegate) ;
Thread DB = new Thread(DBThread) ;
DB.Start();
}
public void StartDBDelegate)()
{
Invoke(new UpdateLoadTableDelegate(RunUpdateNow));
}
public void RunUpdateNow()
{
string source =
"server=testserver;uid=testuid;pwd=testpassword;database=TestDB";
string selectconn1 = "SELECT TestID as dbTestID FROM TestTable"
SqlConnection conn1 = new SqlConnection(source);
conn1.Open();
SqlCommand executeconn1 = new SqlCommand(selectconn1,conn1);
SqlDataReader Connection1 = executeconn1.ExecuteReader();
while(Connection1.Read())
{
Console.WriteLine (Connection1[0].ToString())
}
}
I can reduce this menace by doing the following to the while loop, although I shouldnt have to do this...this is really bugging me. It doesnt do this outside of the thread.
string strTestID = "";
while(Connection1.Read())
{
if(strTestID != Connection1[0].ToString()))
{
Console.WriteLine (Connection1[0].ToString())
strTestID = Connection1[0].ToString());
}
}
|
|
|
|
|
I am currently trying to learn C# and looking at some of the classes available, I have come across a snag. I'm looking at the TimeZone class in the System namespace. It has a static function CurrentTimeZone which is fine, but what if i want to work with a different TimeZone? How do I specify that I wish to work in US Mountain Standard Time or AUS Eastern Standard Time ?
Thanx in advance.
The jubjub
|
|
|
|
|
Through the system clock. This isn't really meant to be changed, but is to help with UTC conversions. The date/time related stuff you should be looking at is the DateTime and TimeSpan structures. Using different combinations of methods and properties between all the date/time-related classes allows you to get offsets, manipulate dates and times, and much more. There's a lot available.
As far as working in different dates and times (like for applications), it must either be set by the operating system or emulated through assuming a different offset (like to display a world clock or something). Just remember to work with UTC and not local dates and times, because some time zones don't recognize daylight savings time (I want to move there!). The TimeZone class is there to help you figure all that stuff out without coding most of it yourself.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Thanx Heath, I was hoping there was something I was missing, like some fancy function or hidden class that would make my life easier so I wouldn't have to do too many calculations. But, to the calculations I go.
I did these sorts of calcs in C++ a while back, by getting the timezone info from the registry, and i was thinking maybe MS has made my life a little easier with .Net.
Thanx for your help buddy.
Jubjub
|
|
|
|
|
Sometimes it's a question of whether it should be allowed. Such time zone assumptions could lead to problems.
Also, here's a good / important tip: much of the stuff in .NET (especially for Windows Forms) is actually their Win32 counterparts wrapped in .NET classes. System.Windows.Forms for example, redefines a LOT of structures and constants found in various headers (especially for common controls) and P/Invokes a lot of methods (countless variations of SendMessage , for example).
So, while classes in a truly object-oriented framework might make things easier by the sheer nature of OO, the functionality isn't a whole lot different (just easier to do: ex. write a C++ COM component and .NET component exposing a CCW and see which one takes less coding and is less prone to coding errors!).
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
This is my first .net web application. I am using asp.net and C#. I have a db lookup that I want a progress bar or cursor change to show the end user the program is loading. I have found many things for the "Web.Form" side but none for the "Web.UI" side. Any suggestions or links?
Thanks,
John
|
|
|
|
|
Actually, this really isn't the place to post this. You'll want the Web Development forum. But anyway...
ASP.NET is server-side technology. In order to display a progress bar, this is all done through client-side scripting. To display a different cursor, see the cursor attribute of CSS. It allows you to set a predefined cursor (like the wait cursor) or to use a custom one (through a url('/path/to/my/cursor.cur') value).
As far as a progress bar, this is either very easy or very hard. To just display some eye candy, you can easily use javascript to resize an image by a certain amount based on a timer. If that amount is to mean anything real, you have to ask the server for status. Perhaps this can be acheived through some nifty use of HTTP Keep-Alive, but I've never seen it. Only other ways are to use ActiveX (IE-specific, Java Applets, or embedded .NET user controls (IE 5.01+ specific) to either display the progress bar or poll the status on the server based on the current user's context and update the resizing image ("progress bar").
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|