Click here to Skip to main content
15,887,214 members
Home / Discussions / C#
   

C#

 
GeneralRe: asp.net enterprise application Pin
Member 882441721-Sep-13 8:30
Member 882441721-Sep-13 8:30 
GeneralRe: asp.net enterprise application Pin
Dave Kreskowiak21-Sep-13 12:10
mveDave Kreskowiak21-Sep-13 12:10 
QuestionOPOS driver Pin
kropek19-Sep-13 2:02
kropek19-Sep-13 2:02 
AnswerRe: OPOS driver Pin
kropek19-Sep-13 2:02
kropek19-Sep-13 2:02 
AnswerRe: OPOS driver Pin
OriginalGriff19-Sep-13 3:46
mveOriginalGriff19-Sep-13 3:46 
QuestionAccessing properties of one property of a class, directly Pin
H.Goli19-Sep-13 1:15
H.Goli19-Sep-13 1:15 
AnswerRe: Accessing properties of one property of a class, directly Pin
Eddy Vluggen19-Sep-13 2:59
professionalEddy Vluggen19-Sep-13 2:59 
AnswerRe: Accessing properties of one property of a class, directly Pin
BillWoodruff19-Sep-13 3:30
professionalBillWoodruff19-Sep-13 3:30 
Several things in your code "jump out at me" as kind of ... confusing:

1. you made the constructor of MyClass private: so you cannot create a new instance of MyClass. Since MyClass is not a static class, the dance will never begin.

2. so, changing the access modifier to MyClass' ctor to 'public, and creating a new instance of 'MyClass:

a. you have not created any instances of the Item class simply by creating an instance of 'MyClass ... there's nothing inherently wrong with that.

b. the Items collection in _items will be null ... nothing wrong with that.

3. MyClass does have a 'CurrentItem property which will contain null when instances of MyClass are created.

a. but when you access 'CurrentItem, which only implements a 'get: if your internal List<Item> is null: it will create a new Item and return it, but none of that instance of Item Class has its internal fields initialized.

4. the only other means by which I see in your code creates instances of the 'Item class is provided by code you do not show in your question here, via a call to what looks like an extension method on the DataTable Object: _items = _mainDataTable.ConvertToListOfItems<Item>

Since multiple instances of the 'Item class are, obviously going to be created: with the way you have your code structured now, you can only access the Properties of the last created instance of 'Item via the 'CurrentItem property, or by selecting an instance of 'Item in your List<Item> stored in _items.

So to answer your question, specifically, yes, you can execute int id=obj.CurrentItem.Id; as you show, just after you create an instance of 'MyClass (if you change the ctor to 'public), and id will be set to #0, because your invocation of 'CurrentItem caused a new instance of 'Item to be created, and an integer variable will have a default value of #0 when an instance of a class is created.

Big picture: it looks to me like your code needs some strategic re-thinking, about which I can't comment since I don't understand your exact goals here.

As the code is now (once the ctor of 'MyClass is set to public access) I can create an instance of the 'Item class without creating an instance of 'MyClass: do you really want to allow that to happen ?
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

QuestionParse complex XML in C# Pin
Member 1026417718-Sep-13 21:45
Member 1026417718-Sep-13 21:45 
AnswerRe: Parse complex XML in C# Pin
Pete O'Hanlon18-Sep-13 22:17
mvePete O'Hanlon18-Sep-13 22:17 
GeneralRe: Parse complex XML in C# Pin
Member 1026417718-Sep-13 22:30
Member 1026417718-Sep-13 22:30 
GeneralRe: Parse complex XML in C# Pin
Pete O'Hanlon18-Sep-13 22:41
mvePete O'Hanlon18-Sep-13 22:41 
GeneralRe: Parse complex XML in C# Pin
Member 1026417718-Sep-13 23:06
Member 1026417718-Sep-13 23:06 
GeneralRe: Parse complex XML in C# Pin
Nicholas Marty18-Sep-13 23:15
professionalNicholas Marty18-Sep-13 23:15 
GeneralRe: Parse complex XML in C# Pin
Pete O'Hanlon18-Sep-13 23:29
mvePete O'Hanlon18-Sep-13 23:29 
GeneralRe: Parse complex XML in C# Pin
Member 1026417718-Sep-13 23:53
Member 1026417718-Sep-13 23:53 
GeneralRe: Parse complex XML in C# Pin
V.19-Sep-13 1:02
professionalV.19-Sep-13 1:02 
AnswerRe: Parse complex XML in C# Pin
Simon_Whale18-Sep-13 23:30
Simon_Whale18-Sep-13 23:30 
AnswerRe: Parse complex XML in C# Pin
jschell19-Sep-13 11:22
jschell19-Sep-13 11:22 
AnswerRe: Parse complex XML in C# Pin
Alan Balkany23-Sep-13 5:28
Alan Balkany23-Sep-13 5:28 
QuestionShow Multiparty Images on one form Pin
sanket16418-Sep-13 20:24
sanket16418-Sep-13 20:24 
AnswerRe: Show Multiparty Images on one form Pin
Paw Jershauge18-Sep-13 20:52
Paw Jershauge18-Sep-13 20:52 
AnswerRe: Show Multiparty Images on one form Pin
Pete O'Hanlon18-Sep-13 21:11
mvePete O'Hanlon18-Sep-13 21:11 
QuestionDB2 Connection for Visual Studio Express 2010 Pin
Chris-Za18-Sep-13 2:18
Chris-Za18-Sep-13 2:18 
SuggestionRe: DB2 Connection for Visual Studio Express 2010 Pin
Richard MacCutchan18-Sep-13 2:48
mveRichard MacCutchan18-Sep-13 2:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.