|
Why are you using the for{} statement? For accessing an array of objects, you should use the foreach{} statement...
David Stone
dstone@newcenturytitle.com
Procrastination is like masturbation; it's all good until you realize you just screwed yourself.
-Writing on a bench at college
|
|
|
|
|
David Stone wrote:
Why are you using the for{} statement? For accessing an array of objects, you should use the foreach{} statement...
I don't really see the benefit of this.
Unless using an index on the array object causes a lookup each time, similar to using field indexes in a recordset.
Is this the case here too?
Cheers,
Simon
"Sign up for a chance to be among the first to experience the wrath of the gods.", Microsoft's home page (24/06/2002)
|
|
|
|
|
David Stone wrote:
Why are you using the for{} statement? For accessing an array of objects, you should use the foreach{} statement...
You can only use the foreach statement if the array (collection) does not get modified in any way. IOW If you have to alter a property during the foreach loop, you will get an exception and you have to revert to the for statement.
As far as I can see , the foreach statement is purely a shortcut for
IEnumerator en = Array.GetEnumerator();
do
{
Object obj = en.Current;
} while (en.MoveNext());
MYrc : A .NET IRC client with C# Plugin Capabilities. See
http://sourceforge.net/projects/myrc for more info.
|
|
|
|
|
The idea of creating a new dataset with this data and then adding it as a whole to the database might work (as per post).
Another idea would be to package all the data into parameters (i.e. have a parameter each for: ID, Desc, Status and add all ID's to the ID parameter and all Desc's to the Desc parameter etc...) You can then split these up using T-SQL. I have some script for this if you'd like.
You're probably going to have to weigh up the performance as well as whether or not the "tasks" table can be recreated each time.
HTH
Cheers,
Simon
"Sign up for a chance to be among the first to experience the wrath of the gods.", Microsoft's home page (24/06/2002)
|
|
|
|
|
Hi Simon,
I would Like to have the script.
Thanks in advance .
sarith sutha
|
|
|
|
|
Here's the debug version of my script.
There are a number of assumptions that I took, viz: delimiter value and length and I didn't need to do any escaping of characters in "array".
If you have issues implementing this, feel free to contact me: simon_stewart@hotmail.com
DECLARE @Array varchar(50)
DECLARE @iStart int
DECLARE @iEleFound int
DECLARE @iMaxEleLen int
DECLARE @iNextDelimIndex int
DECLARE @iEleLen int
SET @Array = '1,2,3,4,5,6,7,8,9,65,66,67,100,101,102,654321'
SET @iStart = 0
SET @iEleFound = 0
SET @iMaxEleLen = 5
SET @iNextDelimIndex=0
SET @iEleLen =0
print '---------'
print 'LENGTH of string: ' + CAST(LEN(@Array) AS VARCHAR)
while CHARINDEX ( ',' , @Array,@iStart ) > 0 -- for each element in the array
begin
SET @iEleLen =1 --default
--if not ',' or ''
if (CAST(SUBSTRING(@Array, @iStart + 1, 1) AS char(1)) <> ',') and not(len (SUBSTRING (@Array, @iStart + 1, 1)) = 0)
begin--element found at this index
SET @iNextDelimIndex = CHARINDEX ( ',' , @Array,@iStart+1)
if(@iNextDelimIndex=0)
begin
SET @iNextDelimIndex = CAST (LEN(@Array) AS VARCHAR)+1
print 'latest element is next:'
end
SET @iEleLen = @iNextDelimIndex - @iStart
--DB function would occur here
print 'NEXT DELIM LOCATION-->' + CAST(@iNextDelimIndex AS VARCHAR(10))
print 'VALUE FOUND-->' + RTRIM( CAST(SUBSTRING(@Array, @iStart + 1, @iNextDelimIndex - @iStart-1) AS char(10)) ) + '<--'
SET @iEleFound = @iEleFound + 1
end
else --no element found at this index
begin
print 'do nothing'
end
SET @iStart = @iStart + @iEleLen
end --if (CAST(SUBSTRING(@Array, @iStart + 1, 1) AS char(1)) <> ',') and not(len (SUBSTRING (@Array, @iStart + 1, 1)) = 0)
print '------ ELEMENTS FOUND: ' + CAST(@iEleFound AS CHAR(2)) + ' ---------'
Cheers,
Simon
"Sign up for a chance to be among the first to experience the wrath of the gods.", Microsoft's home page (24/06/2002)
|
|
|
|
|
Hi There,
I'm using David Hall library and is running into a bit of problem. When creating a task, we can supply the user and password in which the task shall run. The problem I am having is that I want to schedule a task to run with all the priviledges of the current authenticated user, in other words, the person running the program should be the person executing the task when the time comes. I am trying to avoid saving the password of the user and sending it to the task scheduler. Are there anyway I can tell the system to grab my credentials and use that to run the scheduled tasks?
Thanks,
|
|
|
|
|
I've recently been playing with command line compiles of my C# projects. Beeing accustom to C and C++ I'm used to building all of my files into object code and then compiling all of hte object code together in the end. Looking around I thought that "csc /t:module" was the answer.
However i was very mistaken. Everything will compile with /t:module just fine and it willa ll apparently compile together at the end by using the /addmodule switch. However the resulting executable is still dependent upon the .netmodule files.
Is there any way to combine the .netmodule files into one assmebly so that it is no longer dependent upon all of these small little object files. My main intent here is to create incremental builds so that I'm not recompiling all of my classes at the same time. Any advice would be appreciated.
Jared
jparsons@jparsons.org
www.prism.gatech.edu/~gte477n
|
|
|
|
|
You can use alink (al.exe) to create an assembly from the modules.
I will warn you, however, that netmodules aren't supported in the IDE and aren't widely used.
csc already supports incremental compilation with the /incremental switch, which should meet your needs. If that's not fast enough - or you don't want to use it - I suggest you break your projects into multiple assemblies.
|
|
|
|
|
Eric Gunnerson (msft) wrote:
csc already supports incremental compilation with the /incremental switch, which should meet your needs. If that's not fast enough - or you don't want to use it - I suggest you break your projects into multiple assemblies.
This was actually what I was looking for. I don't know how I missed it in the documentation. Thanks.
Eric Gunnerson (msft) wrote:
You can use alink (al.exe) to create an assembly from the modules.
However doesn't this still maek the final .exe dependent upon the .netmodules?
Jared
jparsons@jparsons.org
www.prism.gatech.edu/~gte477n
|
|
|
|
|
jparsons wrote:
Eric Gunnerson (msft) wrote:
You can use alink (al.exe) to create an assembly from the modules.
However doesn't this still maek the final .exe dependent upon the .netmodules?
I don't think so. The only real dependencies in .NET are at the assembly level, and alink just grabs all the IL and metadata and creates an assembly from it.
|
|
|
|
|
Maybe I'm not being clear when I say dependent. What I mean by dependend is if Assembly A is dependent on netmodule B then B must be in the same directory as A to execute. Quick Example.
Test.cs
using System;
public class Test {
public void SayTest() { Console.WriteLine("Test"); }
}
Hello.cs
using System;
public class Hello {
public static void Main(String[] args) {
new Test().SayTest();
}
}
Now if I compile with the following lines.
$>csc /t:module Test.cs
$>csc /t:module /addmodule:Test.netmodule Hello.cs
$>al /main:Hello.Main /t:exe /out:Hello.exe Test.netmodule Hello.netmodule
$>Hello
Test
This works as expected. However if I delete the netmodules and try to run Hello again the microsoft debugger comes up. What I want is a way to add the modules completely so that their "netmodule" state is no longer needed.
Jared
jparsons@jparsons.org
www.prism.gatech.edu/~gte477n
|
|
|
|
|
I've done a bit more research, and found out that I was wrong about things work.
There isn't, as far as I can tell, a way to create an assembly from the netmodules that's identical to what you'd get if you went straight to an assembly.
|
|
|
|
|
I've seen a couple of applications that put Icons in the system tray and display text in the icon. Specifically I'm thinking of WeatherBug, which displays current temperature in the system tray. Is there a way to do that using c#? The NotifyIcon class has a Text property, but that controls what's displayed in the balloon, not text shown in the icon itself.
Thanks
|
|
|
|
|
sasdav wrote:
WeatherBug, which displays current temperature in the system tray
I would guess it is just usual icons that is shown in the system tray. You can't put text to the system tray (I think, I'm sure)... perhaps in XP, I don't know!
Rickard Andersson@Suza Computing
C# and C++ programmer from SWEDEN!
UIN: 50302279
E-Mail: nikado@pc.nu
Speciality: I love C#, ASP.NET and C++!
|
|
|
|
|
Yes. At least I think so.
I think what you'd need to do is create a new Image, get a Graphics object from it, draw to that Graphics object, and then save it out as an icon.
Then, open that file as an Icon instance and set the Icon property to that instance.
If you haven't written your app yet, I did a column on one last month. See http://www.gotdotnet.com/team/csharp
|
|
|
|
|
That's a good article, thanks for the link. But (you knew there would be a but, didn't you) I've already got an application that uses a static icon in the System Tray. I built a talking clock that tells me the time every so many minutes that I'm pretty proud of.
Do you have any more detail on adding text to the icon while the program is running?
By the way, you didn't happen to see my question about TAPI, did you? I haven't gotten a single reply on it.
Thanks again.
|
|
|
|
|
I'm not sure what extra detail you need. All you need to do is:
Image i = new Bitmap(16, 16);
Graphics g = Graphics.FromImage(i);
g.DrawString(...);
g.Dispose();
i.Save("test.ico", ImageFormat.Icon);
Icon icon = new Icon("test.ico");
notifyIcon.Icon = icon;
As for the TAPI question, I don't know the answer.
|
|
|
|
|
Well, maybe it's not. This code:
Image i = new Bitmap(16, 16);
Graphics g = Graphics.FromImage(i);
SolidBrush blackBrush = new SolidBrush(Color.Black);
g.DrawString("00",this.Font,blackBrush,new Point(0));
g.Dispose();
i.Save("test.ico",ImageFormat.Icon);
Icon icon = new Icon("test.ico");
notifyIcon.Icon = icon;
Generates this error:
System.ArgumentException: The argument 'picture' must be a picture that can be used as a Icon.
at System.Drawing.Icon.Initialize(Int32 width, Int32 height)
at System.Drawing.Icon..ctor(String fileName)
There is a file called test.ico being created, but when I load it into the icon editor it's just gibberish.
|
|
|
|
|
Another idea:
* When you create the bitmap, use the ImageFormat.Icon format. Create to a Bitmap rather than an Image,
* After you've done the drawing, use:
Icon icon = Icon.FromHandle(bitmap.GetHicon());
I *think* that should work; the docs from GetHicon imply that this should work. Please let me know.
|
|
|
|
|
Excellent! And it doesn't generate the test.ico file either. Thanks very much.
Here's the code I ended up with:
Bitmap i = new Bitmap(16, 16);
Graphics g = Graphics.FromImage(i);
SolidBrush blackBrush = new SolidBrush(Color.Black);
g.DrawString("text",this.Font,blackBrush,new Point(0));
g.Dispose();
Icon icon = Icon.FromHandle(i.GetHicon());
notifyIcon.Icon = icon;
|
|
|
|
|
Hi all, just a small question:
What is the correct way to try-catch in foreach loop?
I have , and it seems to work OK, but it looks like it should break the loop, which we dont want.
try
{
foreach (MyClass myclass in anArrayList)
{
DoStuff();
}
}catch (System.InvalidCastException e) {}
Anyone?
READ MSDN
|
|
|
|
|
In the above code snippet it appears that you are wary about 'anArrayList' not containing classes that are or inherit from MyClass. Unfortunately the only way to get it to continue through the loop is to write the foreach code yourself.
IEnumerator ie = anArrayList.GetEnumerator();
while(ie.MoveNext())
{
MyClass myclass = ie.Current as MyClass;
if( myclass != null )
{
DoStuff();
}
else
{
}
} A bit uglier, but it allows you to avoid an exception and you can continue processing the loop.
James
"Java is free - and worth every penny." - Christian Graus
|
|
|
|
|
|
Yes, that is ok.
There is one problem you may run into; and that is if you want to actually execute DoStuff on instances of MyClass as well as instances of a class that inherits from MyClass.
The above technique also won't work with all interfaces because of the way that the GetType() method works in regard to them.
Both cases can be solved by using the is and as operators. Essentially (not what really happens but the effect is the same) the is operator returns true of the left operand can be cast to the right operand. The as operator casts the operand to the type of the right operand, but returns null if the cast cannot be performed. If is returns true, then as will not return null and vice versa.
public class MyClass {}
public interface IMyInterface {}
public class Foo : MyClass {}
public class Bar : MyInterface {}
public class Baz : MyClass, IMyInterface {}
...
MyClass mc;
Foo foo;
Bar bar;
Baz baz;
mc is MyClass == true
mc is IMyInterface == false
foo is MyClass == true
foo is IMyInterface == false
bar is MyClass == false
bar is IMyInterface == true
baz is MyClass == true
baz is IMyInterface == true To me the is and as operators are more versitile than simply comparing the Type returned from GetType()/typeof.
James
"Java is free - and worth every penny." - Christian Graus
|
|
|
|