Resolved Nested Methods alternative


Jul 7, 2017
Programming Experience
I'm trying to come up with a "library" of methods that my main app will be using. Right now I have a Functions.cs file like this:

namespace SAT
    public class SATFuncoes
        [DllImport("C:\\SAT\\SAT.dll", CallingConvention = CallingConvention.Cdecl)]
            public static extern IntPtr ConsultarStatusOperacional(int numeroSessao, [MarshalAs(UnmanagedType.LPStr)] string codigoDeAtivacao);

        public static string ConsultarStatusOperacional(string codAtiv)
            int numSessao = GerarCodigoNumerico(int.Parse(DateTime.Now.ToString("HHmmss"))); //This is just a method to generate a random 6 digit int based on current time
            IntPtr ptr = SATFuncoes.ConsultarStatusOperacional(numSessao, codAtiv); //Calls the method provided by SAT.dll
            string resultado = Marshal.PtrToStringAnsi(ptr); // writes the result to the string
            String[] resultados = resultado.Split('|'); // splits the result into an array of strings
            return resultado;//returns me the whole string

SAT.dll functions return me a string of multiple variables separated with pipe ("|"). To use that information on the main application, I'm using a string.split method to extract the variables.

Is there a way to set up a method so that calling SATFuncoes.ConsultarStatusOperacional("1234") would return me a string with all the variables, but calling SATFuncoes.ConsultarStatusOperacional("1234").Variable2(); return me only resultados[1], SATFuncoes.ConsultarStatusOperacional("1234").Variable3(); return resultados[2]?
The number of variables are always known and their types are always expected as well.
Last edited:
If ConsultarStatusOperacional returns a String object then that suggested Variable2 would have to be something that you can call on a String. It's not a member of the String class so that means that it would have to be an extension method. I don't think it's practical to have Variable2 and Variable3 though. Don't you think that it would be more appropriate to have a single method that you pass the index of the part you want to?
public static class StringExtensions
    public static string Variable(this string source, int index)
        return source.Split('|')[index];

That code will throw an exception if the index is not valid. If you want it to simply return null in that case:
public static class StringExtensions
    public static string Variable(this string source, int index)
        return source.Split('|').Skip(index).FirstOrDefault();

Note that a method like that would normally be called like this:
var subbstring = Variable(myString, index);

but the fact that it is declared static and the first parameter is declared with the 'this' keyword means that it can be called like this:
var substring = myString.Variable(index);
Hadn't thought of that. Actually I didn't know you could do that. Thanks!
I think you remember me from vbforums, don't you?
Top Bottom