pjzuber

I have a question regarding inherited arrays. In my program I have an array of strings(protected) in my parent class. In a child class, while i have access to the array variable, all of the data in the array is missing. Is this normal procedure for c# What's strange is that while the data is missing when calling the variable directly, if i pass the variable from the parent class to the child class via a method, the data is retained. Any help would be greatly appreciated.

~Patrick



Re: Visual C# General Inherited Data Members

Peter Ritchie

When a class inherits a member it inherits access to its value. The only way to get the symptoms you've described is to re-declare a member in the derived class, hiding the base class members.

If you provide a reproducible sample of your problem, I can point out exactly what to change.






Re: Visual C# General Inherited Data Members

pjzuber

This is the child, the parent, HL7, has the string array Msh, all i am doing in the parent class is creating a new TextBoxDisplay object and then calling the displayText method and storing it in a string variable. Through MessageBox.Show() i have determined that the if statements are all working correctly and that the Msh[] array has no items in it as soon as it eneters the class

//Child

Code Snippet

public class TextBoxDisplay : AllinOne.HL7

{

private String text;

public TextBoxDisplay(){

text = "hello";

}

public String displayText(ListBox segmentOrder){

//MSH items selected

if (segmentOrder.Text == "Sending Application")

{

text = Msh[0];

}

if (segmentOrder.Text == "Sending Facility")

{

text = Msh[1];

}

if (segmentOrder.Text == "Receiving Application")

{

text = Msh[2];

}

if (segmentOrder.Text == "Receiving Facility")

{

text = Msh[3];

}

if (segmentOrder.Text == "Time of Message")

{

text = Msh[4];

}

if (segmentOrder.Text == "Security")

{

text = Msh[5];

}

if (segmentOrder.Text == "Message Type")

{

text = Msh[6];

}

if (segmentOrder.Text == "Message Control ID")

{

text = Msh[7];

}

if (segmentOrder.Text == "Processing ID")

{

text = Msh[8];

}

if (segmentOrder.Text == "Version ID")

{

text = Msh[9];

}

if (segmentOrder.Text == "Sequence Number")

{

text = Msh[10];

}

if (segmentOrder.Text == "Continuation Pointer")

{

text = Msh[11];

}

if (segmentOrder.Text == "Accept Acknowledgement")

{

text = Msh[12];

}

if (segmentOrder.Text == "Application Acknowledgement")

{

text = Msh[13];

}

if (segmentOrder.Text == "Country Code")

{

text = Msh[14];

}

return text;

}

}

//Parent

//CurrentText is a text box

//SegmentOrderBox is my listBox

Code Snippet

TextBoxDisplay box = new TextBoxDisplay();

CurrentText.Text = box.displayText(SegmentOrderBox);





Re: Visual C# General Inherited Data Members

Peter Ritchie

Without a reproducible sample of your problem, I can only guess at what the problem is. Is LH7.Msh a field or a property




Re: Visual C# General Inherited Data Members

pjzuber

not completely sure what your asking, i have it declared at the beginning of my parent class like this:

protected String[] Msh = new String[18];

i've always just called it a data member





Re: Visual C# General Inherited Data Members

Peter Ritchie

pjzuber wrote:

not completely sure what your asking, i have it declared at the beginning of my parent class like this:

protected String[] Msh = new String[18];

i've always just called it a data member

this works fine:

Code Snippet

class BaseClass

{

protected String[] values = new String[18];

public BaseClass()

{

values[0] = "one";

values[1] = "two";

values[2] = "three";

values[3] = "four";

//...

values[17] = "eighteen";

}

}

class DerivedClass : BaseClass

{

public void Method()

{

Trace.WriteLine(values[0]);

}

}

DerivedClass c = new DerivedClass();

c.Method();

By "reproducible sample of your problem" I mean everything you've given so far provides nothing to reproduce what you're saying you're experiencing. Nothing about simply inheriting a protected array causes the problems you're describing (see above code for proof); you need to provide an entire sample that reproduces the problem before we can help you with it. See http://www.yoda.arachsys.com/csharp/complete.html for more detail on reproducible samples.






Re: Visual C# General Inherited Data Members

mwalts

That is also called a field, properties are the ones with the get and set sections build in, just so you know.

So, you create the child class from the parent class That isn't normally a good idea, and when you do it will create a new instance of the parent object as well, in the background. In fact, if you do this in the default constructor it will likely cause a stack overflow.

I really think that there might be a better way to do what you are trying to do here. If you don't setup the Msh strings in the parent's constructor, they might not have the values you expect in the child. If the parent changes the Msh strings, that is even more true. You could try making the Msh strings static so

protected static String[] Msh = new String[18];

But I really think if you post what your trying to do, we can find a more elegant way of doing it.

To understand what I meant about the constructors, this might help
http://www.yoda.arachsys.com/csharp/constructors.html

Good luck,

-mwalts




Re: Visual C# General Inherited Data Members

pjzuber

Thnx mwalts, all i needed was to make them static members, Thanks for your help.