Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Institate a Class

Status
Not open for further replies.

mchoudhury

Programmer
Joined
Nov 26, 2004
Messages
71
Location
GB
Hi guys,
My vb.net class was getting longer so i decided to create another public class and carry on coding. From my 1st class iam claaing a function of the created class.
in the first class iam inisitating new new class:
Protected TM As TelephoneInputMinutes
Protected TC As TelephoneInputCalls
and values are being added to object of these classes as i go along the activity process.

In my new class thats been created i'm trying to retrive some of the value being added to carry on the process of the activity of that page.
But whats happening if i dont inisiatate these class again, i get an error of:
Object reference not set to an instance of an object

and if inisiatete them as
Protected TM As TelephoneInputMinutes
Protected TC As TelephoneInputCalls
to the new class then evrything goes back to 0 therefore i cannot continue from where it was left.
I need to retrive thye values of TM and TC that was inputedted on the first calss back to the second class. If i put the function in tyhe first class i done have any problems.

Does anyone knows what can be done

Function = getCostPerMonth from 2nd class CostPerMonth
1st calss name = TelephoneUsage
Thanks
 
You need to set up a link between the two classes to hold the state of TM and TC.

This can be done with properties.

Private m_oTM As TelephoneInputMinutes
Public Property TM() As TelephoneInputMinutes
Get
TM = m_oTM
End Get
Set(ByVal Value As TelephoneInputMinutes)
m_oTM = Value
End Set
End Property

Private m_oTC As TelephoneInputCalls
Public Property TC() As TelephoneInputCalls
Get
TC = m_oTC
End Get
Set(ByVal Value As TelephoneInputCalls)
m_oTC = Value
End Set
End Property

This is a bit of extra work though to create links between classes referencing other classes. Is you code that long that you must split the original class?

the code your using...

Protected TM As TelephoneInputMinutes

creates a brand new class
 
sorry perhaps i did not make this clearer.
In my first class (telephoneusage.vb) i have
Protected TM As new TelephoneInputMinutes()
Protected TC As new TelephoneInputCalls()

on that web form i have textboxes for inserting values, these values get inserteed into TM and TC database as new fields. Because the code on my 1st class was getting messy and longer i decided to carryon the programme by creating anothyer class.

Now i need to retrive the values of TM and TC fields from the database that were inserted through the textboxes.

in the 2nd class if i add:
Protected TM As new TelephoneInputMinutes()
Protected TC As new TelephoneInputCalls()
then this adds another filds to the DB and i cannot update anything of the previous fileds. and if i dont add these two lines then i get object not initated error.
how can i call the values that were inserted in table TM and TC in the first clss to the second class.

hope this is clearer. i already have this created:
Public Property LocalDaytime() As Single
Get
Return m_LocalDaytime
End Get
Set(ByVal Value As Single)
m_LocalDaytime = Value
End Set
End Property

Public Property LocalEvening() As Single
Get
Return m_LocalEvening
End Get
Set(ByVal Value As Single)
m_LocalEvening = Value
End Set
End Property



 
In that case, if I have understood the problem correctly, you need to allow Class2 to access the state of TM and TC in Class1.

There may be another way of doing this but here's one solution.

In Class2 add a property:

Private m_oClass1 as Class1
Public clsClass1() As Class1
Get
clsClass1= m_oClass1
End Get
Set(ByVal Value As Class1)
m_oClass1 = Value
End Set
End Property


When you create Class2 make sure it's modular and in New pass Class1 into the property


private m_oClass2 as new Class2

sub New
m_oclass2.clsClass1=me
end sub

Now you have a reference back from Class2 to Class1 and can access TM and TC as long as they are modular also

eg in Class2

private function SomeFunction() as boolean
me.clsClass1.TM.doTMfunction

x=me.clsClass1.Y
end function


 
Found the answere it working perfectly - took minimal coding:

In Class2 the function was written like this:
Public Function getCostPerMonth(ByVal TC As TelephoneInputCalls, ByVal TM As TelephoneInputMinutes)

in class1 to retrive the function inordr to run:
Protected CPM As New CostPerMonth()
CPM.getCostPerMonth(TC, TM)

Thanks for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top