|
What you are storing in the hash table is not the array itsef, but a reference to the array. In the second example you are storing two references to the same array. That means that any change you make to the array will be visible from both references.
You don't need to use different variables, you only have to create separate objects.
---
b { font-weight: normal; }
|
|
|
|
|
Guffa,
Thanks for your quick reply!
I wasn't aware of the hashtable only storing a reference of the array rather than the array values. That explanation was of great help to me.
I'm afraid, but I'm still stuck on how to go about creating separate objects. The main purpose of this particular hashtable is to save array values (the actual values, not a reference!) as 'y' changes. Could you give me an example as to how to resolve this without creating a new array for each item?
Thank you!
Mazze
|
|
|
|
|
But you have to create a new array for each item. A hash table is not capable of storing array values directly, you have to put the values in an array and store the reference to the array in the hash table.
I believe that you are not aware of where exactly you are creating the array.
The code:
int[] y = { 1, 2, 3 };
is the short form of:
int[] y = new int[] { 1, 2, 3 };
which is the same as:
int[] y;<br />
y = new int[] { 1, 2, 3 };
So, in that statement you are declaring a variable that can hold a reference to an array, then you create the array and store the reference in the variable.
If you want to make a copy of an array, you can use the Clone method. As long as the data in the array is a value type, it will create a fully independent copy of the array.
The Clone method returns an object reference, so you have to cast it to the correct type:
int[] y = { 1, 2, 3 };<br />
int[] z = (int[])y.Clone();
---
b { font-weight: normal; }
|
|
|
|
|
Hello
Your mistake in Example2 was changing the values of the same array, which eventually will change in the HashTable too.
Proposed solution:
Dynamically allocate another Array using the same name:
eg.
Hashtable x = new Hashtable();
int[] y = { 1, 2, 3 };
x["1"] = y;
y = new int[] { 4, 5, 6 };
x["2"] = y;
MessageBox.Show(((int[])x["1"])[1].ToString());
MessageBox.Show(((int[])x["2"])[1].ToString());
PS.
Beware of the Garbage Collector
Regards
|
|
|
|
|
Guffa,
Nader,
Thanks for helping me resolve this issue. I now have a grasp of what's meant by creating two separate objects. Everythings works fine! Great!!!
As mentioned earlier, I'm a newbie so sorry for asking questions that are crystal clear to advanced programmers.
Once again, thank you guys!
Have a great day!
Mazze
|
|
|
|
|
how to Windows Service.plx help me by step by step solution;
|
|
|
|
|
|
|
foysal mamun wrote: how to Windows Service
|
|
|
|
|
Hi!
How could I capture video from webcam (Microsoft LifeCam VX-3000) using .NET 2.0 or WinFX (.NET 3.0) and save it to WMV or AVI? Actually, using WinFX (WPF, WCF, etc.) is preferable.
Thank you in advance!
--
Yours sincerely,
Dmitry Alekseenko
|
|
|
|
|
Why you were voted a five I have no idea, maybe someone appreciates a clear-cut question following the posting guidelines...
Anyway I don't think that you can do this directly with .NET 3 (but not being able to install it on Win2K I wouldn't know). The easiest solution would be to grab DShowNET (just do a CP search for it), it's a wrapper around the DirectShow API and provides four or five very good examples on capturing from a webcam. There is even one example which does exactly what you want.
|
|
|
|
|
A question like this can be quite open-ended and actually requires quite a lot of knowledge of the camera as well as the internal processes available.
I would suggest you take a peak at Coding4Fun which is out on the MS website. (I think it is www.coding4fun.microsoft.com) There is a project out there on building a baby monitor system.
The article not only shows what the developer did to get it to work, but also illustrates the steps you would do in answering this question.
|
|
|
|
|
I have an unfinished C# puzzle game I wrote, and I want to change it to use a text file to load the levels by parsing the text. I don't know how exactly to do it. I would also like to be able to read a directory and list each text file in an array, so I don't have to alter the code to add new levels.
Can someone please help?
|
|
|
|
|
you can search some data in msdn about File's read and write.Believe me , it is very easy.you can do that by yourself
|
|
|
|
|
There are a few processes you could look into for accomplishing this...but it depends on the design you want to do.
First you need to know the exact data you need to save and determine if you want to expose this data in a simple text file. (cheaters can hack a text file to death)
Then you need to determine if you can get all of the puzzle data into a single object. This helps drive your design.
Finally I find the best way to parse such data is to let the system do as much work as possible. This can easily be accomplished simply by using a binary serializer / deserializer to accomplish the task.
So the steps you would then follow -- if this looks good to you -- is as follows:
Define your object and all collections within that object.
Make certain that every class that makes up that puzzle object has the [Serializable] attribute added to the definition. Also make sure that your puzzle object consists of (hopefully) nothing but private data with getter/setter logic to encapsulate the data and expose the data to the serializer.
Next research what you need to do to get a BinarySerializer cooking (which isn't much) and you will also need System.IO to do your file stuff.
Then create your stream, create your serializer for your object type, then do a serializer.Serialize( stream, myPuzzleObject );
You will find your object is dumped into the filename the user defines if you use the FileDialog and you will find that when you do a serializer.Deserialize() that you have a fully populated puzzle object loaded. This is how I persist my Sudoku puzzles for my library displays, library adds, and current puzzle state saves.
|
|
|
|
|
Ok, well, it's a puzzle game much like PopCap's Mummy Maze. The parts that I would put in a text file is simply the starting point of the player and enemy (or enemies), the exit, and the walls. All of which I currently have hard coded into an array. Since I only have one level, it seems nice enough, but on a 25x25 grid, and a two dimensional array, it takes a lot of lines of manual code to fill it up for just one level. That's why I don't want to have to hard code several levels. It would be so much easier if I could just compile the program once, and just add files as I go along, thus adding more levels.
Sure, cheaters could just place themselves next to the exit, but what is the fun in that?
As for all this serializing stuff...I don't think I have learned that yet. Also, I work much better with examples. Preferably commented examples. So, could you please point me toward one? I would very much appreciate it.
I know how to open files and use the stremreader, but I only know how to use the StreamReader for one line at a time, though I suppose I could use a For loop to go through the characters and then use a Switch...Case statement to parse the text into the correct object for the array. But I was hoping for something easier.
Assuming it is possible to make it easier.
|
|
|
|
|
Tip: If you want to learn by example, go to MSDN and search for the key items you want to do ... such as Serialize. You can find quite a bit of documentation and usage info out there. But here is something to get you started quickly. I used shorthand for the public accessors...you SHOULD know how to implement those!
private PuzzleArray level = new PuzleArray();
...
private void SaveLevel ( PuzzleArray currentLevel, string path )
{
using (FileStream stream = File.Create(path) )
{
BinarySerializer ser = new BinarySerializer(typeof(PuzzleArray));
ser.Serializer(stream, level);
}
}
private void LoadLevel ( ref PuzzleArray currentLevel, string path )
{
using (FileStream stream = File.Open(path))
{
BinarySerializer ser = new BinarySerializer(typeof(PuzzleArray));
currentLevel = (PuzzleArray) ser.Deserialize( stream );
}
}
....
[Serializable]
public class PuzzleArray : List<Puzzle>{}
[Serializable]
public class Puzzle
{
Point playerPosition;
public Point PlayerPostition{get; set;}
Point enemyPosition;
public Point EnemyPosition{get; set; }
Point[] walls
public Point[] Walls{get; set;}
private Point lastMove = new Point();
}
|
|
|
|
|
i cannt show a form in center of its parent form. i use to show "form1.ShowDialog();"
plx help me;
|
|
|
|
|
you can set form position property .
|
|
|
|
|
setting property like this:
this.StartPosition = FormStartPosition.CenterScreen;
|
|
|
|
|
but it dose not work. it show at topleft conner. i can solve the problem by setting the properties. plz help..
|
|
|
|
|
Hello
This should Do it:
private void MyShowMethod()
{
Form1 form = new Form1();
form1.StartPosition = FormStartPosition.Manual;
int X = Left + Width / 2 - form1.Width / 2;
int Y = Top + Height / 2 - form1.Height / 2;
form1.Location = new Point(X, Y);
form1.ShowDialog();
}
Regards
|
|
|
|
|
Open the form in Visual Studio.
Look at the properties for that window.
Change StartPosition from WindowsDefaultLocation to CenterParent.
|
|
|
|
|
Hi,
i had a problem doing drag-dropping of images, i could only drag to a picturebox control and it will be the main image and displayed in the center of the picturebox. however, i wanted to be able to drag an image and drop it in any position of a picturebox which already contains an image(eg. the main pic is a photo, and i want to add a star image on the bottom left corner of the photo) and also to render both together. Do anyone have any idea on how can i go about it?
Thanks
|
|
|
|
|
Hi!
That's something you cannot do with a regular PictureBox . The PictureBox holds (and displays) a single image only.
To achive the effect you desire, you'll have to create your own control. This will have to have a collection of Image s and perform painting and handling of these images on its own.
Regards,
mav
--
Black holes are the places where god divided by 0...
|
|
|
|