1

I have made an InputBox class that returns a string but I want the class to return a string upon exit.

I want it to behave exactly like the Visual Basic Input Box.

For now I have passed a mutable string value into the constructor and change it that way. But then I realized the Visual Basic input box is way better.

myValue = InputBox(message, title, defaultValue)

How can I create my own InputBox class like this? I realize I can import the VB inputbox to c# but I want to make my own custom InputBox.

Edit: I guess the reason I am so fascinated by this is because Visual Basic lets you call a function without putting the name of the class. I wish there was a way of doing this in C# without using delegates. I think my solution may be to create a function which encapsulates my form. So perhaps I will rephrase my question to is there a way I can return a value through something that behaves visually like a class?

2
  • I think the only time you can call a static function in C# without specifying what class it is from is if the function was declared in the class you are currently writing code for. If it was a constructor you would need new. So I don't think you can have it be the same syntax as in VB, but that shouldn't matter - C# is not VB, it doesn't need to have the same syntax. Commented Jun 13, 2013 at 1:09
  • Winforms but Simon has given me the Holy Grail, the ultimate tool to make all my classes look like functions! I will just use implicit operator to make classes look like functions but they will be very modular functions. Commented Jun 13, 2013 at 1:31

1 Answer 1

1

You are incorrect in assuming that the Visual Basic InputBox is a class. It is, in fact, a function. Here is the method definition:

Public Function InputBox( _
    ByVal Prompt As String, _
    Optional ByVal Title As String = "", _
    Optional ByVal DefaultResponse As String = "", _
    Optional ByVal Xpos As Integer = -1, _
    Optional ByVal YPos As Integer = -1 _
) As String

Digging a little deeper, it resides in Microsoft.VisualBasic.dll as a method of the Interaction class.

The closest way to simulate the VB.NET syntactic sugar is to implicitly convert your custom InputBox to a string, e.g:

public class InputBox
{
    private string _title;

    public InputBox(string title)
    {
        _title = title;
    }

    public static implicit operator string(InputBox from)
    {
        return from._title;
    }
}

// .. elsewhere ..
 string title = new InputBox("Test");
 Console.WriteLine(title); // prints Test

.. although I'm not sure if I'd do this personally.

Obviously title is an example. Your InputBox class would spawn a Form (or inherit from it) and return the input into the form.

..may I ask why you're looking to replicate this?

Sign up to request clarification or add additional context in comments.

8 Comments

I miss the ability to have simple functions that I will reuse in all my programs. Such as the old MsgBox and InputBox where it is so common to ask a user for input in a standardized dialogue box. It seems silly to have to use the name of the class or create a delegate just so I don't have to type it out. I am making my own dialogue boxes for a game I am making. I am fascinated by this implicit operator string I have never seen this before and think I will definitely use it! Can you go into more detail of how exactly this works? So I can create an implicit operator for every data type???
Wow I thank you so much for this implicit operator code which I have never seen before. Now I have a whole bunch of ideas to feed my functional based programming desires! You just gave me the tool I need to turn C# into a more functional programming language against everyone else' will!
I think my example is as close as you'll get. You're looking for something similar to the friend keyword in C++.. which doesn't exist in C# in the same way. The above example is purely operator overloading.. in this case, its the implicit operator from InputBox to string. Operator overloading isn't extremely common in C# (from what I've seen), but it should definitely be at least understood by a C# developer.
Your example is great! I will definitely research more into the friend keyword and the differences. friend sounds like my friend. Now I will begin to make almost all my classes into wannabe functions. I never thought about how integer and double knew to implicitly convert and this answers that question. I guess it is uncommon because it really lets you turn C# into a more functional looking language which C# is trying to turn people away from. Don't you miss in VB6 just being able to write MsgBox? I am so making my own MsgBox class that behaves like a function with all modular behavior.
I would warn you about the perils of operator overloading.. but I think it's best if you learn it for yourself :) Glad I could help anyway.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.