473,587 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compile errors (object pointers?)

EntryTeam
55 New Member
Expand|Select|Wrap|Line Numbers
  1. PlayerStatus ps; 
  2. PlayerStatus ps1 = new PlayerStatus();
  3. ps = ps1; //<-
This code causes compilation errors. Help me please to fix it.

Invalid token ';' in class, struct, or interface member declaration (CS1519) - C:\Users\user1\ Documents\Sharp Develop Projects\v2\v2\ MainForm.cs:18, 11

Invalid token '=' in class, struct, or interface member declaration (CS1519) - C:\Users\user1\ Documents\Sharp Develop Projects\v2\v2\ MainForm.cs:18, 6
Another question:
when i pass to a method class-argument, does method duplicate original argument, or uses a pointer to a real object?
Aug 27 '09 #1
8 1734
GaryTexmo
1,501 Recognized Expert Top Contributor
The problem might be in your PlayerStatus class... there shouldn't be anything wrong with the three lines of code you posted. Can you declare and use a variable of type PlayerStatus without the ps = psl?

For your second question, there's a bit of trickyness here. For the most part, it does pass the reference to your object along... here's an example using a list of strings.

Expand|Select|Wrap|Line Numbers
  1. private void SomeMethod()
  2. {
  3.   List<string> test = new List<string>();
  4.   DoSomething(test);
  5.  
  6.   if (test != null && test.Count > 0)
  7.   {
  8.     foreach (string str in test)
  9.     {
  10.       Console.WriteLine(str);
  11.     }
  12.   }
  13.   else
  14.   {
  15.     Console.WriteLine("List is empty!");
  16.   }
  17. }
  18.  
  19. private void DoSomething(List<string> list)
  20. {
  21.     list.Add("item1");
  22.     list.Add("item2");
  23.     list.Add("item3");
  24.     list.Add("item4");
  25.     list.Add("item5");
  26. }
In this example, the list will contain the items that were added in the method. However, if you change the DoSomething method to...

Expand|Select|Wrap|Line Numbers
  1. private void DoSomething(List<string> list)
  2. {
  3.     list = new List<string>();
  4.     list.Add("item1");
  5.     list.Add("item2");
  6.     list.Add("item3");
  7.     list.Add("item4");
  8.     list.Add("item5");
  9. }
The list in SomeMethod won't be changed to the new one you created in DoSomething. In order to make this work, you need to explicitly pass the reference to test by doing the following...

Expand|Select|Wrap|Line Numbers
  1. private void DoSomething(ref List<string> list)
Expand|Select|Wrap|Line Numbers
  1. DoSomething(ref test)
I can tell you that this is how it works, but I can't tell you why... I've just discovered this recently and frankly, it confuses me. Hopefully someone else on this forum can explain why we can pass an existing list and modify it in a method, but if we create a new instance of an object in a method it doesn't get passed back out.

*Edit: After doing some googling, I think I understand why this is the case now. Per this MSDN article...

A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for a new class and have it persist outside the block. To do that, pass the parameter using the ref (or out) keyword.
Aug 27 '09 #2
EntryTeam
55 New Member
I can tell you that this is how it works, but I can't tell you why... I've just discovered this recently and frankly, it confuses me. Hopefully someone else on this forum can explain why we can pass an existing list and modify it in a method, but if we create a new instance of an object in a method it doesn't get passed back out.
Maybe this is because the compiler confuses argument with local variable (same name) ?

Can you declare and use a variable of type PlayerStatus without the ps = psl?
Yep, this works okay.

So, this is my class, you say? There's nothing special about it, actually.

I have one more class (WorldMap) which uses the "problemati c" PlayerStatus class. The PlayerStatus itself is primitive.
Aug 27 '09 #3
EntryTeam
55 New Member
okay, i've found it:
i had to move this code inside method to get rid of the errors.
Expand|Select|Wrap|Line Numbers
  1. ps = ps1; 
it's an old lame mistake i make every time =)))
Aug 27 '09 #4
cloud255
427 Recognized Expert Contributor
Hi,

This is really all about deep and shallow copies, which C# doesn't handle as well as an unmanaged language such as C++.

The accepted solution to this problem is to have your class inherit from IClonable.

In your implementation of the Clone() method you should perform binary serialization of the the object, de-serialize it and return the de-serialized object. Not very elegant is it?

When you want a deep copy of your object you can then simply call the Clone method:

Expand|Select|Wrap|Line Numbers
  1.  PlayerStatus ps; 
  2.  PlayerStatus ps1 = new PlayerStatus();
  3.  ps = ps1.Clone(); //ps is now a deep copy of ps1
As for passing by reference and value:
When you pass a parameter by value, the changes to that variable will only be in effect for the scope of the method. This is kind of like a new instance is created inside the method and is assigned the value of the parameter.

When passing by reference you actually pass a pointer to the object. So any changes made to the object will also happen in the scope from where the function call was made. The variable never 'leaves' the original code block, the method is simply given access to alter the variable's memory block.

See the below code for a more concrete idea:

Expand|Select|Wrap|Line Numbers
  1. void MainFormLoad(object sender, EventArgs e)
  2.         {
  3.             int x = 0;
  4.             byValue(x);
  5.             MessageBox.Show("Original Object: " + x.ToString()); //x is still 0
  6.  
  7.             byReference(ref x);
  8.             MessageBox.Show("Original Object:" + x.ToString()); //x is now 5
  9.         }    
  10.  
  11.         void byValue(int value)
  12.         {
  13.             value += 5;
  14.             MessageBox.Show("Inside value function: " + value.ToString());
  15.         }
  16.  
  17.         void byReference(ref int value)
  18.         {
  19.             value += 5;
  20.             MessageBox.Show("Inside reference function: " + value.ToString());
  21.         }
I'm not too good at explaining stuff like this... forgive me if it is incoherent.
Aug 27 '09 #5
EntryTeam
55 New Member
cloud255
From now on, I'm going to use ref keyword intensivly, although, programm worked well anyway. I'll save lots of memory =))))
Expand|Select|Wrap|Line Numbers
  1. void byReference(ref int value) 
ref has to appear not only in method prototype, but also in method call, right? This is what compiler says))))
----
Interesting code.
Expand|Select|Wrap|Line Numbers
  1. ps = ps1.Clone(); //ps is now a deep copy of ps1 
Actually, I only needed ps to be a reference/pointer, not copy the whole variable into ps.
Aug 27 '09 #6
GaryTexmo
1,501 Recognized Expert Top Contributor
It may have gotten missed because it was at the bottom of an admittedly long, long post (sorry!) but yea I found the answer to my confusion at the following MSDN page. This page also explains your question about ref, EntryTeam. I hope it helps, I know it helped me!

Also, what was the solution? You didn't really post enough code for me to get the context of what changed... I'm curious as to how you solved it :)
Aug 27 '09 #7
EntryTeam
55 New Member
2GaryTexmo

Also, what was the solution? You didn't really post enough code for me to get the context of what changed... I'm curious as to how you solved it :)
The error was appearing, 'cause this code was placed outside any method. I had this kind of errors before, just forgot 'bout it.

@EntryTeam
P.s.:
It may have gotten missed because it was at the bottom of an admittedly long, long post (sorry!) but yea I found the answer to my confusion at the following MSDN page.
It's my mistake, I posted reply before I finished reading your post ;)
Thank you a lot for answering, GaryTexmo and cloud255. MSDN also appeared to be pretty helpful.
Aug 28 '09 #8
GaryTexmo
1,501 Recognized Expert Top Contributor
Oh sorry for not being clear, I just meant I didn't understand how that was your solution... I think I do now :)

Anyway, glad you got it resolved!
Aug 28 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

6
1404
by: William Payne | last post by:
In the following code, should the code compile without casting &n to void* ? My compiler accepts it, but should it? void foo(void*) { } struct bar { int n;
3
6235
by: Daren Fox | last post by:
I am writing an application that will require new commands to be added while the program is still running. I thought I had found the answer in .nets compiler facilities. I would seperate my commands out into a dll and compile this at startup and when the source file is changed. This works fine on startup of the application. But when I try to...
26
2871
by: Martin Jørgensen | last post by:
Hi, I don't understand these errors I get: g++ Persort.cpp Persort.cpp: In function 'int main()': Persort.cpp:43: error: name lookup of 'j' changed for new ISO 'for' scoping Persort.cpp:37: error: using obsolete binding at 'j'
5
5028
by: wong_powah | last post by:
#include <vector> #include <iostream> using std::cout; using std::vector; enum {DATASIZE = 20}; typedef unsigned char data_t;
16
5410
by: desktop | last post by:
I have read that using templates makes types know at compile time and using inheritance the types are first decided at runtime. The use of pointers and casts also indicates that the types will first be know at runtime. But is there some strict definitions that defines runtime code and compile time code that can be used in general?
12
2199
by: Dameon99 | last post by:
Hi, when i try to compile my code it comes up with two errors both in the same function. The errors are both the same: 106assig-4.cpp invalid conversion from `void*' to `int**' Here is my code: int **setMem(int **matrixPointer, int rows, int columns)
22
3594
by: Tomás Ó hÉilidhe | last post by:
I've been developing a C89 microcontroller application for a while now and I've been testing its compilation using gcc. I've gotten zero errors and zero warnings with gcc, but now that I've moved over to the micrcontroller compiler I'm getting all sorts of errors. One thing I'd like to clarify is the need (in C89) for a compile- time...
9
2449
by: beet | last post by:
Hi, I am really not good at c/c++. Some simple code.. #include <stdio.h> #include <stdlib.h> #include <math.h> #include "simlibdefs.h"
2
6912
by: Andrus | last post by:
I need compile in-memory assembly which references to other in-memory assembly. Compiling second assembly fails with error Line: 0 - Metadata file 'eed7li9m, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' could not be found Saving assembly to Windows temp directory for referencing only creates huge amout of temporary
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6621
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.