|
Thanx now I understand that you have to convert the values of the array and not the array itself. But I still dont understand how come the string and the Employee arrays are being accepted by the display method. Has it got something to do with reference and value types. Because when I changed the Employee class to a struct I got an error. If you explain. How is it the object array is giving preference to ref types and not to value types, if all have been derived from object.
|
|
|
|
|
All value types (built-in value types and structs) inherit from the abstract class ValueType, which is treated differently by the compiler by storing the data inline instead of using a reference.
When you declare an array of strings, the array contains references to the strings, and the strings are stored outside the array. When you declare an array of ints, the array contains the int values themselves.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Yes but what I dont understand is, why is the object array, accepting string[] and not int[].
where as an object variable will accept a string or an int or anything. Sorry if I am being trouble.
|
|
|
|
|
humayunlalzad wrote: Yes but what I dont understand is, why is the object array, accepting string[] and not int[]. where as an object variable will accept a string or an int or anything.
When you store a string reference in an object reference variable, it's simply cast into an object reference. As the compiler knows that it's safe to store a string reference in an object reference variable, there is actually no conversion at all, the reference is just stored in the variable.
When you store an int value in an object reference variable, the value itself is not stored in the variable. Instead a new object is created on the heap, the int value is stored in this object, and the reference to the object is stored in the object reference variable. This is called boxing.
A string array can be handled as an object array, as they both contain references. An int array can not be handled as an object array, as it doesn't contain references. Instead each int in the array has to be boxed, and the references placed in an object array.
Boxing of single value types is done automatically, but there is no automatic boxing of all items in an array, as this could get very inefficient. Creating a value type array is efficient as it's still only one object, but if you could trigger an automatic boxing of all items in the array, you could accidentally create thousands of new objects.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
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.
|
|
|
|
|