locked
Returns from a method RRS feed

  • Question

  • Forgive me, this may be stupid. I come from many years of visual basic programming and have no decided I wish to learn c#. I have what seems to be a simple problem ?. I am just playing with simple methods etc and want to simply call a method to produce, in this example, a single string return. I am passing to string variables into a method which I want to return a single string. ie firstname, lastname to retrun fullname - firstname + " " + lastname……. what am I missing...????

    namespace ConsoleApplication4
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Write("First Name : ");
                string firstname = Console.ReadLine();
                Console.Write("Last Name : ");
                string lastname = Console.ReadLine();
                string fullname = methodone(firstname, lastname);
               
                Console.ReadLine();
            }

            private static methodone(string firstname, string lastname)

            {
                string fullname = firstname + " " + lastname;
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Your name is : " + (fullname));
                //return firstname + lastname;
                return fullname ;    --------------------------------->>> can not return this string ??????????
            }

        // End of Class
        }
    // End of namespace
    }


    Martin D. Jones

    Tuesday, May 29, 2018 1:06 PM

All replies

  • You need to specify the return type of the method:

    private static string methodone(string firstname, string lastname)



    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    • Proposed as answer by CoolDadTx Tuesday, May 29, 2018 1:56 PM
    Tuesday, May 29, 2018 1:36 PM
  • Martin,

    I guess you come from an old way of VB programming. 

    To start with what is for most VB developers the first time difficult: Your method would be what is in VB a function (I know there are some who don't agree with me). 


    In C type languages a VB function is made because the return type is given and the method has no special name. The code you see already from Dave. 

    In VB this would be Public function (string firstname, string lastname) as string

       

    A VB sub is in C# would be

    private static void methodone(string firstname, string lastname)


    Success
    Cor



    Tuesday, May 29, 2018 2:03 PM
  • Hello,

    You are better off asserting values entered first then performing the method call e.g.

    using System;
    namespace ConsoleApp4
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Write("First Name : ");
                string firstname = Console.ReadLine();
                Console.Write("Last Name : ");
                string lastname = Console.ReadLine();
                if (firstname.Length != 0 && lastname.Length != 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine($"Your name is {methodone(firstname, lastname)}"); 
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Can not complete operation as values are missing");
                }
    
                Console.ReadLine();
            }
            private static string methodone(string firstname, string lastname)
            {
                return $"{firstname} {lastname}";
            }
        }
    }
    And of course as already pointed out you were missing the return type.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, May 29, 2018 2:08 PM
  • Many thanks, the basics. Been in the VB world for far to long, like changing to French :). What would you suggest the best way forward in regards to full windows app development WPF ???. 

    Martin D. Jones

    Wednesday, May 30, 2018 8:10 AM
  • Many thanks for your help - VB from day one, but need to upgrade. What would be your advise to move into c# ?

    Martin D. Jones

    Wednesday, May 30, 2018 8:32 AM
  • Many thanks for your help - VB from day one, but need to upgrade. What would be your advise to move into c# ?

    Martin D. Jones

    First ask yourself why you do it. .Net VB has almost the same  keywords as VB before version 7 (product name VB.Net) but is currently in version VB15 and has still less changes in code than between version 1 to 6.

    Beside the keywords and characters used in C style or Basic style, there are not really much differences between C# and .Net VB. Probably is going first to .Net VB for you easier because the step to OOP is less high. I see that those who step direct from classic VB to C# keep their VB6 style of programming. 

    Nevertheless I advice you to study C# because samples on Internet are given in C# and in VB. 

    Be aware that despite what I wrote there are no good converters anymore from VB6 to VB15. In VB6 was Com used and in C# and in VB since version 7 is .Net used (and more). Often is the bottleneck for converting ActiveX which is not available anymore in .Net programming (all program languages) in the way it was in past.  


    Success
    Cor


    Wednesday, May 30, 2018 10:04 AM