473,395 Members | 1,496 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,395 software developers and data experts.

making Array of objects of a class

Hello All
The question might be somewhat stupid but I have tried for an hour and could
not get through it.

I am creating a console based application just to begin with C#

following is the code
namespace Student1

{

class ABC

{

private

string strname;

public void GetData()

{

Console.WriteLine("Enter your Name");

strname = Console.ReadLine();

}

public void Print()

{

Console.WriteLine(strname);

}

};

class Class1

{

[STAThread]

static void Main(string[] args)

{
ABC[] arr = new ABC[6];

arr[0].GetData(); // This Line gives exception?

arr[0].Print();
}

}

}

according to me it should display a string to get data then print it but the
an exception occurs.System.NullRefrenceException

the Message is "Object reference not set to an instance of an object."

best regards

Aftab
Nov 16 '05 #1
2 3203
Muhammad Aftab Alam wrote:
ABC[] arr = new ABC[6];


All you have is array capable of holding ABC objects, not an array of
actual ABC objects.

You'll have to initialize the array manually.

--
There are 10 kinds of people: those who understand binary and those who
don't.
Nov 16 '05 #2

try to

using System;
using System.Collections;

namespace Student1

{

class ABC

{

private

string strname;

public void GetData()

{

Console.WriteLine("Enter your Name");

strname = Console.ReadLine();

}

public void Print()

{

Console.WriteLine(strname);

}

};

class Class1

{

[STAThread]

static void Main(string[] args)

{
ABC[] arr = new ABC[6];

for(int i=0;i<arr.Length;i++)
{
arr[i] = new ABC();
}

arr[0].GetData(); // This Line gives exception?

arr[0].Print();
}

}

}
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Phil... | last post by:
I am trying to figure out how to create an array that contains objects and not references to objects. I know how to do the latter, but not the former. Any help is appreciated. I have the...
3
by: SimonH | last post by:
Hi all, I would like to make a generic set of methods that could be called regardless of the database behind the scenes. One of the methods I would like would take a string sql statement and...
13
by: Chris Mantoulidis | last post by:
There must be some tips to make a program quicker. I guess more than 50% of ppl here will say "avoid the if-s". Yeah I know this makes a program quicker but some times an "if" is inevitable,...
2
by: James | last post by:
Hi, I'm hoping someone can help me out. If I declare a class, eg. class CSomeclass { public: var/func etc..... private varfunc etc..
2
by: Patrick | last post by:
Hi I have a problem in my C++ Funktion. I needed an 2 dimensional Array. For this i took a template script in order to create the array in the funktion. The Size of the Array I have to calculate...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
12
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.