Hello, I am attempting to call a method from a click event on a form and not having much luck.

public void xbuttonValidateUPC_Click(object sender, EventArgs e)
{//Validate UPC Button Click
UPC_JDB UPC1 = new UPC_JDB("123");
string temp = (xtextBoxValidate.Text);
UPC1.ValidateUPC(temp);
xtextBoxValidateAnswer.Text = temp;
}//end Validate UPC Button Click

Program is supposed to take a string value in xtextBoxValidate text box, send it to ValidateUPC, and get back a message to display in xtextBoxValidateAnswer text box whether the UPC code is valid or not. At this point it just puts whatever I put in xtextBoxValidate.Text into xtextBoxValidateAnswer.Text. Any ideas?

Did you wrote the UPC1.ValidateUPC()?
If so, is the parameter that takes the "temp" a (ref string ...)?
Does it take a reference to a string (if you expect the string to be modified)?

Did you wrote the UPC1.ValidateUPC()?
If so, is the parameter that takes the "temp" a (ref string ...)?
Does it take a reference to a string (if you expect the string to be modified)?

Yes, I created UPC1.ValidateUPC(). Yes, the paramter that takes the temp variable is a string. Yes, it does take a reference to a string. It works in console but trying to get it working in a form has eluded me so far. I pretty much too the code from the console version and modified it for the form.

Here is the console version that works just fine.:

UPC_JDB UPC1 = new UPC_JDB("123");
Console.Write("Enter UPC to be validated: ");
string temp = Console.ReadLine();
Console.Write(UPC1.ValidateUPC(temp));
Console.ReadLine();

I was able to walk through the program and ValidateUPC is definitely getting the string variable and processing it properly. The problem I believe is in the return.

Here is the code for the return in ValidateUPC.

if (CheckSum != num12)
           {
               return "The UPC checksum is invalid"; 
           }
           else
           {
               return "The UPC checksum is valid";
           }

I was assuming the return string value of temp would be either "The UPC checksum is invalid" or "The UPC checksum is valid". It doesn't appear anything is returning from ValidateUPC.

Edited 3 Years Ago by Dani: Formatting fixed

No, you're not actually modifying "temp" with that type of function.
What you should do (if you want to keep it the way you have it is):

xtextBoxValidateAnswer.Text = UPC1.ValidateUPC(temp);

Console.Write(UPC1.ValidateUPC(temp)); writes the output from the function to the console whereas UPC1.ValidateUPC(temp); never saves the output for later display.
I suspect your Validate rountine is coded using "pass by value" rather than "pass by reference" - partly because a routine using ref wouldn't actually need a returned value.

This article has been dead for over six months. Start a new discussion instead.