Anyone good with C#?

Discussion in 'OT Technology' started by RetroEvolute, Dec 17, 2008.

  1. RetroEvolute

    RetroEvolute 404 Not Found OT Supporter

    Joined:
    Mar 25, 2008
    Messages:
    14,858
    Likes Received:
    0
    Location:
    Manhattan, K.S.
    I'm in a programming class and I'm down to the final project, but am realizing that I forgot how to do a lot of stuff.. :wtc:

    Thanks.

    I've got almost 2 files done.. :dunno:

    I'll post the instructions if I get a positive response. :)
     
  2. CodeX

    CodeX Guest

    Was there an actual question here?
     
  3. RetroEvolute

    RetroEvolute 404 Not Found OT Supporter

    Joined:
    Mar 25, 2008
    Messages:
    14,858
    Likes Received:
    0
    Location:
    Manhattan, K.S.
    Here's the info and what I've got. I'm just really mixed up cause there's so much to think about.

    What I have so far:

    FinalProject.cs-
    Code:
    using System;
    
    public class Test {
        public static void Main() {
            List list = null;
            ArrayList list = new ArrayList();
            
            for (int i = 0; i <= 100; i++) {
                list.add(i);
            }
            Console.WriteLine(list.ToString());
            
            for (int i = 1; i <=100; i+=2) {
                list.remove(i);
            }
            
            Console.WriteLine(list.ToString());
            Console.ReadLine();
        }
    }
    
    Currently only running a set of numbers from 1-100, and then again but only evens to test the other scripts.

    List.cs-
    Code:
    public interface List {
        void add(object x);
        bool remove(object x);
        int size();
    }
    ArrayList.cs-
    Code:
    using System;
    
    public class ArrayList : List {
        private object[] arr;
        private int length;
        
        public ArrayList() {
            arr = new object[10];
            length = 0;
        }
        
        //factor = 0.5, make half as big
        //factor = 2, make twice as big
        private void resize(double factor) {
            object[] newarr = new object[(int)(arr.Length*factor)];
            Array.Copy(arr, 0, newarr, 0, length);
            arr = newarr;
        }
        
        public void add(object obj) {
            if (arr.Length == length) resize(2);
            arr[length] = obj;
            length++;
        }
        
        //resizing: if array less than half full, and has more than 10 elements
        public bool remove(object x) {
            for (int i = 0; i < length; i++) {
                if (arr[i].Equals(x)) {
                    Array.Copy(arr, i+1, arr, i, length-i-1);
                    length--;
                    if (length < arr.Length/2 && arr.Length > 10) {
                        resize(0.5);
                    }
                    
                    return true;
                }
            }
            
            return false;
        }
        
        public int size() {
            return length;
        }
        
        public override string ToString() { //from object
            string s = "";
            for (int i = 0; i < length; i++) {
                s += arr[i].ToString() + "\n";
            }
            return s;
        }
    }
    I just don't know where to begin. Kinda overwhelmed right now.. :(
     
  4. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    An Array list is an object, not a class
     
  5. CodeX

    CodeX Guest

    Do you need help understanding the OO concepts of classes/interfaces/objects/etc... or do you need help with the syntax of the language?

    The requirements for the project are a bit ridiculous, you could implement a program with the same functionality with much less overhead, but I assume that is just to test you on the varous aspects of it.
     
  6. CodeX

    CodeX Guest

    Array List as described here is a class. An object is an instance of a class or structure. He would need to make an instance of the arraylist class. which implements the list interface.... it's kind of redundant, and I assume the only reason they want you to do that is to maintain two seperate implementations of list, one that sorts the elements and one that does not, but that distinction is hardly enough reason to make two seperate classes to implement them imo.

    I haven't used C# but in normal C++ OO this seems like list would be an abstract base class and arrayList and arrayListSorted would be child classes that inherit from list but overload the methods and implement them differently. If that were the case I would make the methods in list pure virtual and not even bother with a generic implementation.
     
  7. RetroEvolute

    RetroEvolute 404 Not Found OT Supporter

    Joined:
    Mar 25, 2008
    Messages:
    14,858
    Likes Received:
    0
    Location:
    Manhattan, K.S.
    Yeah. They really just kinda threw us into this stuff pretty quick. This is my first programming class I've ever taken and it's hard to grasp what everything does, and why/how.

    Concepts of the classes/interfaces/objects would probably help. Beyond that, I have a pretty good concept of how the code flows. I mean... This is the 8th project I've technically had to do for the class, but it really goes over EVERYTHING and I realize I've forgotten/never completely understood some things.

    My main problems are how to call classes, and what all the static, void, etc prefixes really mean. It seemed like I was just told. Oh, you need this here.. And I just obeyed. :ugh:

    Thanks for the replies.
     
  8. CodeX

    CodeX Guest

    Well basically a class is a collection of variables and methods. Think of it as defining a type of something, with distinct properties and actions that can be performed on it. You then use that class to create objects of that type, with each object having its own seperate set of member variables and each being able to call the same set of methods.

    For example I re-wrote Super Mario 3 and an example of a class that I used was Goomba. The Goomba class contained all of the information needed to describe one specific goomba in the game world, such as its location and movement direction, current frame of animation, the boundaries of its "patrol" area, etc. The class also contained methods that could be carried out on the goomba, such as a method to check for collisions with other objects, a method to perform the death animation when mario jumps on him, and a method to to make the goomba enter what I called "berserk mode" (lol).

    Now, to use this class I created an instance of it (an object) for each goomba on the current level. So you would have something like this:

    Goomba goomba1 = new Goomba();
    Goomba goomba2 = new Goomba();
    Goomba goomba3 = new Goomba();
    Goomba goomba4 = new Goomba();

    Now, each of those 4 goombas are of the type Goomba (just like int x is of the type int) and each of them maintains a seperate set of the member variables of that class (location on the screen, etc.). In addition each of them can call the same set of methods defined in that class, except that the variables used in those methods will automatically point to the set of variables maintained by the specific object that called it.

    So, if I were to call a method Move(int direction, int distance); on goomba1 I would do so like this: goomba1.Move(LEFT, 10); Now, the implementation of the Move method is defined and works the same for all goombas, but when it is called it uses the set of variables of the specific object that called it, this way when I tell goomba1 to move, only goomba1 moves, and not all of them, or a different one.

    Make any sense?
     
  9. RetroEvolute

    RetroEvolute 404 Not Found OT Supporter

    Joined:
    Mar 25, 2008
    Messages:
    14,858
    Likes Received:
    0
    Location:
    Manhattan, K.S.
    Makes sense. I'm well on my way figuring things out. Thanks.

    Any more tips that have helped you out in the past when programming that may help me as well?
     
  10. RetroEvolute

    RetroEvolute 404 Not Found OT Supporter

    Joined:
    Mar 25, 2008
    Messages:
    14,858
    Likes Received:
    0
    Location:
    Manhattan, K.S.
    Hey, is there anyone that could possibly help me with the

    I've gotten pretty much the whole thing taken care of, but I'm not sure how to do this part, and it's kinda crucial to entire program.. :sad2:

    Also, how to swap between -sorted and -unsorted via command prompt.

    Thanks guys. I'd be screwed without you. Ha :bowdown:
     
  11. CodeX

    CodeX Guest

    alot of that is specific to C#, which I don't know, I know C++. The IComparable interface and the Equals and ToString methods appear to be something already established in the languages common library, but you are being asked to overload them to be used for this specific class (the Person class). I assume that when a class implements and interface it is basically the same thing as a parent/child inheritence relationship in the OO terms that I am familiar with. If that is the case I could help you understand it better but without knowing for sure I am hesitant.

    Passing in command line arguments is again dependant on the language, in C++ you do this:

    void main(int argc, char* argv[])
    {
    //Rest of program
    }

    Where argc is then the space delimited argument count and argv is a pointer to an array of C-Style strings containing the arguments. In other languages like visual basic and java it is completely different and I assume it is somewhat different in C# as well.
     
  12. RetroEvolute

    RetroEvolute 404 Not Found OT Supporter

    Joined:
    Mar 25, 2008
    Messages:
    14,858
    Likes Received:
    0
    Location:
    Manhattan, K.S.

    Thanks. I've got that working now.

    One more.. This may be simple for anyone with a solid background is programming, but I'm confused:
    Code:
    Console.Write("Input a person <last first age> to insert: ");
            string personinfo = Console.ReadLine();
            char[] delimeter = {' '};
            string[] tokens = personinfo.Split(delimeter);
            return tokens;
    
    ERROR CS0029: Cannot implicitly convert type 'string[]' to 'string'

    :dunno:
     
  13. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    i don't see anything wrong there, my guess is that in your function signature you put the return value as string but you're returning tokens which is string[].
     
  14. RetroEvolute

    RetroEvolute 404 Not Found OT Supporter

    Joined:
    Mar 25, 2008
    Messages:
    14,858
    Likes Received:
    0
    Location:
    Manhattan, K.S.
    How do you mean? Ha.

    The bolded is pretty much what the error tells me, isn't it? :dunno:
     
  15. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    well i don't know what function that code segment you pasted is in, but imagine it was like this

    Code:
    public string someFunction(){
          string[] tokens;
          return tokens;
    }
    
    that would give you an error because in the signature you said the function was going to return a string and then you returned a string array.
     
  16. RetroEvolute

    RetroEvolute 404 Not Found OT Supporter

    Joined:
    Mar 25, 2008
    Messages:
    14,858
    Likes Received:
    0
    Location:
    Manhattan, K.S.
    Ahh, thank you. That did it. :bigthumb:
     
  17. zanyspy_dude

    zanyspy_dude King of teh n00bz

    Joined:
    Aug 29, 2002
    Messages:
    4,473
    Likes Received:
    0
    Location:
    Indianapolis, IN
    wow, OT actually helping someone in the comp forum. Jesus christ!
     
  18. RetroEvolute

    RetroEvolute 404 Not Found OT Supporter

    Joined:
    Mar 25, 2008
    Messages:
    14,858
    Likes Received:
    0
    Location:
    Manhattan, K.S.
    I know. OT ftw.

    Btw, I just turned the project in. I didn't get everything done perfectly, but it's complete enough, I think. Should get a good majority of the points.

    And thanks for all the help. I would be really hurting if it weren't for it.

    :h5:
     
  19. CodeX

    CodeX Guest

    I pretty much help anyone who posts here about programming. :coolugh:
     
  20. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    This project would have been really simple with .NET 3.5 and Linq. C# 4 isn't even that far off either.

    I guess Linq and generics might be a bit much at first?
     
  21. RetroEvolute

    RetroEvolute 404 Not Found OT Supporter

    Joined:
    Mar 25, 2008
    Messages:
    14,858
    Likes Received:
    0
    Location:
    Manhattan, K.S.
    Dunno. I've never done it!

    But probably... :wtc:
     
  22. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    It probably would've been, but generics aren't the answer to everything. This seems to be a pretty good exercise to illustrate the concepts being presented.
     
  23. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    It would have been ideal for this. I can't remember the last time I used ArrayList.

    Generics isn't the answer to everything but when it comes to collections I've found it almost always is.

    It's just a bit concerning to see this being taught, how far behind it is with the current state of C# and code being written today, though I can't say I'm surprised heh.
     
  24. SPACECATAZ

    SPACECATAZ New Member

    Joined:
    Dec 22, 2006
    Messages:
    2,502
    Likes Received:
    0
    What's a good way to learn/retain C++ programming?

    I'm reading a book called C++ Primer Plus, but it seems like all of the fundamentals I am learning are going in one ear and out the other. I have to take a class on it next semester and it looks like I'm already trouble. How did you go about learning C++? Thanks
     
  25. CodeX

    CodeX Guest

    Do it.

    Write programs in your free time, start with something trivial, then think "what small thing would make this program better?" Then figure out how to do that. This way you are forced to keep learning how to do new things, and in doing so will almost inevitably learn the deeper more difficult aspects of the language.

    Keep doing that, program on a daily basis if even for only a half hour. Make programs that interest you. I wrote games, the first ones I wrote were text based DOS games, then I thought, "I want to do graphics" so I learned how to write an inline assembly putpixel routine using DOS Mode X. Later, I taught myself the majority of the DirectX and OpenGl API's and now, if I ever had the time, I could make some pretty good looking games, a far cry from the shit I started with, but it is a natural progression, and it takes time depending on how much you put into it.

    If you were looking for book suggestions I don't have any, thats not how I learn and imo thats not a good way to learn to program, you learn to program by practicing it.

    As far as figuring things out, cplusplus.com is a great resource for language specific information, the msdn is also an excellent resource if you are developing for windows, other than that, ask on forums. If you can't figure out how to do something there are forums with hundreds of highly qualified people, most of whom will help if you ask the question in a way indicating that you actually care to learn, and not have it done for you.

    And for the love of god, EXPERIMENT. You can't hurt anything by doing something wrong, the worst that can happen is a compile time error (unless you are writing assembly, then the worst that can happen is you will have to restart your computer)
     

Share This Page