I need to pass between classes in the same namespace but unsure how

S

sparks

Ok I have a person class and a files class to output to a database.
the question I have is if person has name, and address for example
can I pass person object instead of each individual item in the class?


public class Person
{
private string Name;
private string Address;
}

a function in my files class would be something like this abreviation


public void Personrecord(Person)

INSERT INTO Personrecord (" + "Name , Address "+")
VALUES (' "+Name + " ' , ' " + Address +" ' )";


how can I pass a person instead of each variable, Name and Address?

It is hard for a nube since I am not sure "Exactly" how to ask this.

thank you for any and all help

Jerry
 
M

Morten Wennevik

Hi Jerry,

You should be able to pass Person to the database, but you need to
create a single chunk of data out of it before you do, for instance
using serializing.

http://msdn.microsoft.com/library/en-us/cpguide/html/cpovrserializingobjects.asp

You might also just create a ToString method in your Person class
that gives you a string consisting of a name and address and put
that in the database. Something like this:

public class Person
{
private string Name;
private string Address;

public override string ToString()
{
return Name + "\t" + Address;
}
}

Splitting the value in the database at the tab character will
give you name and address.
 
T

Thomas P. Skinner [MVP]

Well you can pass a class reference easily to a function, but your problem
is accessing the fields since you have made them private. You can make them
public or define properties for each.

e.g. (if they are public fields)

public void Personrecord(Person per)
{
something=per.Name; etc.
}

Thomas P. Skinner [MVP]
 
S

sparks

well I guess this is where I am confused.
yes I can use accessors to get each value but I was wondering if I could pass Person
instead of each individual item that makes up a person from my person.cs


this is what I have in my files.cs file

public void fileSaver(string Name,string Address)
{
string sCommand = " INSERT INTO tblPerson(Name, Address)" +
"VALUES (" + Name + ",'" + Address + ")";

Connection.Open();
OleDbCommand cmd = new OleDbCommand (sCommand, Connection);
command.ExecuteNonQuery();
Connection.Close();


I was wondering if I could pass a person

public void filesaver(Person)
======================

does this make since?


ps the command has me confused as well..
this does not work...am I doing something wrong? yes I am sure I am since it doesn't work
even though this is an example in the dietel book, which I am using.
 
J

Jim Hughes

Yes, you can.

Person is the type, person is the instance

public void fileSaver(Person person)
....
string sCommand = " INSERT INTO tblPerson(Name, Address)" +
"VALUES (" + person.Name + ",'" + person.Address + ")";
....

Please look into using parameterized SQL and/or stored procs to prevent sql
injection attacks.
 
T

Thomas P. Skinner [MVP]

If you are asking if you can pass a person object in an SQL statement then I
believe the answer is no. If you are asking if you can pass a person object
to a function then this has been explained and you can.

Thomas P. Skinner [MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top