p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer (http://p2p.wrox.com/forumdisplay.php?f=574)
-   -   I can't access public varable. (http://p2p.wrox.com/showthread.php?t=94144)

Marek February 15th, 2015 12:45 AM

I can't access public varable.
 
Warning 1 Accessing a member on 'WindowsFormsApplication1.NewItemForm.PriceEach' may cause a runtime exception because it is a field of a marshal-by-reference class C:\Users\Mark\Desktop\Visual C# Folders\C#24 hours completed excersises\Try13\WindowsFormsApplication1\Form1.cs 30 34 WindowsFormsApplication1

Hi

I need help with TryIt 13 exercise. When I click OK button to accept fields on the main form I get above message . I am using Visual Studio 2010 professional , academic edition. Tried to re-assign fields to local variables and display them but it did not help.No idea why original (solution) program work and mine does not. Thanks for help.

Mark

Rod Stephens February 15th, 2015 01:39 PM

Hi Mark,

I don't know why it would do that. I certainly can't imagine why my solution would work but yours wouldn't, at least not for the fields.

But it seems to not like the fields, so you may be able to work around it by converting them into properties instead of fields. Try using these declarations:

Code:

public string ItemName { get; set; }
public decimal PriceEach { get; set; }
public decimal Quantity { get; set; }
public decimal TotalPrice { get; set; }

Let me know if that works.

mmorgan30 February 16th, 2015 02:17 PM

it is probably because you are calling a method of a value type that is defined in a class that implements MarshalByRefObject. Without looking at the code I am assuming its a System.Windows.Forms.Form.

check out https://msdn.microsoft.com/en-us/library/x524dkh4.aspx for a work around.

Marek February 16th, 2015 10:51 PM

Hi

Thank you guys for trying help me. I re-started my computer. I was able to run program. It gave me warnings , but no errors. Converting fields to properties did not help. This warning is generated only by numeric type fields, string fields work fine. I run identical program using VB and everything was fine. Thanks

Mark

Rod Stephens February 17th, 2015 11:44 AM

Strange. Did you try the workaround mmorgan30 mentioned? The main form would need to save the field values into variables and then use the variables' ToString methods.


All times are GMT -4. The time now is 05:01 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.