using a class within a class

  • Thread starter Thread starter Rich
  • Start date Start date
R

Rich

How can I use class B within class A?

public class A
{
public string str1;
public B[] b;
}

public class B
{
public int IDnum;
}

public voic proc1()
{
A a = new A();
a.str1 = "test"
a.b[0].IDnum = 1; <---errors out here -- how to instantiate?
}


Thanks
 
Rich said:
Sorry, wrong group.

Rich said:
How can I use class B within class A?

public class A
{
public string str1;
public B[] b;
}

public class B
{
public int IDnum;
}

public voic proc1()
{
A a = new A();
a.str1 = "test"
a.b[0].IDnum = 1; <---errors out here -- how to instantiate?
}


Thanks

Well, since I haven't seen the question move to the right group, I'll
answer here.

The code works fine provided you fix the error on the line above the
line in question (missing ;), and change voic to void. Also the public
void proc1() needs to be in a class somewhere. You should always copy
the exact code rather than retyping it in a post.
 
Family said:
Rich said:
Sorry, wrong group.

Rich said:
How can I use class B within class A?

public class A
{
public string str1;
public B[] b;
}

public class B
{
public int IDnum;
}

public voic proc1()
{
A a = new A();
a.str1 = "test"
a.b[0].IDnum = 1; <---errors out here -- how to instantiate?
}


Thanks

Well, since I haven't seen the question move to the right group, I'll
answer here.

The code works fine provided you fix the error on the line above the
line in question (missing ;), and change voic to void. Also the public
void proc1() needs to be in a class somewhere. You should always copy
the exact code rather than retyping it in a post.

Depending on your viewer, the last post should have said you were
missing a semicolon after the line above your error line. I ended up
seeing a winky face in my viewer, which was not what I meant...
 
thanks for the reply, but I am still getting an "object reference not set to
instance of an object" error

public void testProc()
{
A a = new A();
a.str1 = "test";
a.b[0].IDnum = 123; <-- errors out here
Console.WriteLine("test {0}: {1}", a.str1, a.b[0].IDnum);
}

I am missing something.

Family Tree Mike said:
Family said:
Rich said:
Sorry, wrong group.

:

How can I use class B within class A?

public class A
{
public string str1;
public B[] b;
}

public class B
{
public int IDnum;
}

public void proc1()
{
A a = new A();
a.str1 = "test"
a.b[0].IDnum = 1; <---errors out here -- how to instantiate?
}


Thanks

Well, since I haven't seen the question move to the right group, I'll
answer here.

The code works fine provided you fix the error on the line above the
line in question (missing ;), and change voic to void. Also the public
void proc1() needs to be in a class somewhere. You should always copy
the exact code rather than retyping it in a post.

Depending on your viewer, the last post should have said you were
missing a semicolon after the line above your error line. I ended up
seeing a winky face in my viewer, which was not what I meant...
 
Rich said:
thanks for the reply, but I am still getting an "object reference not set to
instance of an object" error

Sorry, I 1) didn't realize you meant a runtime error, versus a compile
error, and 2) I would move this to .net.languages.csharp, but don't know
how...

Your code never allocates the array in class A. Change the code in
class A to:

public B[] b = new B [5]; // where 5 is some number you feel is correct.
 
thank you again for getting back to me on this.

public void testProc()
{
A a = new A();
a.str1 = "test";
B[] b = new B[5];
a.b[0].IDnum = 123; <-- still getting same error here - not instantiated
Console.WriteLine("test {0}: {1}", a.str1, a.b[0].IDnum);
}

I guess I should move this question over to the C# group (which is what I
meant to do originally). Thank you for the input, however.

Family Tree Mike said:
Rich said:
thanks for the reply, but I am still getting an "object reference not set to
instance of an object" error

Sorry, I 1) didn't realize you meant a runtime error, versus a compile
error, and 2) I would move this to .net.languages.csharp, but don't know
how...

Your code never allocates the array in class A. Change the code in
class A to:

public B[] b = new B [5]; // where 5 is some number you feel is correct.
 
How can I use class B within class A?

public class A
{
public string str1;

public B[] b = {new B(), new B(), new B(), new B()};


You have to initialize the array with new instances.
 
Thanks. That did the trick - for compilation.



Tom Shelton said:
How can I use class B within class A?

public class A
{
public string str1;

public B[] b = {new B(), new B(), new B(), new B()};


You have to initialize the array with new instances.
 
Back
Top