As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
We're funding a new Acquisitions Incorporated series on Kickstarter right now! Check it out at https://www.kickstarter.com/projects/pennyarcade/acquisitions-incorporated-the-series-2

PA Programming Thread :: PAdev.net - Need hosting for a service of yours? Check it out.

11819212324100

Posts

  • GodfatherGodfather Registered User regular
    edited November 2011
    I made my very first C# program! Check it out:

    G3a92.png

    I know this is extremely laughable for everyone here, but it's a big deal for me!

    Godfather on
  • Monkey Ball WarriorMonkey Ball Warrior A collection of mediocre hats Seattle, WARegistered User regular
    edited November 2011
    What is the STAThread attribute? Just curious.
    Now that I look at it, some of my first C# doodles has it too, VS must add it automatically.
    "Indicates that the Com Thread Model is sleeping in your apartment"... or something.
    Weird.

    One of the first things I did when I was learning it was make a RPC Calculator.
    namespace StackCalc {
        /** <summary>
         * Represents the main calculator UI and functionality.
         * This class is a gross violation of MVC design.
         * </summary> **/
        public partial class StackCalcMainWin : Form {
    
            /** <summary>Stack of operands (values) input by the user. </summary> **/
            Stack<double> input = new Stack<double>();
    
            /** <summary>Represents a mapping of all the operators to their functions.</summary> **/
            IDictionary<char, Func<double, double, double>> binaryOperators
                = new Dictionary<char, Func<double, double, double>>();
    
            // Initializes the calculator and it's UI **/
            public StackCalcMainWin() {
                InitializeComponent();
                addOperators();
                this.Text = "RPN Calculator";
                label1.Text = "";
                label2.Text = "Usage:\n    Type \"q\" and enter to quit\n    Valid Operands: + - * / ^ pop";
            }
    
            /** <summary> 
             * Adds all the operators command characters and functions to the
             * operators mapping.
             * </summary>
             **/
            void addOperators() {
                binaryOperators.Add('+', (x, y) => x + y);
                binaryOperators.Add('-', (x, y) => x - y);
                binaryOperators.Add('*', (x, y) => x * y);
                binaryOperators.Add('/', (x, y) => x / y);
                binaryOperators.Add('^', (x, y) => Math.Pow(x, y));
            }
    
            /** <summary> Run when the user types into the input textbox. </summary> **/
            void inputBox_TextChanged(object sender, EventArgs e) {
                string boxtext = inputBox.Text;
                if (boxtext.Count() > 0) {
                    char last = boxtext.Last();
                    if (binaryOperators.ContainsKey(last) && checkOperands(2)) {
                        //lookup operand, feed it two popped off operands, and push the results
                        input.Push(binaryOperators[last](input.Pop(), input.Pop()));
                        errorlabel.Text = "";
                        inputBox.Text = "";
                        displaytext();
                    } else if (last == '\n') { //user probably input an operand
                        parseInput(boxtext.Trim());
                        inputBox.Text = "";
                    }
                }
            }
    
            /** <summary>Attempts to parse input that is not an operator. </summary> **/
            void parseInput(string s) {
                if (s == "quit" || s == "QUIT" || s == "q" || s == "Q") {
                    Environment.Exit(0);
                } else if (s == "pop" || s == "Pop" || s == "POP") {
                    if (checkOperands(1)) {
                        input.Pop();
                        displaytext();
                    }
                } else {
                    try {
                        input.Push(Double.Parse(s));
                        errorlabel.Text = "";
                        displaytext();
                    } catch (FormatException) {
                        errorlabel.Text = "Cannot Parse";
                    } catch (OverflowException) {
                        errorlabel.Text = "Overflow!";
                    }
                }
            }
    
            /** 
             * <summary> Checks for sufficient operands for binary operators.</summary>
             * <returns> True if there are at least two operands on the stack.</returns>
             */
            bool checkOperands(int enough) {
                if (input.Count >= enough) {
                    return true;
                } else {
                    errorlabel.Text = "Insufficient Operands!";
                    return false;
                }
            }
    
            /** <summary> A ridiculously ineffecient method to display the contents of the stack. </summary> */
            void displaytext() {
                label1.Text = "";
                Stack<double> copy = new Stack<double>();
                while (input.Count > 0) {
                    copy.Push(input.Pop());
                }
                while (copy.Count > 14) {
                    input.Push(copy.Pop());
                }
                while (copy.Count > 0) {
                    double line = copy.Pop();
                    label1.Text += line;
                    if (copy.Count != 0) {
                        label1.Text += "\n";
                    } else {
                        label1.Text += " <";
                    }
                    input.Push(line);
                }
            }
        }
    }
    

    Man that is some ugly code I wrote, :p

    Monkey Ball Warrior on
    "I resent the entire notion of a body as an ante and then raise you a generalized dissatisfaction with physicality itself" -- Tycho
  • AnteCantelopeAnteCantelope Registered User regular
    This is more out of curiosity than a desire to actually utilise it, but is there a way to write a program that can alter its own source code? Say I want to store a username and password, a way to prompt the user to enter the Strings, assign them to variables, and have them written into the code, rather than reprompting next time or writing to a file. Or say I'm writing a calculator program, and the user enters 1+1, is there a way to write int i = userInput so that 1+1 is parsed as code, rather than a string?
    I'm sure there are better ways to implement each of those examples, but are they theoretically possible?

  • ecco the dolphinecco the dolphin Registered User regular
    edited November 2011
    Godfather wrote:
    I made my very first C# program! Check it out:
    G3a92.png

    I know this is extremely laughable for everyone here, but it's a big deal for me!

    Niiiiice!

    Well done on your first step. =)

    This is more out of curiosity than a desire to actually utilise it, but is there a way to write a program that can alter its own source code? Say I want to store a username and password, a way to prompt the user to enter the Strings, assign them to variables, and have them written into the code, rather than reprompting next time or writing to a file. Or say I'm writing a calculator program, and the user enters 1+1, is there a way to write int i = userInput so that 1+1 is parsed as code, rather than a string?
    I'm sure there are better ways to implement each of those examples, but are they theoretically possible?

    Ignoring the amazing security implications that this idea has, you'd probably be best to do it in an interpreted language like perl or python.

    Your calculator would effectively generate another source file, and you'd call the interpreter to run that.

    Edit: So you'd effectively be doing a variant of a Quine

    ecco the dolphin on
    Penny Arcade Developers at PADev.net.
  • AnteCantelopeAnteCantelope Registered User regular
    Godfather wrote:
    I made my very first C# program! Check it out:
    G3a92.png

    I know this is extremely laughable for everyone here, but it's a big deal for me!

    Niiiiice!

    Well done on your first step. =)

    This is more out of curiosity than a desire to actually utilise it, but is there a way to write a program that can alter its own source code? Say I want to store a username and password, a way to prompt the user to enter the Strings, assign them to variables, and have them written into the code, rather than reprompting next time or writing to a file. Or say I'm writing a calculator program, and the user enters 1+1, is there a way to write int i = userInput so that 1+1 is parsed as code, rather than a string?
    I'm sure there are better ways to implement each of those examples, but are they theoretically possible?

    Ignoring the amazing security implications that this idea has, you'd probably be best to do it in an interpreted language like perl or python.

    Your calculator would effectively generate another source file, and you'd call the interpreter to run that.

    I figured security would be an issue, because you can just call cout << masterPassword or anything like that. I guess I'm just interested to know whether a program can alter its source code and recompile itself, you know?

    Python's interpreted, right? Meaning that the interpreter deals with it line by line as the program runs, and so user input could possibly be used as code further down the line without the interpreter knowing the difference? Or have I completely misunderstood the idea of 'interpreted at runtime'?

  • ecco the dolphinecco the dolphin Registered User regular
    edited November 2011
    This is more out of curiosity than a desire to actually utilise it, but is there a way to write a program that can alter its own source code? Say I want to store a username and password, a way to prompt the user to enter the Strings, assign them to variables, and have them written into the code, rather than reprompting next time or writing to a file. Or say I'm writing a calculator program, and the user enters 1+1, is there a way to write int i = userInput so that 1+1 is parsed as code, rather than a string?
    I'm sure there are better ways to implement each of those examples, but are they theoretically possible?

    Ignoring the amazing security implications that this idea has, you'd probably be best to do it in an interpreted language like perl or python.

    Your calculator would effectively generate another source file, and you'd call the interpreter to run that.

    I figured security would be an issue, because you can just call cout << masterPassword or anything like that. I guess I'm just interested to know whether a program can alter its source code and recompile itself, you know?

    Short answer:

    1.) If you can give the programme knowledge of its own source code (trivial in interpreted languages)
    2.) Knowledge of how to generate valid source code based on input, and how to modify the source code accordingly
    3.) Knowledge of how to compile/get the interpreter to execute the new source code

    then yes, it's entirely possible.

    Exact implementation details will vary.

    Python's interpreted, right? Meaning that the interpreter deals with it line by line as the program runs, and so user input could possibly be used as code further down the line without the interpreter knowing the difference? Or have I completely misunderstood the idea of 'interpreted at runtime'?

    I'd say that there's a fair chance that python loads an entire .py file, and then interprets it line by line.

    Might be best (depending on how much an interpreter caches files) to generate an entirely new file, and have the interpreter load that.



    Unless you really wanted to get into self modifying binaries? I think it used to be quite the thing back in DOS days. I believe it was quite popular with virus writers, since you could modify yourself to something that looked benign to fool virus scanners that worked on static signatures, and then modify yourself back to something malign.

    ecco the dolphin on
    Penny Arcade Developers at PADev.net.
  • Monkey Ball WarriorMonkey Ball Warrior A collection of mediocre hats Seattle, WARegistered User regular
    edited November 2011
    Monkey Ball Warrior on
    "I resent the entire notion of a body as an ante and then raise you a generalized dissatisfaction with physicality itself" -- Tycho
  • BarrakkethBarrakketh Registered User regular
    I'd say that there's a fair chance that python loads an entire .py file, and then interprets it line by line.
    It compiles the source file(s) to bytecode when executed. That's why you get those *.pyc files. If the interpreter sees that the source file has changed the next time you run/import it it'll compile the bytecode again, otherwise it can skip that step. You can also pre-compile to bytecode and run/distribute that, and the interpreter also has a builtin function to compile files.

    There's also the builtin compile function, as well as exec, execfile, and eval.

    Rollers are red, chargers are blue....omae wa mou shindeiru
  • seabassseabass Doctor MassachusettsRegistered User regular
    edited November 2011
    I'm not sure that there is any language where a program can't modify it's own source, provided it has access to it:

    Open the file
    Do some I/O
    Call make
    Start the new you

    Now, there are a bunch of languages where it's more elegant. LISP, for example, allows you to build arbitrary strings of characters and then try to evaluate them as if they were code. It's a useful trick if you want to parse natural language into something the program can more easily work with, like a tree. It also allows for some of the worst looking code I've ever seen.

    seabass on
    Run you pigeons, it's Robert Frost!
  • SeolSeol Registered User regular
    edited November 2011
    Any language can modify its own source for the next run. Most compiled languages can't modify the program for the current run, putting program code in a different memory segment to data, and making that segment read-only - this is a deliberate decision and is rarely anything other than what you want.

    Well, many languages can extend themselves at run time, but that's adding more, not changing what's there already.

    But most of the time you want to change code at run time, you actually don't, you just want a more versatile program in the first place.

    Seol on
  • seabassseabass Doctor MassachusettsRegistered User regular
    Seol wrote:
    Any language can modify its own source for the next run. Most compiled languages can't modify the program for the current run, putting program code in a different memory segment to data, and making that segment read-only - this is a deliberate decision and is rarely anything other than what you want.

    Well, many languages can extend themselves at run time, but that's adding more, not changing what's there already.

    But most of the time you want to change code at run time, you actually don't, you just want a more versatile program in the first place.

    Since most languages allow you to start a new process as part of your execution, next run is sort of hazy. If I write a c program that overwrites itself with a new program, compiles and forks, I can definitely get a modified version of myself into memory in a single execution.

    Run you pigeons, it's Robert Frost!
  • GodfatherGodfather Registered User regular
    This programming thing is pretty cool, I'm actually having a lot of fun! I have this natural overfocused nature to my mind, so this plays straight up my alley!

    Does anyone here use their programming voodoo powers to build any games around here? What language is best for that sort of thing?

  • JHunzJHunz Registered User regular
    Godfather wrote:
    This programming thing is pretty cool, I'm actually having a lot of fun! I have this natural overfocused nature to my mind, so this plays straight up my alley!

    Does anyone here use their programming voodoo powers to build any games around here? What language is best for that sort of thing?
    C# is actually a fairly good choice to get started in that right now, if you check out the XNA Framework.

    bunny.gif Gamertag: JHunz. R.I.P. Mygamercard.net bunny.gif
  • AnteCantelopeAnteCantelope Registered User regular
    seabass wrote:
    Seol wrote:
    Any language can modify its own source for the next run. Most compiled languages can't modify the program for the current run, putting program code in a different memory segment to data, and making that segment read-only - this is a deliberate decision and is rarely anything other than what you want.

    Well, many languages can extend themselves at run time, but that's adding more, not changing what's there already.

    But most of the time you want to change code at run time, you actually don't, you just want a more versatile program in the first place.

    Since most languages allow you to start a new process as part of your execution, next run is sort of hazy. If I write a c program that overwrites itself with a new program, compiles and forks, I can definitely get a modified version of myself into memory in a single execution.

    Now that's pretty clever. So I could, for instance, write a program that checks if userPassword = NULL. If so, it prompts for user input, edits the source code, compiles, forks, and logs in like normal. It would, I'm sure, be a terrible way to do it, worse than any other implementation, but it's nonetheless cool to know it exists.

  • SeolSeol Registered User regular
    seabass wrote:
    Seol wrote:
    Any language can modify its own source for the next run. Most compiled languages can't modify the program for the current run, putting program code in a different memory segment to data, and making that segment read-only - this is a deliberate decision and is rarely anything other than what you want.

    Well, many languages can extend themselves at run time, but that's adding more, not changing what's there already.

    But most of the time you want to change code at run time, you actually don't, you just want a more versatile program in the first place.

    Since most languages allow you to start a new process as part of your execution, next run is sort of hazy. If I write a c program that overwrites itself with a new program, compiles and forks, I can definitely get a modified version of myself into memory in a single execution.
    Yes, but you still have the old version running, and the new version doesn't have the state of the old one's stack, and so on. That's not to say it's not something that isn't appropriate every so often - when writing a virus or an IDE, for example. More relevant than whether it can be done is whether it should be done: most of the time, it's not the answer you want (the original query, for example, would be almost infinitely simpler and more secure by implementing a mini-interpreter).

  • The AnonymousThe Anonymous Uh, uh, uhhhhhh... Uh, uh.Registered User regular
    I'm seeing people saying that encrypting data in Django is pointless because it doesn't actually protect anything. That sounds really weird, so I have to ask: is that the case, or are people just being geese?

  • KambingKambing Registered User regular
    I'm seeing people saying that encrypting data in Django is pointless because it doesn't actually protect anything. That sounds really weird, so I have to ask: is that the case, or are people just being geese?

    That's not so much the claim as is the fact that if an attacker somehow gets access to the computer that contains the encrypted data, it is likely that the decryption key is also easily accessible by the attacker. This is probably true because to access the data, you will need to be constantly decrypting said data, so the same computer containing the data must also have access to the key. Likely, that key is stored on the compromised computer itself!

    If you're concerned about that level of security, you either need to push out your trusted computing base further out so that it is manageable, or really think hard about how you encrypt your data but 1) keep it accessible to the application and 2) keep it from being easily decrypted if the database is compromised.

    @TwitchTV, @Youtube: master-level zerg ladder/customs, commentary, and random miscellany.
  • bowenbowen How you doin'? Registered User regular
    I'm seeing people saying that encrypting data in Django is pointless because it doesn't actually protect anything. That sounds really weird, so I have to ask: is that the case, or are people just being geese?

    It's an interpreted system, all details for encryption and decryption are in plain sight. First and foremost you want to prevent access to the system, as no matter how hard you encrypt it if someone can get the files, its game over. Then, after that, you can worry about decryption and such.

    Generally you only need to worry about encryption and decryption when you're sending something over the wire. Email, FTP, website, etc.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • seabassseabass Doctor MassachusettsRegistered User regular
    bowen wrote:
    I'm seeing people saying that encrypting data in Django is pointless because it doesn't actually protect anything. That sounds really weird, so I have to ask: is that the case, or are people just being geese?

    It's an interpreted system, all details for encryption and decryption are in plain sight. First and foremost you want to prevent access to the system, as no matter how hard you encrypt it if someone can get the files, its game over. Then, after that, you can worry about decryption and such.

    Generally you only need to worry about encryption and decryption when you're sending something over the wire. Email, FTP, website, etc.

    When I had my first job, I was trying to encrypt a whole patient database using RSA on a palm device. I was bitching about how hard it had been to tune it down to running in a reasonable time when my mentor looked at me and said "You know, you can just encrypt the whole thing using something like DES, then use RSA to pass the symmetric key around. There is no reason to use asymmetric cryptography on the whole database". So, you know, just keep that in mind.

    Run you pigeons, it's Robert Frost!
  • bowenbowen How you doin'? Registered User regular
    The only reason I'd ever encrypt data would be is if I don't trust the system (public access) or I'm sending data to someone (website/email).

    As it stands right now my database isn't encrypted even though it contains patient data. It's not accessible by the web and there's a custom client that accesses it. To get access to it would require more work than a decryption key would so there's no point in my mind. Sure someone could fire up a SQL browser on the server it's on, but, if you're at that point, the encryption isn't helping shit.

    Basically I repeated myself I think.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • seabassseabass Doctor MassachusettsRegistered User regular
    edited November 2011
    bowen wrote:
    The only reason I'd ever encrypt data would be is if I don't trust the system (public access) or I'm sending data to someone (website/email).

    As it stands right now my database isn't encrypted even though it contains patient data. It's not accessible by the web and there's a custom client that accesses it. To get access to it would require more work than a decryption key would so there's no point in my mind. Sure someone could fire up a SQL browser on the server it's on, but, if you're at that point, the encryption isn't helping shit.

    Basically I repeated myself I think.

    Yeah, we were encrypting a flat file representation of the database, meant to be sent by ftp or email. So it was more a "Hey, hipa says we have to encrypt this" kind of thing than anything else.

    hipa only has the one p...

    seabass on
    Run you pigeons, it's Robert Frost!
  • bowenbowen How you doin'? Registered User regular
    Ah yeah. My old boss is so lucky he didn't get reported for doing that over the wire without encryption. Jesus fuck dude, even a passworded zip file.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • urahonkyurahonky Registered User regular
    I hate typing plug in without a space or hyphen. plugin just looks weird.

    I should really pick up C#, since it's supposedly really close to Java. Then I could ease into XNA.

  • bowenbowen How you doin'? Registered User regular
    I would do C# before Java if I was going to do game programming, that's for sure.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • urahonkyurahonky Registered User regular
    bowen wrote:
    I would do C# before Java if I was going to do game programming, that's for sure.

    I can't even begin to imagine how annoying it would be to make a decent game with Java.

  • seabassseabass Doctor MassachusettsRegistered User regular
    edited November 2011
    bowen wrote:
    I would do C# before Java if I was going to do game programming, that's for sure.

    I dunno. I found the XNA framework to be terribly complicated. It's got a lot of power, but if you're just trying to figure out how to do collision detection or you want to write a missile-commander clone in an afternoon, I think Java is absolutely fine for that.

    Maybe I'm just not used to using frameworks. I do almost everything from scratch, but that's because I haven't done commercial development in about 5 years at this point. Academia is weird.

    urahonky wrote:
    bowen wrote:
    I would do C# before Java if I was going to do game programming, that's for sure.

    I can't even begin to imagine how annoying it would be to make a decent game with Java.
    Depends. How do you feel about swing? Writing some kind of game in Java is what we have the freshmen do for the final project in their intro course. None of them are of exceptional quality mind you, but I know that at least the arcade clones that they turn in are at least as good as the flash clones you see on the web.

    seabass on
    Run you pigeons, it's Robert Frost!
  • bowenbowen How you doin'? Registered User regular
    Mainly if I wanted to integrate C or C++ into it, it would be trivial. Java is fine for things that don't go into the 3d realm, then you start noticing performance bottlenecks super quick.

    XNA gives you a nice interface to meld with DirectX at the least.

    I personally wouldn't use XNA either, but graphics engines are worth exactly what you pay for them, and free ones are garbage honestly. XNA is more like a framework than an engine, and thus a huge swath of time will be making your engine.

    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • urahonkyurahonky Registered User regular
    I'm comfortable with swing, but I just don't think that I'd be able to successfully make a game with Java that looks and plays decently.

  • Gilbert0Gilbert0 North of SeattleRegistered User regular
    urahonky wrote:
    I'm comfortable with swing, but I just don't think that I'd be able to successfully make a game with Java that looks and plays decently.

    It's not that hard, my 300 level programming class made Tetris in java. Behind the scenes it's actually pretty simple, the hardest part was the threading (2-player) and the swing stuff.

  • Mike DangerMike Danger "Diane..." a place both wonderful and strangeRegistered User regular
    ColdFusion question time again: what am I doing wrong here?
    <cfquery name="addJob" datasource="#dbConnection#" dbtype="oledb">
    INSERT INTO PosterPrinting_tbl_Jobs(Address, Event, PType, OtherDim, PTube, FileName, State)
    VALUES('#Trim(FORM.Address)#', '#Trim(FORM.Event)#', '#Trim(FORM.PType)#', #OtherDim#, '#Trim(Form.PTube)#', '#CFFILE.Serverfile#', '#State#')
    </cfquery>
    

    This gives me back "could not find output table PosterPrinting_tbl_Jobs", only I am pretty sure that that exists and is what I want.

    Steam: Mike Danger | PSN/NNID: remadeking | 3DS: 2079-9204-4075
    oE0mva1.jpg
  • urahonkyurahonky Registered User regular
    Do you need to include the database name?

  • InfidelInfidel Heretic Registered User regular
    SQL Injection Alert

    OrokosPA.png
  • SarksusSarksus ATTACK AND DETHRONE GODRegistered User regular
    So I am hating my life, and Java. I have an assignment and the program is mostly done. Everything works except one part. We are creating a "wallet" using one vector, and then filling it with things using enumerations. One of the enumerations is for paper currency and it includes a method to return the value of each object (or bill) so that we can later sum the amount of bills in the wallet and print that to the screen.

    I need to call this enumerator's method from another method outside of main that we're using to print the contents of each enumeration to the screen and it's just not finding the damn method. I keep getting cannot find symbol errors. I am trying to create a reference variable to the enumeration so it knows where it is but it requires I assign an object to that reference variable (one of the bills, for example) and if I did this it would screw up the math.

    Here is the enumeration:
    public enum PaperCurrency {
    		One(1), Five(5), Ten(10), Twenty(20), Fifty(50), Hundred(100);
    
    		private final int value;
    
    		private PaperCurrency(int value) {
    			this.value = value;
    		}
    
    		public int getValue() {
    			return value;
    		}
    }
    

    Here is the method:
    	public static void printPaperCurrency(Vector vector) {
    
    
    			int currencyValue = 0;
    
    			System.out.println("\nPaper Currency:");
    
    			for (Object bill : vector) {
    				if (bill instanceof PaperCurrency) {
    					System.out.println(bill);
    					currencyValue = currencyValue + bill.getValue();
    				}
    			}
    
    			System.out.println("Currency value: " + currencyValue );
    }
    

    Right now since the variable bill is an Object and not a PaperCurrency obviously bill.getValue() doesn't work. I have tried creating a reference variable like so:

    PaperCurrency pp = PaperCurrency.One (because it requires you assign one of the objects in the enumeration to the variable) and all that does is sum one dollar bills.

    The assignment requires that we only use one vector of type Object otherwise this would be a lot easier. Since there's only one vector I haven't been able to move the summing functionality into the main method.

    Does anyone have any idea what I can do? I wouldn't be surprised if I missed something obvious because I've been making a lot of stupid mistakes with this project.

  • seabassseabass Doctor MassachusettsRegistered User regular
    edited November 2011
    Can't you just cast the object to it's most refined type, then get the value?

    Note, that is almost certainly the wrong way to do things, I'm not great with Java.

    seabass on
    Run you pigeons, it's Robert Frost!
  • Jimmy KingJimmy King Registered User regular
    I did some more experimenting with that post_sycndb signal handler just to get all the info. It doesn't work unless the app containing the model that needs the unique partial index is the last app installed, so it's not a good solution at all. This is outside the realm of stuff normally done here, so there's no real procedure in place for dealing with having stuff like this done outside of Django definitions.

    I did do a little bit of playing with adding partial index support do Django from the Meta class this weekend. I just barely looked into it, but on the surface it looks like it's probably way easier to add than you would think.

  • SeolSeol Registered User regular
    Sarksus wrote:
    Right now since the variable bill is an Object and not a PaperCurrency obviously bill.getValue() doesn't work. I have tried creating a reference variable like so:

    PaperCurrency pp = PaperCurrency.One (because it requires you assign one of the objects in the enumeration to the variable) and all that does is sum one dollar bills.

    The assignment requires that we only use one vector of type Object otherwise this would be a lot easier. Since there's only one vector I haven't been able to move the summing functionality into the main method.

    Does anyone have any idea what I can do? I wouldn't be surprised if I missed something obvious because I've been making a lot of stupid mistakes with this project.
    You need to cast it.
    PaperCurrency pp = (PaperCurrency)bill;
    

    This is safe, because you've checked it's a PaperCurrency using instanceof. You can now use all of PaperCurrency's methods on pp.

    However! You shouldn't be using a vector of objects, you should be using a generic collection (and almost certainly an ArrayList over a Vector, but that's for minor reasons that aren't too important right now):
    Vector<PaperCurrency> wallet = new Vector<PaperCurrency>();
    

    Why aren't you using generic collections? That is probably a question for whoever is teaching you.

  • SarksusSarksus ATTACK AND DETHRONE GODRegistered User regular
    Oh God I love you.

    I've never done casting with objects before so I wasn't casting where I should have, and I didn't create the reference variable where I should have either. I was creating it outside of the for each loop and now that I have the casted variable inside of it it woooorks. Thank you all so much.

    And yeah, I do realize how bad it is to use one vector of objects but that is what the assignment has us doing. I think it just values the wallet analogy, with the wallet being one vector and containing a bunch of stuff in it. This assignment is specifically meant to teach us about vectors and enumerations. A few people have told me what a bad idea this is and I understand why so I don't intend to do it this way outside of class.

  • InfidelInfidel Heretic Registered User regular
    The important thing is that you learn when and why to cast. You understand the situation here fully, yes? :^:

    OrokosPA.png
  • SarksusSarksus ATTACK AND DETHRONE GODRegistered User regular
    Yes, I get it now. I'll be keeping type safety in mind from now on and how that effects where I can do the cast. If I was thinking of type safety it would have been easier to figure out where to put the cast. I'll also remember the disadvantages of filling a vector of objects with different enumerations. This was a good lesson. I'm going to go through the program and comment it/clean it up now.

    Thank you :D

  • RendRend Registered User regular
    Okay guys. This saturday I am going to be competing in a competition as man 1 on a team of 3 programmers.

    This will be my first time doing competitive programming. 7 problems, 5 hours, thunderdome style. We'll be using Java. I have looked over a ton of the previous years' problems and I am confident in my ability to figure most of them out, but for anyone who has competed in the The Most Nerdiest Game, any advice?

This discussion has been closed.