My situations is as follows.

main.cs is a main script (you don't say, whaaaaaa?), it includes functions.cs by using functions;. I managed to get OOP working (had some struggle with it, but figured it out). I know that I can pass variables from main.cs to functions.cs by executing:

Class FavVarName = new Class();
FavVarName.ExternalVoid("if you tell it's a string, you can put text here");

So, I know that how to manage "if you [...] text here", and how to get it working and make it usable in functions.cs. But what if I want to set bool callback = true in main.cs from functions.cs?

How would I set variable callback in main.cs while runtime(?) is still busy with executing remote "void's" in functions.cs?

This is kinda hard to explain, I hope you can understand it.

Recommended Answers

All 11 Replies

Yeah... it's kinda hard to understand too... I didn't.
Can't you post your classes or at least the methods you're talking about?

Can't you post your classes or at least the methods you're talking about?

Argh, not really. But I think I know how to play this:

To set variable in functions.cs called flashback from file main.cs. You execute:

main.cs

using functions;
Class FavVarName = new Class();
FavVarName.ExternalVoid("if you tell it's a string, you can put text here");

functions.cs

class Class {
    public void ExternalVoid(string input);
    Console.Writeline(input);   // echo's "if you tell...put text here"
}

But how do I set variable in main.cs called comeback from file functions.cs by example:

main.cs

using verify;
Class verify = new Class();
public string bool = verify.VerifyPassword("Shrek :D");

if (bool == "funghi") { Console.Writeline("Hello"); } 

functions.cs

class Class {
    public void VerifyPassword(string input);
    [abra ca dabra]
    return "funghi";
}

I need to functions.cs being able to set usable variables in environment of main.cs.

Okay, another way

main.cs

using verify;
Class verify = new Class();
public string answer = verify.VerifyPassword("Shrek :D");
Console.WriteLine("Your account is " + answer); // verified

functions.cs

class Class {
    public void VerifyPassword(string input);
    [abra ca dabra, checking passwords]
    return "verified"; // if password match found
}

Does that explain it? Or makes it more clear?

Hi

It is still somewhat unclear as to what you want to do. I think you are asking how to update variables in your main program with the results from methods in your class called Class? Is that correct?

Can I make a constructive suggestion, don't name your classes Class, and don't name variables as types such as string bool, this makes your code very hard to read.

In your example of functions.cs it looks like you are doing some form of password validation, therefore, name your class something like Security. Also, for the VerifyPassword method, have this return a boolean value (true/false) rather than a string. Also, if it is returning something it cannot be void. Void denotes a sub routine, not a function and sub routines do not return data. So something like:

class Security {
    public bool VerifyPassword(string input) 
    {
        [abra ca dabra, checking passwords]
        return true; // if password match found
    }
}

Then, in your main.cs you could do:

Security login = new Security();
if(login.VerifyPassword("myPassword") == true)
{
    //Login successful
}
else
{
    //login not successful
}

Is that what you were referring too?

Exactly! But I need to be able to send more than true or false. Is this possible?

Like what?

Yes it is possible but it depends on what it is you want to return.

String, boolean, integer and/or a double. It also depends on what I want to achieve, but I want to apply it in many places and data type is different between these places.

Not quite sure I follow.

To return different data types you simply change the return type of the method. For example, if it was a calculator then you might have methods such as:

public int Add(int firstNumber, int secondNumber)
{
    return firstNumber + secondNumber;
}

public double GetPercentage(....)
{
    //Implementation
}

So in the above (air coded example) you have one method that returns an int and another that returns a double.

Or are you referring to something else, such as having a method that has the same name but can return different types? Or something else entirely?

So in the above (air coded example) you have one method that returns an int and another that returns a double.
Or are you referring to something else, such as having a method that has the same name but can return different types? Or something else entirely?

Oh man, I must be terrible at explaining things in English, oh goly.

int response = HDD.AnswerMe(); for example, won't work, because you "can't convert void to int", even though "void" has:

public void AnswerMe() {
    return 15;
}

And the error it produces is: c:\Users\---\Documents\Projects\OperatingSystem\OperatingSystem\Built-in Functions\Storage.cs(4,4): Error CS0127: Since 'Storage.Mem.AnswerMe()' returns void, a return keyword must not be followed by an object expression (CS0127) (OperatingSystem).

This is where problem lies, I can't return anything else but true or false. That's the point, how do I do that?

Hi

Void methods cannot return values.

If you want to return 15 in the AnswerMe method then the declaration of AnswerMe has to be a function and would look something like:

public int AnswerMe() {
    return 15;
}
commented: We got winner! +2

Thank you. It worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.