Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Getting a return value from a class

Author  Topic 

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2008-07-20 : 07:46:40
Hello,
Am using .net c#

I have a .net page that calls a class as thus:

user_processor myclass = new user_processor();


And the class does some work and is "MEANT" To give a feedback to the calling page. Which i tried using. ( Here is my edited code )


public class user_processor
{
public user_processor(string user)
{


try
{
int lenght = Int32.Parse(csv[0]);

statusquo = "success";
{ return statusquo; }
}
catch
{
//IF ERROR
statusquo = "failure";
{return statusquo ;}
}


I get the error

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0127: Since 'user_processor.user_processor(string)' returns void, a return keyword must not be followed by an object expression

Source Error:


Line 90: statusquo
Line 91: statusquo = "failure";
Line 92: { return statusquo; }
Line 93: }
Line 94: catch

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-07-20 : 08:15:32
Your declaration:

public user_processor(string user)


Doesn't indicate the return type of your method. If it is returning a string, you should write:


public string user_processor(string user)


Also note that it is bad practice to return strings like "success" or "failure"; you should either return a boolean value or an enum.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2008-07-20 : 08:24:38
Thanks Jeff,
As i said, I actually edited my code above. My return value initially was an integer.

Adding string. I now get this error.

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0542: 'user_processor': member names cannot be the same as their enclosing type

Source Error:

Line: 18

Line 16: public class user_processor
Line 17: {
Line 18: public string user_processor(string user

Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-07-20 : 08:37:06
The error is telling you exact what the problem is. Member names cannot be the same as their enclosing type. You have pick a different name for your method.



- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2008-07-20 : 08:57:20
Ok.
I picked a different name but still got an error

not all code paths return a value

Then I added an output parameter as thus

Line 18: public string user_processor(string user, out int Ehi )

Then later in the code assigned the output parameter to replace the statusquo. But still get the error

not all code paths return a value

Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-07-20 : 10:07:24
post the class as it is. that's the best way for us to find the error.

_______________________________________________
Causing trouble since 1980
Blog: http://weblogs.sqlteam.com/mladenp
Speed up SSMS development: www.ssmstoolspack.com <- version 1.0 out!
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-07-20 : 16:34:04
ctors cannot have a return type. that method you posted is not a method, but a ctor.

generally ctors should not do any work. they should just initialize the class with default values or values that you pass in if it's not a default ctor.

add a method returning string or whatever that is named something different from the name of your class (user_processor) to do the actual work.


elsasoft.org
Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2008-07-20 : 17:12:25
hi Guys,
i finally got it to work.

Mladen, I actually hadnt got a "FULL CODE" as i was testing a new class.

I used a private constructor, as thus...

private static string mytest = "somevalue";
public static string testing
{
get
{
return mytest;
}
set
{
mytest = value;
}
}
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-07-20 : 23:23:13
that's not a private constructor. that's a private field named "mytest", with public property named "testing"


elsasoft.org
Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2008-07-21 : 03:32:10
cool

thanks for the update :-)
Go to Top of Page
   

- Advertisement -