473,323 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

C# class variable encapsulation problem?

Expand|Select|Wrap|Line Numbers
  1. public class BUS
  2. {
  3. private int m_variable;
  4. public int variable
  5. get { return m_variable; }
  6. set { m_variable= value; } 
  7. }
  8. }
  9.  
  10. public static void Main()
  11. {
  12. BUS[] brand= new BUS[1000];
  13. for (int i=0; i<1000; i++)
  14. {
  15. brand[i].variable = 1000;
  16. Console.WriteLine(brand[i].variable);
  17. }
  18. }
I encapsulates the code in C# 2005 like this, what;s wrong the codes above? I can't run the program. Error is "An unhandled exception of type 'System.NullReferenceException' occurred in ConsoleApplication1.exe". What's wrong with this line "brand[i].variable = 1000;"? I tried to assign the variables there.
Mar 12 '09 #1
2 2619
tlhintoq
3,525 Expert 2GB
Place a breakpoint on line 13.
Step through the code one line at a time (F-10)
Have the Autos window open.
You should see exactly which variables are null.
You can even look down at the values of individual elements of your array.

Mar 12 '09 #2
artov
40
The line 16 refers to four named things: :

1. integer "i"
2. array "brand"
3. BUS "band[i]"
4. property "variable"

You set the i on line 14 in for -statement.
You set the brand on line 13.
You set the brand[i], ..., oh, you don't !!

So why do you thing the problem is on property variable?

Add line 15.5 : brand[i] = new BUS();
Mar 13 '09 #3

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

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
15
by: Tim Henderson | last post by:
Hi i have some errors in my code that i can't find the reason for here is the error i have 3 classes: -Song -Album -Artist Song has 3 pieces of data with getter and setter meathods: -Name...
7
by: Ben | last post by:
Hi all, I'm not yet good at thinking the right way in c++ so although I could solve this problem, I'm not sure if they way I'm thinking of is the best way to do it. I need a data type or class...
7
by: Snyke | last post by:
Well my problem is simple: I have a class which has a variable number of members. Lets say I have a class Family and a variable number of FamilyMembers. Until now I used a member of Family called...
4
by: Al Murphy | last post by:
I have a windows application called "WindowsApplication1". I have a variable of tyoe DataSet called myDataSet as shown below: namespace WindowsApplication1 { public class Form1 :...
9
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value...
9
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't...
9
by: Brian Henry | last post by:
If i inherite a queue class into my class, and do an override of the enqueue member, how would i then go about actually doing an enqueue of an item? I am a little confused on this one... does over...
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
4
by: Donos | last post by:
Hi I have a HANDLE to an Event, like this.. HANDLE h = ::CreateEvent(NULL, FALSE, FALSE, NULL); This is running in one thread in one class. For example we will call that class as "Class A"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.