I How to return and use a value from one method call to another  
Author Message
NoEgo





PostPosted: Visual C# General, I How to return and use a value from one method call to another Top

Here is a rough scratch of my code which is much longer. Just focus on my question here, do not focus on correctness.

Question/Problem: how can I pass the JAAlbum.ProductID to me second call to AddNewProduct for "Track" so that I can use its value in the call to  AddNewProductRelationship_Album_Track

Right now, when I call AddNewProduct(xl, row, host, fileExtWAV, "Track", filePathSourceWAV, fileName, moveFile, sstore), the previous JAA.ProductID is lost but I need it !

foreach loop here
{

          if ((String.Compare(lastAlbumTitle, currentAlbumTitle,true) == 0) || (firstAlbum == 1))
          {
               //First iteration of loop, This is called
               AddNewProduct(xl, row, host, fileExtWAV, "Album", filePathSourceWAV, fileName, moveFile, sstore);
          }
          else
          {
               // Second iteration through loop, then this is called
               AddNewProduct(xl, row, host, fileExtWAV, "Track", filePathSourceWAV, fileName, moveFile, sstore);     
          }

}


private static void AddNewProduct(ExcelWrapper     xl, int row, IWorkerDriver host, string FileExtension, string ProductType, string FilePath, string FileName, bool moveFile, int sstore)
{
     bunch of code here, then....< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     Product JAAlbum = null;

     Product JATrack = null;


     switch (ProductType)
     {
          case "
Album":

          JAAlbum = ProductAbstraction.Load(prAudioID, sstore, out created, TransactionAbstraction.GetTransaction());

          JAAlbum.ProductSourceID = 1;
          JAAlbum.SellerProductID = prAudioID;                                             // Sellers product id
          JAAlbum.ProductName = productName;                                             // Product Name column
          ...etc. (
Just know ProductID is already included in the JAAlbum object)

          if (created)
          {
               doing some stuff here...maybe I should return the JAAlbum.ProductID here so I can use it in my call to  AddNewProductRelationship_Album_Track
          }

     break;

     case "
Track":

          JATrack = ProductAbstraction.Load(prAudioID, sstore, out created, TransactionAbstraction.GetTransaction());

          JATrack.ProductSourceID = 1;
          JATrack.SellerProductID = prAudioID;
          JATrack.ProductName = productName;
          ...etc.
          
          if (created)
          {
               //HERE IS THE PROBLEM, I've lost JAAlbum.ProductID from the first completed call of AddNewProduct  for Album
               AddNewProductRelationship_Album_Track(
JAAlbum.ProductID, JATrack.ProductID, albumDescription); <---- PROBLEM IS HERE, says that I am passing null value for JAAlbum.ProductID

          }

     break;

     }

 

How can I maintain/retrieve the last JAAlbum.ProductID in future method calls like when I called AddNewProduct for "Track"




Visual C#17  
 
 
Mark Dawson





PostPosted: Visual C# General, I How to return and use a value from one method call to another Top

Hi,

you are calling a static method, but the local variables inside the method are not static they will not persist across function calls, so on the second call the variable will be set to null at the beginning of the function. If I was you I would have seperate methods like AddAlbum, AddTrack, you don't need the switch inside the AddNewProduct method since you already have the iff statement outside of the function call. Then AddAlbum would return the album, which you can then store in the function that is looping through all the items and pass the album into the AddTrack method.

Mark.



 
 
FavorFlave





PostPosted: Visual C# General, I How to return and use a value from one method call to another Top

Thanks Mark, then I have a few easy syntax questions on your response

Let me first humble myself horribly in the public eye...let's be honest.  Yes, I'm asking for some hand holding.  I know these are basics but I need to know how to get rid of my errors so that I can carry on at least for now and be able to use the lastProductID.  Keep in mind, I'm learning here.  And do not ask me if this is a homework assignment, I have been in IT 8 years, mostly classic ASP is the problem....got into OOP just a year or two ago

1) how do I make the variables inside static, I assume with the static keyword but please show me

2) I tried this so far (didn't really break it out like you said, I need help with that) but here's what I tried:

http://www.webfound.net/forum_posts/addproduct2.txt

Errors:

AddNewProduct(Common.Utilities.ExcelWrapper, int, Interfaces.ImportOperation.IWorkerDriver, string, string, string, string, bool, int, long)': not all code paths return a value - line 20
Unreachable code detected - line 115

 



 
 
Mark Dawson





PostPosted: Visual C# General, I How to return and use a value from one method call to another Top

Hi,

if you want to make the variables static then you just need to declar them static inside your class i.e.

class X

{

private static int myValue;

...

In regards to your compiler errors, "not all paths returns a value" means there is some way your code can execute that means you do not hit a "retux x" statement, your method returns a long, however inside the switch case for "track" you do not return any value, your code will then exit the switch statement but there is no return statement also, the function always has to return something. The unreachable code detected is telling you that you have some code that will never execute, probably because you have a return statement before some code i.e.

public void Foo()

{

return;

int i = 1 + 2; //this is unreachable and will never be executed.

}

Mark.



 
 
FavorFlave





PostPosted: Visual C# General, I How to return and use a value from one method call to another Top

ok, figured it out.  I needed to set the lastProductID in each of the stupid Case checks, then at the end, right before the closing tag of AddNewPRoduct, return the lastProductID

I was doing a return lastProductID after each Case check, before each break;
that's all I needed to understand...it wasn't obvious.



 
 
FavorFlave





PostPosted: Visual C# General, I How to return and use a value from one method call to another Top

I had already tried that by doing this

public void Start(IWorkerDriver host)

{

...some code and then

try

{

private static long lastProductID = 0;

...blah blah blah

Error: Invalid expression term 'private

Invalid expression term 'static'



 
 
FavorFlave





PostPosted: Visual C# General, I How to return and use a value from one method call to another Top

cool, thanks much Mark!