Class question

S

shapper

Hello,

I have various classes in a project and all them use a few common
properties.
So I created a new class, named Config, with all those properties so I
don't need to define those properties in all classes.

My problem is how to make the Config properties accessible in my other
classes.

For example, I would like to use something like:

Dim myConfig As MyNamespace.Config
myConfig.Property_A = "PropertyA"
myConfig.Property_B = "PropertyB"

Dim myClass1 As MyNamespace.Class1
Class1.RunMethod1

And myClass1 would use PropertyA and PropertyB without needing to
define them in myClass1 itself.

Any idea how to create this?

Thanks,
Miguel
 
P

Peter Bradley

Yes. If I understand you correctly.

All you have to do is to make sure that your Class1 instance has a reference
to the Config instance. You could, for instance, pass the reference in the
Class1 constructor:

If you need to access the Config instance over a wider scope, you could
store a reference to it in the Session:

Session["myconfig"] = myConfig; // or whatever you do in VB.NET

Any object can now retrieve myConfig from the Session:

Config aConfigInstance = (Config)Session["myconfig"]; // ... translated
to VB.NET

Is that what you're after?


Peter
 
S

shapper

Yes. If I understand you correctly.

All you have to do is to make sure that your Class1 instance has a reference
to the Config instance. You could, for instance, pass the reference in the
Class1 constructor:

If you need to access the Config instance over a wider scope, you could
store a reference to it in the Session:

Session["myconfig"] = myConfig; // or whatever you do in VB.NET

Any object can now retrieve myConfig from the Session:

Config aConfigInstance = (Config)Session["myconfig"]; // ... translated
to VB.NET

Is that what you're after?

Peter


I have various classes in a project and all them use a few common
properties.
So I created a new class, named Config, with all those properties so I
don't need to define those properties in all classes.
My problem is how to make the Config properties accessible in my other
classes.
For example, I would like to use something like:
Dim myConfig As MyNamespace.Config
myConfig.Property_A = "PropertyA"
myConfig.Property_B = "PropertyB"
Dim myClass1 As MyNamespace.Class1
Class1.RunMethod1
And myClass1 would use PropertyA and PropertyB without needing to
define them in myClass1 itself.
Any idea how to create this?
Thanks,
Miguel

Not really.

I suppose the way to go is to make Class1 to Inherit from class
Config. I think.
 
P

Peter Bradley

shapper said:
Yes. If I understand you correctly.

All you have to do is to make sure that your Class1 instance has a
reference
to the Config instance. You could, for instance, pass the reference in
the
Class1 constructor:

If you need to access the Config instance over a wider scope, you could
store a reference to it in the Session:

Session["myconfig"] = myConfig; // or whatever you do in VB.NET

Any object can now retrieve myConfig from the Session:

Config aConfigInstance = (Config)Session["myconfig"]; // ...
translated
to VB.NET

Is that what you're after?

Peter

Not really.

I suppose the way to go is to make Class1 to Inherit from class
Config. I think.

Only if Class1 IS-A Config. Otherwise you're breaking just about every rule
in OOP.

But if you explained why it's not really what you want, someone might be
able to help.


Peter
 
S

shapper

Yes. If I understand you correctly.
All you have to do is to make sure that your Class1 instance has a
reference
to the Config instance. You could, for instance, pass the reference in
the
Class1 constructor:
If you need to access the Config instance over a wider scope, you could
store a reference to it in the Session:
Session["myconfig"] = myConfig; // or whatever you do in VB.NET
Any object can now retrieve myConfig from the Session:
Config aConfigInstance = (Config)Session["myconfig"]; // ...
translated
to VB.NET
Is that what you're after?
Peter
Not really.
I suppose the way to go is to make Class1 to Inherit from class
Config. I think.

Only if Class1 IS-A Config. Otherwise you're breaking just about every rule
in OOP.

But if you explained why it's not really what you want, someone might be
able to help.

Peter

Hello,

Let me try to explain it better.

Basically I have a library project with 12 classes under the same
namespace.

All these classes access an SQL 2005 database and need a connection
string.

However, I want the user to define the connection string when using
these classes.

But I want the connection string to be defined only once and be used
by all classes.

Of course I could add a ConnectionString property to every class but
this does not seem the best way to do this.

Basically that's it.

What should be the best way to do this?

Thanks,

Miguel
 
M

Mike Hofer

Yes. If I understand you correctly.
All you have to do is to make sure that your Class1 instance has a
reference
to the Config instance. You could, for instance, pass the reference in
the
Class1 constructor:
If you need to access the Config instance over a wider scope, you could
store a reference to it in the Session:
Session["myconfig"] = myConfig; // or whatever you do in VB.NET
Any object can now retrieve myConfig from the Session:
Config aConfigInstance = (Config)Session["myconfig"]; // ...
translated
to VB.NET
Is that what you're after?
Peter
Not really.
I suppose the way to go is to make Class1 to Inherit from class
Config. I think.
Only if Class1 IS-A Config. Otherwise you're breaking just about every rule
in OOP.
But if you explained why it's not really what you want, someone might be
able to help.

Hello,

Let me try to explain it better.

Basically I have a library project with 12 classes under the same
namespace.

All these classes access an SQL 2005 database and need a connection
string.

However, I want the user to define the connection string when using
these classes.

But I want the connection string to be defined only once and be used
by all classes.

Of course I could add a ConnectionString property to every class but
this does not seem the best way to do this.

Basically that's it.

What should be the best way to do this?

Thanks,

Miguel- Hide quoted text -

- Show quoted text -

Miguel,

Is your application an ASP.NET application, or a Windows Forms
application? Also, which verison of .NET are you targeting? 1.0, 1.1.,
or 2.0?

The place you want to store your connection string in is your
application's configuration file. However, getting at that data
differs depending on which version of the Framework you're targeting
(and potentially the type of application you're writing).

Thanks!
Mike
 
S

shapper

Yes. If I understand you correctly.
All you have to do is to make sure that your Class1 instance has a
reference
to the Config instance. You could, for instance, pass the reference in
the
Class1 constructor:
If you need to access the Config instance over a wider scope, you could
store a reference to it in the Session:
Session["myconfig"] = myConfig; // or whatever you do in VB.NET
Any object can now retrieve myConfig from the Session:
Config aConfigInstance = (Config)Session["myconfig"]; // ...
translated
to VB.NET
Is that what you're after?
Peter
Not really.
I suppose the way to go is to make Class1 to Inherit from class
Config. I think.
Only if Class1 IS-A Config. Otherwise you're breaking just about every rule
in OOP.
But if you explained why it's not really what you want, someone might be
able to help.
Peter

Let me try to explain it better.
Basically I have a library project with 12 classes under the same
namespace.
All these classes access an SQL 2005 database and need a connection
string.
However, I want the user to define the connection string when using
these classes.
But I want the connection string to be defined only once and be used
by all classes.
Of course I could add a ConnectionString property to every class but
this does not seem the best way to do this.
Basically that's it.
What should be the best way to do this?

Miguel- Hide quoted text -
- Show quoted text -

Miguel,

Is your application an ASP.NET application, or a Windows Forms
application? Also, which verison of .NET are you targeting? 1.0, 1.1.,
or 2.0?

The place you want to store your connection string in is your
application's configuration file. However, getting at that data
differs depending on which version of the Framework you're targeting
(and potentially the type of application you're writing).

Thanks!
Mike

Hi Mike,

I am creating a class library and the target will be Asp.Net 2.0 web
sites.
So I think the options would be:
1. The user defines a connection string in its code and "indicates" my
classes to use it.
2. The user defines a connection string in its web site Web.Config
file and "indicates" the classes to use is.

Anyway, I am not sure the way to go on this.
Sure I could force the user to create a connection string named
"ClassMig" and then in my classes I would get that connection string.

But this is not the way I wanna do it.

Thanks,
Miguel
 

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