Article View: borland.public.jbuilder.java.language
Article #19099Re: Problem with member visibility/packages
From: Conrad Hoesle-Kienzlen
Date: Sat, 14 Mar 1998 00:00
Date: Sat, 14 Mar 1998 00:00
234 lines
6570 bytes
6570 bytes
Paul,
I've just looked at that code again. Told you something wrong in my last
reply: you don't need to put the type in front of your variables in method
getTxtFields(),
I overlooked that you had declared them already. The other things hold
true, however.
There are a few other things:
Since BusLogicDataClass seems to be intended as a "helper" class for
UIDataClass, there is no reason for the line:
BusLogicDataClass nameaddress = new BusLogicDataClass();
within that class itself. With that, you are instantiating an object, which
you should do only in UIDataClass.
The setTxtFields() method sets all variables to an empty string, so how do
you get the real values to those variables?
The constructor calls both getTextFields() and setTextFields, this is an
unusual thing to do in a constructor.
This is how I would write BusLogicDataClass:
package Mysource;
import java.io.*;
import java.util.*;
public class BusLogicDataClass { // class to store data
private String name;
private String address1;
private String address2;
private String address3;
private String age;
private String mortgage;
// constructor with data
public BusLogicDataClass(String na, String ad1, String ad2, String ad3,
String ag, String mo) {
name = na;
address1 = ad1;
address2 = ad2;
address3 = ad3;
age = ag;
mortgage = mo
} // end of constructor with data
// constructor without data
public BusLogicDataClass() {
super(); // just calls the default constructor without setting any of
the strings
}
public String[] getTxtFields() {
String str[] = new String[6]; // allocate array of 6 strings
str[0] = name;
str[1] = address1;
str[2] = address2;
str[3] = address3;
str[4] = age;
str[5] = mortgage;
return str;
} // another way would be a delimited string
str=name+"/"+address1+"/"....
public void setTxtFields(String str[]) { // or do it like in the
constructor
name = str[0];
address1 = str[1];
...etc
}
} // end of class BusLogicTestClass
It is generally a bad idea to initialize strings with "" if you afterwards
assign something longer, since this will cause Java to create a new string
and discard the old one, which is unnecessary overhead.
Within UIDataClass you would then use it like this:
BusLogicDataClass nameaddr = new BusLogicDataClass(); // empty
constructor
...
nameaddr.setTxtFields(xxx); // where xxx is a string array with the
values
Or:
BusLogicDataClass nameaddr = new BusLogicDataClass("Hugo", "Astreet",
"Bappart","CCity", "42", "10.000");
BTW: couldn't you have come up with shorter names <g>?
...Conrad
Paul Richards wrote:
> I'm struggling with visibility and access and would welcome some help.
> I have a directory \Mysource containing UIDataClass.java and
> BusLogicDataClass.java, and have added 'package Mysource' to each file,
> and import statements for each class.
>
> When I try to compile BusLogicDataClass.java I get a list of errors
> like so:
>
> "Undefined variable or classname: txtfldName?"
>
> for each of the textfields in UIDataClass.java.
>
> When I try to compile UIDataClass.java I get the following:
>
> ".\Mysource\BusLogicDataClass.java: Undefined variable or classname:
> txtfldName?
> name = txtfldName.getText()?;
> ^?"
> for each textfield.
>
> It's probably something very basic which I'm missing - can anyone spot
> the wood for the trees? (summary code below)
>
> TIA
>
> ++ *Paul*
> ++ from Cheltenham, England
> ++ Fri, 13 Mar 1998 18:44 GMT
> ++ WinNT build 1381 + SP3
> ++ http://freespace.virgin.net/paul.richards/
> ......
>
> /*
> * UIDataClass.java
> */
>
> package Mysource;
>
> import java.awt.*;
>
> import Mysource.*;
>
> public class UIDataClass extends Frame {
> public UIDataClass() {
> super();
> setLayout(null);
> addNotify();
> Insets insets = insets();
> // .....
> // set up UI buttons, labels, textfields ...
> // ....
> show();
> }
>
> public boolean handleEvent(Event evt) {
> // <snip>
> }
>
> // Event handlers
> <snip>
>
>
> // End of event handlers
>
> public static void main(String args[]) {
> new UIDataClass();
> BusLogicDataClass BLDC = new BusLogicDataClass();
> }
>
> Label label1;
> Label label2;
> TextField txtfldName;
> Label label3;
> TextField txtfldAddr1;
> Label label4;
> TextField txtfldAddr2;
> Label label5;
> TextField txtfldAddr3;
> Label label6;
> TextField txtfldAge;
> Label label7;
> TextField txtfldMortgage;
> Button btnAdd;
> Button btnView;
> Button btnExit;
>
> // Other methods/variables:
>
> }
>
> /**
> * BusLogicDataClass.java
> */
>
> package Mysource;
>
> import Mysource.*;
> import java.io.*;
> import java.util.*;
>
> public class BusLogicDataClass // class to store data
> {
> private String name;
> private String address1;
> private String address2;
> private String address3;
> private String age;
> private String mortgage;
>
> <snip>
> {
> BusLogicDataClass nameaddress = new BusLogicDataClass();
> }
>
> public BusLogicDataClass() // constructor
> {
> getTxtFields();
> setTxtFields();
> } // end constructor
>
> void getTxtFields()
> {
> name = txtfldName.getText();
> address2 = txtfldAddr1.getText();
> address2 = txtfldAddr2.getText();
> address3 = txtfldAddr3.getText();
> age = txtfldAge.getText();
> mortgage = txtfldMortgage.getText();
> }
>
> void setTxtFields()
> {
> txtfldName.setText("");
> txtfldAddr1.setText("");
> txtfldAddr2.setText("");
> txtfldAddr3.setText("");
> txtfldAge.setText("");
> txtfldMortgage.setText("");
> }
>
> } // end class BusLogicDataClass
Technical Details
Message-ID:
<350AA744.A4E9A9E6@csi.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<VA.0000002a.0c5a12e6@p5>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,ea213ab83e1f43a0 X-Google-Attributes: gidf74b3,public From: Conrad Hoesle-Kienzlen <Conrad_Hoesle@csi.com> Subject: Re: Problem with member visibility/packages Date: 1998/03/14 Message-ID: <350AA744.A4E9A9E6@csi.com> X-Deja-AN: 334017607 Content-Transfer-Encoding: 7bit References: <VA.0000002a.0c5a12e6@p5> Content-Type: text/plain; charset=us-ascii Organization: CHK IT-Consulting Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language