Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a general class that I use for database connectivity... I think I got this right but I want to make sure before I continue on. In c# when you have in one class:

 

int i = 45;

 

and then later in down the line you open a second from that also has a variable that needs the same value as i, so you make it public or internal you have:

 

FormX x = new FormX();

x.i = this.i;

 

Now both are equal to 45, but if on my main class I change i to 14, i is now also going to be 14 in the FormX because C# sets by referance right... it's not a copy of i right?

 

Getting back to the database I want to make sure that a global database class I'm passing around doesn't effect things in one particular form... so for that form I should go:

 

FormView fv = new FormView();

fv.DataBaseClass = new DataBaseClass(this.databaseclass.connection); //so it has the same connection string

 

By using the new keyword I'm in essense having two seperate, independent connections to my database what I do to one will have no effect on the other right?

  • *Experts*
Posted

I discussed some of the points you ask in this thread.

 

In your first sample, "x.i = this.i;" will put a copy of this.i into x.i. Changing x.i after the assignment will only change the copy. This is because an int is a value type and is always copied.

 

In your second example, if the DataBaseClass object takes that connection string that's passed in and creates another connection, then it would have it's own separate connection. Closing it, starting/commiting a transaction, are all done on that connection. Connections are classes so they're reference types - assigning a variable of that type to another variable only copies the pointer (both variables end up pointing to the same thing).

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Connections are classes so they're reference types - assigning a variable of that type to another variable only copies the pointer (both variables end up pointing to the same thing).

 

-ner

Okay, so I think I was on the right track, but to make sure... my custom database class sets up the dataset, data adapters, etc... so to keep the part of my program that is retrieving the data from the part of my program that is viewing the data I should use the new keyword so that I seperate class is made, not a pointer to the original.

 

To get more into that class and this program, the database is about 2.4 mbs... it has 21 differant tables all with relationships. The part of the program that is collecting data changes a parameter in it's select statement so that the dataset for a certain table carries relevant data as it updates the database... the other 1800 records in that table or the others would be a waste of memory.... as it goes along, so a couple of the tables in the dataset are constantly being updated with differant portions of the actual database. Now in the other part of the program I'm viewing data... so if that part of the program needs to view data for a table that relates to a row we call x and the other part of the program that is collecting the information needs the portion of that same table we call y and one changes the other I'm going to get all sorts of smoke coming out of the machine... concurrency errors, no valid row in a related table, etc... so this is why I need two seperate and working independantly connections to the actuall database, thus I figue the new keyword for my class should do that, just the connection string is of value type, thus a copy would be passed to make the connection string the same and that won't be a problem. Okay, now I talked it out to myself and it sounds right, let me know Ner if I've gone off the beaten trail (again).

 

Thanks!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...