Parameter passsing between classes
-
Hi guy this is so lame i cant pass parameters between classes, i feel like commiting sucide :o( anyways I have used a public variable in say class A Public: long iSelPackageItemID; and assigned the value in function... setPackageID() { iSelPackageItemID=some value; } then i have declared a function long getPackageID() ( return iSelPackageItemID; ) after a long long time i call the function getPackageID() from some another class say B and i am getting garbage value i.e. getPackageID() is returning garbage. Whatever is the solution i just want the value from iSelPackageItemID in class B which is assigned in class a.
-
Hi guy this is so lame i cant pass parameters between classes, i feel like commiting sucide :o( anyways I have used a public variable in say class A Public: long iSelPackageItemID; and assigned the value in function... setPackageID() { iSelPackageItemID=some value; } then i have declared a function long getPackageID() ( return iSelPackageItemID; ) after a long long time i call the function getPackageID() from some another class say B and i am getting garbage value i.e. getPackageID() is returning garbage. Whatever is the solution i just want the value from iSelPackageItemID in class B which is assigned in class a.
Maybe you have casting problem. post the code when you call setPackageID from your B class (before taking any action for suicide :))
-
Maybe you have casting problem. post the code when you call setPackageID from your B class (before taking any action for suicide :))
No casting since all the variables are long. I checked in the debug mode that the value is retained in the class as along the variable is in scope, but garbage value is assigned once the value goes beyond the scope. I guess i need to increase the scope of variable to application variable.. How do i do it...?
-
Hi guy this is so lame i cant pass parameters between classes, i feel like commiting sucide :o( anyways I have used a public variable in say class A Public: long iSelPackageItemID; and assigned the value in function... setPackageID() { iSelPackageItemID=some value; } then i have declared a function long getPackageID() ( return iSelPackageItemID; ) after a long long time i call the function getPackageID() from some another class say B and i am getting garbage value i.e. getPackageID() is returning garbage. Whatever is the solution i just want the value from iSelPackageItemID in class B which is assigned in class a.
Well i solved the problem time being. by declaring extern long iSelPackageItemID in the .h file and long iSelPackageItemID in .cpp file i got help from here http://www.codeguru.com/forum/showthread.php?threadid=427587 But the core OOP funda remains unanswered, i know this is going beyond the scope but just for knowledge sake. I am also working in java and i haven't seen such kind of syntax, also in a desingning book it stated not to use a global variable since it voilates the basic rules of OOP. So is the above code a good programming practise...?
-
Hi guy this is so lame i cant pass parameters between classes, i feel like commiting sucide :o( anyways I have used a public variable in say class A Public: long iSelPackageItemID; and assigned the value in function... setPackageID() { iSelPackageItemID=some value; } then i have declared a function long getPackageID() ( return iSelPackageItemID; ) after a long long time i call the function getPackageID() from some another class say B and i am getting garbage value i.e. getPackageID() is returning garbage. Whatever is the solution i just want the value from iSelPackageItemID in class B which is assigned in class a.
-
How are you creating "some value"? The first thing that comes to mind is that it is a variable that has gone out of scope between when you assign it and when you try to reference it. Judy
I am not clear on this thing ... but now it looks like pretty much that it's going out of reference. My OOP fundas are weak... but just one question when i assign a value to a class member A, then i declare an obj of class B so when i try to access the member from a through B does it go out of scope..?
-
No casting since all the variables are long. I checked in the debug mode that the value is retained in the class as along the variable is in scope, but garbage value is assigned once the value goes beyond the scope. I guess i need to increase the scope of variable to application variable.. How do i do it...?
I dont know how exactly u had written the class, I assumed some and written like this..
class A { public: long iSelPackageItemID; A() { iSelPackageItemID=100; } //and assigned the value in function... setPackageID() { iSelPackageItemID=200; } //then i have declared a function long getPackageID() { return iSelPackageItemID; } }; class B { public: A ob1; }; int main() { B ob2; A ob; cout< if ur code is similar to this than u got the solution. If this the right solution that ur problem is, u had given some valsue to iSelPackageItemID in the setPackageID() method, but when u create the onject to class A or B, ur members functions will not call and the valuse of setPackageID is not intialised, so give valuse to setPackageID either in constructor or call the setPackageID() function before calling getPackageID()<< method.
-
I am not clear on this thing ... but now it looks like pretty much that it's going out of reference. My OOP fundas are weak... but just one question when i assign a value to a class member A, then i declare an obj of class B so when i try to access the member from a through B does it go out of scope..?
I'm not saying that the member itself of class A goes out of scope, I'm suggesting that what was assigned to the member went out of scope. The member itself is still good but it is referncing memory that is no longer good .You see this a lot with objects created on the stack. For example: char *MyCreateFunc () { char buffer[256] // do all the stuff to fill in buffer return buffer; } The statement A::m_memberVar = MyCreateFunc (); will compile and execute and may even work as expected sometimes. However, it is wrong!! A::m_memberVar remains in scope but what it points to is no longer in scope because the memory containing the data is no longer valid once the MyCreateFunc returns. Show the code where you create the value assigned to the member of class A. The member variables of a class remains in scope until the class itself goes out of scope, so unless you're accessing class A incorrectly, the problem has got to be in the value assigned to the member variable. Judy