CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2009
    Location
    Stuttgart, Germany
    Posts
    56

    Best way to manage values without get complicated with returns and parameters?

    Ok guys I've been working in some program and it's finished already and working perfectly, but I am no very pleased about it because I'm not quit sure if I am implementing the variables. I mean I need common values within different methods and I heard that its not good to use all the time Global Variables(fields), so I tried to use some kind of structure but I don't know if its the best to used this structure, at the same time I used some fields to support my program, because it seems to be that I cannot initializate a variable within a structure. I also use parameters and returns in this program. By the moment Im not working with Threads(I heard that fields are not good for Threading security), but at the moment Im not using them.

    Now I attached an example, maybe it does not make any sense, because it's only a little picture of how Im working now with the program, but as I said I am not satisfy. I would like to improve it and have a better understanding how to manage values within a program. Why Structures, fields, local variables, interfaces.... why methods within structures.. how this kind of stuff can help me to enhance my code, advantages and disadvantage etc.

    Take a look.

    Code:
    using System.Collections.Generic;
    using System;
    using System.Threading;
    
    class MyClass2
    {
        List<double> targetTemp = new List<double>();
        static int T_stab;
        string auxOvenFilePath = "testFile.txt";
    
        public struct CChambStruct
        {        
            public DateTime startTime;
            public DateTime endTime;
        }
    
        CChambStruct CCh;
    
        public static void Main()
        {
            T_stab = 1;
            MyClass2 hola = new MyClass2();        
            hola.doTemp();
        }
    
        public void doTemp()
        {        
            for (int i=0; i<=5; i++)
            {
                CCh.startTime = getTime();
                targetTemp.Add(20*i);            
                Thread.Sleep(i*1000);
                CCh.endTime = getTime();
                doSomeTask(i, 'a');
            }        
        }
    
        public void doSomeTask(int i, char tempType)
        {
            Console.WriteLine("Filename: {0} \nIndex: {1} \nTemp Type: {2} \nTarget Temp: {3} \nTime duration: {4}seconds \nStabilization Time: {5} \n", auxOvenFilePath, i, tempType, targetTemp[i], CCh.endTime.Subtract(CCh.startTime).TotalSeconds.ToString(), T_stab);
        }
    
        public DateTime getTime()
        {
            DateTime currTime = DateTime.Now;
            return currTime;
        }
    }
    I hope it doesnt look like spagetti.

    Best regards,
    Raul.

  2. #2
    Join Date
    Apr 2009
    Location
    Bahamas
    Posts
    33

    Re: Best way to manage values without get complicated with returns and parameters?

    Structs are value types and classes are reference types. It is perfectly fine to use global fields as long as they are used many times throughout the program and are declared as const. This can reduce a huge amount of code duplication.

    Global variables are never a good idea. I use global consts all the time for some program settings that are required by almost every class in the kernel and are guaranteed to never change. If you do use global consts make sure to keep them in a seperate class so they are all together and be sure to fully qualify them so its clear to the reader where they are declared.

    I think you are confusing global fields with class fields they are completely different. Whether you declare class fields or use params and return values completely depends on your program. Just make sure you are scoping your class fields correctly.

    Any data having to do with the class itself is stored as a class field. The first name of a customer class for example. To make the program OO compliant.

    If you are performing a calculation of some kind you will delagate the computation to a different function using a return value or sometimes out or ref to bring the data back to the callng functuon.

    It sounds like your best bet would be to search on MSDN for these areas to learn what they do or get a book from the library.

    Knowing what to use and where to use them is completely dependant on your program. Just make sure you are using correct OOP.

    -- Edit --

    I was using a blackbery before and was unable to view your code correctly. Now that I can see it, I would state that it needs a lot of changes...

    I don't know why you are creatinga struct inside your class, or why your class is creating another instance of itself.

    Code:
    using System.Collections.Generic;
    using System;
    using System.Threading;
    
    public class MyClass2
    {
        private List<double> m_targetTemp = new List<double>();
        private static int m_stab;
        private const string AUX_OVEN_FILE_PATH = "testFile.txt";
           
        private DateTime m_startTime;
        private DateTime m_endTime;
        public DateTime StartTime { get; } { set; }
        public DateTime EndTime { get; } { set ; }
        public TargetTemp { get; } { private set; }
    
        public static void Main()
        {
            m_stab = 1;
            this.doTemp();
        }
    
        public void doTemp()
        {        
            for (int i=0; i<=5; i++)
            {
                StartTime = getTime();
                TargetTemp.Add(20*i);            
                Thread.Sleep(i*1000);
                EndTime = getTime();
                doSomeTask(i, 'a');
            }        
        }
    
        private void doSomeTask(int p_i, char p_tempType)
        {
            Console.WriteLine("Filename: {0} \nIndex: {1} \nTemp Type: {2} \nTarget Temp: {3} \nTime duration: {4}seconds \nStabilization Time: {5} \n", AUX_OVEN_FILE_PATH, p_i, p_tempType, TargetTemp[i], EndTime.Subtract(StartTime).TotalSeconds.ToString(), m_stab);
        }
    
        private DateTime getTime()
        {
            return DateTime.Now;
        }
    }
    I'm at the public library so I don't have access to anything but notepad, but off the top of my head I provided another possible solution. Without knowing for sure how you intend to use this class I can't be 100% correct.

    Is this class going to be called by a function in another class? What information are you wanting to expose to the calling function? I noticed you have some static fields. I changed getTime so that it is more concise, and I would recommend not usung camel-casing for function names. Make sure you are scoping your functions correctly. If I am understanding your intent correctly, getTime and doSomeTask should not have been scoped as public. If your file path will not be changing it needs to be declared as const and use correct const-casing. Also, make sure you are encapsing your fields. You should never allow a class field to be visible to another class.

    Interfaces are a completely different story, for example they could be used with reflection to allow your program to support different data stores without having to change any code within the program itself. You could support XML, Acccess, MS SQL, and MySQL simply by creating the provider that implements the interface.

    I haven't checked the code I provdied carefully, so let me know if you have any questions.
    Last edited by pgrammer; April 22nd, 2009 at 02:21 PM. Reason: provided code example
    Thanks,
    - pgrammer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured