|
Thank you so much, now everything is crystal clear
|
|
|
|
|
Like others have said, you can't cast an array of something into an array of something else. Ever. You have to loop, one way or the other. This is part of the philosophical heritage of C# - stuff that's very expensive to execute shouldn't be so simple to write that it routinely fools people into thinking it's inexpensive.
However, the .NET Framwork doesn't necessarily play by those rules, so a look in the class library can sometimes help you avoid writing the loop yourself. Look at System.Array.Copy and System.Array.ConstrainedCopy, for instance (or ConvertAll).
In your particular example I would suggest a generic version of Display(), though, as in:
public static void Display<T>(T[] arr)
{
foreach (T e in arr)
{
Console.WriteLine("{0} ", e.ToString());
}
Console.WriteLine();
}
public static void Display(object[] oarr)
{
Display<object>(oarr);
}
Provided that you rename the array called "i" (name clashes with the for loop counter, at least in VS2005) you can remove the comments and it will work like a charm. Note that you DON'T need to specify the type (as in Display<int>(intArray); ), as it is inferred by the compiler.
For added value, you can also insert the keyword "params" before the parameter declarations, as in:
public static void Display<T>(params T[] arr)
public static void Display(params object[] oarr)
That way, you can invoke them like this:
Display(1, "hello", 2, "word");
Display<int>(1, 2, 3, 4);
If you do anything more complex than ToString() (or something else that's inhereted from System.Object ) on the elements, you will probably have to constrain T (using the where keyword) to make sure that it implements some interface or other.
I still think generics are sexy... wonder how long that will last...
Later,
--
Peter
modified on Wednesday, May 14, 2008 6:19 PM
|
|
|
|
|
Great, it did work like a charm. And now you've made me a fan of Generics too. And as you said I didn't specify the type and it was working fine.
Thanx a lot.
|
|
|
|
|
I am trying to save in image into an MS SQl amd keep getting this error "Must Declare the Scalar Variable"
below is my code for the insertion;
OdbcCommand cmd = new OdbcCommand("INSERT INTO dbo.photo (photo) VALUES (@BLOBData)", myConnection);
//Save image from PictureBox into MemoryStream object.
MemoryStream ms = new MemoryStream();
picture.Save(ms, ImageFormat.Jpeg);
//Read from MemoryStream into Byte array.
Byte[] bytBLOBData = new Byte[ms.Length];
ms.Position = 0;
ms.Read(bytBLOBData, 0, Convert.ToInt32(ms.Length));
//Create parameter for insert statement that contains image.
OdbcParameter prm = new OdbcParameter("@BLOBData", OdbcType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false,0, 0, null, DataRowVersion.Current, bytBLOBData);
cmd.Parameters.Add(prm);
myConnection.Open();
cmd.ExecuteNonQuery();
myConnection.Close();
}
catch (OdbcException ex)
{
MessageBox.Show(ex.Message);
}
Any help?
a novice
|
|
|
|
|
|
I need a little help with my ctcss coder/encoder i have to programs coder and encoder. First take signal from microfone and put signal (100 or 200hz) an d send to second computer using audio cabel (that i have). On second pc i need detect ton and if its is set to this what i send it will remove it. Could You tell me how to remove this signals? some codes? the signals is in array of byte (length 400) sampling rate 8000.
|
|
|
|
|
Dears,
I've a web application in which every this is working okay offline, but after piblishing the app online i had a problem saving arabic data from the application into the database which in my case sql server 05 it is saved as ???????????????????? 'question marks' columns collations is set to Arabic_CI_AS and my fields are all nvarchar or nchar and when i manually enter any arabic data into the database server online it is saved correctly and i can also read it correctly from my application
it would be gr8 if some one has got a solution for this.
Thanks in advance
Yours
Ahmad Abubakr.
|
|
|
|
|
Posted in three forums.
Does it matter what I write here? Someone just has to say something about it.
|
|
|
|
|
Can any one help
How to create the Setup file in C# Console Application in .Net
|
|
|
|
|
bruze wrote: How to create the Setup file in C# Console Application in .Net
Not sure what you want to know. You add one to the project using the menu "Add Item"
led mike
|
|
|
|
|
Do you mean setup file which will install your console application to PC?
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
|
|
|
|
|
How to create the executable file in c# console application in .net
|
|
|
|
|
bruze wrote: How to create the executable file in c# console application
The Executable is already created as soon as you compile your code. You can find the executable of your C# Console Application in Bin -> Debug folder of your project.
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
|
|
|
|
|
|
I'd like to have a globals class in my app with a global dictionary, but am having trouble getting past the error below. Any ideas or suggestions?
<br />
using System;<br />
using System.Collections.Generic;<br />
namespace GlobalDict<br />
{<br />
public partial class Globals<br />
{<br />
public static Dictionary<string, int> GlobalInts = new Dictionary<string, int>()<br />
{<br />
{"one", 1},<br />
{"two", 2},<br />
};<br />
}<br />
class Program<br />
{<br />
public static Globals g = new Globals();<br />
static void Main(string[] args)<br />
{<br />
int i = g.GlobalInts["two"];<br />
<br />
}<br />
}<br />
}<br />
Sincerely,
-Ron
|
|
|
|
|
Aside from warning you about using globals AND public members, you can't use an instance. Try this syntax: Globals.GlobalInts["two"]; If you remove the static modifier, you'll be able to use the instance reference.
Scott P
“It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.”
-Edsger Dijkstra
|
|
|
|
|
Lets say i however fire the Close method.and close it is fine. but i want to confirmed it is actually close and dispose with that resources.
Because i have a page and i have used a TVgrabber componenet and show TV card tuner, but when i going to another page closing that..it gets flicker with my page. I couldn't close that Grabber properly also i want to dispose all in.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace ItvC
{
public partial class frmTVX : Form
{
public static frmTVX tv = null;
public frmTVX()
{
InitializeComponent();
tv = this;
}
private void Close()
{
this.TopMost = false;
this.Close();
frmMain.FormMain.Show();
}
}
}
|
|
|
|
|
Well...
First, you're calling Close() from within itself. Eventually, you're gonna run out of stack space.
Second, the last line in Close() will never be executed because you're recursing back to the same function before it gets there.
I'm surprised your system hasn't caught fire yet as a result.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
i have created the service project and sometimes it gives some exceptions. i want to handle them and show the message to the user by MessageBox . but in this context there is no MessageBox . How can i use the MessageBox?
|
|
|
|
|
It's generally not a good idea to have a service interact with the user.
If you really know what you are doing, you have to add a reference to System.Windows.Forms to show a MessageBox. For it to actually appear, ensure the checkbox for Allow service to interact with desktop is checked (service applet - properties).
How about logging the events, or sending out an email when something fails?
Cheers,
Vikram.
The hands that help are holier than the lips that pray.
|
|
|
|
|
|
You can't/shouldn't.
Instead, write an system tray application that monitors the service and puts the message box up itself...
[EDIT] Why did you vote this a one? Did you want me to write the code for you too?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
modified on Wednesday, May 14, 2008 3:31 PM
|
|
|
|
|
It wasn't the OP who 1-voted you, it was someone with gold status. He(?)'s bronze, so if it was him the average with my 5 would've been 4.2, not 3.
You know, every time I tried to win a bar-bet about being able to count to 1000 using my fingers I always got punched out when I reached 4....
-- El Corazon
|
|
|
|
|
With a service it's a very bad idea to assume there is a user at all. Services are by definition assumed to have no user present, supervising them. Often services are running on a remote computer like a file or application server that may be in another country from the person responsible for it.
Keep this in mind at all times when writing a service, it should handle everything possible on it's own.
Have a look at the System.Diagnostics.EventLog component. That is a commonly expected and accepted way for a service to indicate an error of some kind. Avoid the temptation to put too much in that log, your users won't appreciate it. Stick to truly important messages.
Then users can view a problem in the Event log in windows if they have one or use many automated tools for working with the event log (like emailing them if an error occurs etc etc).
"The great pleasure in life is doing what people say you cannot do."
- Walter Bagehot
|
|
|
|
|
thanks. I got another problem with service project.that is when i install it after restarting the machine service is running, but the name of the exe file which installed is displayed in ADD/Remove Prog's. through it can be uninstalled. is it ok to display it in ADD/Rem Prog's.
|
|
|
|