• Hey Guest. Check out your NeoGAF Wrapped 2025 results here!

Your Programming FUCK YEAH moments.

Status
Not open for further replies.
Yeeha, finished Euler 1 (by forcing myself to use a class to calculate).

Only 343 exercises to go.
 
Toma said:
Its seems like a lot is... I wouldnt say an unneccessary burden, but I tend to fall back to use ways NOT OOP even when trying to exercise OOP. Somewhere on this page I wrote that I used static methods to do stuff in my class and people just told me that with that I was basically undoing what OOP is for. Sigh.

Anyway, what the people were saying about it, already helped a great deal. Now I only need to concentrate on trying to use OOP concepts, but its hard if you programmed a while before without using OOP.
It can feel that way, but once it clicks, it really clicks. Even for simple projects what you may lose in time actually implementing OOP as opposed to functional you gain back in modularity and versatility.

With that being said, I'm still definitely a beginner. I have a feeling the next big thing that's going to fundamentally change how I work with C++ is learning how to use a hash table.
 
- Making my first demos on GameMaker
- Making my own languages parser/interpreter in scheme
- Launching a fully working site in hours thanks to RoR/Heroku
- The first (successful) attempts at metaprogramming
 
When I wrote my first OOP PHP webpage! FUCK YEAH! I almost cried!

Code:
<?php
  require("page.inc");

  $homepage = new Page();

  $homepage->content ="<h2>Blah Blah Title</h2>
                       <p>More Blah Blah...</p>
                       <p>Even More Blah Blah...</p>";
  $homepage->Display();
?>
 
I had an introductory numerical methods/computer applications course last semester. 99% of it was learning to use various interpolation methods with MATLAB and on paper, like various forms of Euler, Gauss-Seidel, Jacobi, and Runge-Kutta. We were basically instructed to use as many kludges as we needed to for the sake of understanding the method and solving the problem, as long as our scripts met the requirements and weren't too horribly inneficient. Anyway, one of my assignments, using cubic splines if I recall, was a real mess. The example scripts and notes weren't very helpful, and I stayed up very, very late trying to get it to work, but to no avail. I got up early the next morning, looked over my notes again, then slapped myself and re-wrote the entire thing in about 25 minutes. It worked perfectly and I got a great score on that assignment.
 
Okaaay I think I am slowly wrapping my head about how to use classes. However, I still have problems with how I can initialize multiple instances.

For example:

Code:
                Team[] TeamArray = new Team[TeamCount];
                TeamArray[counter] = new Team(elem);

seems to work, while

Code:
                List<string> TeamListe = new List<string>();
                TeamListe[counter] = new Team(elem);

doesnt. (Cannot implicitly convert type 'soccer_table.Team' to 'string')

Can I use lists to initiate instances of objects? I'd like to sort them later on, which is why an array seems a bit stupid.
 
Getting a splash screen to show up on the DS for the first time. That was a huge FUCK YEAH! moment for me.
 
deadbeef said:
Toma,

Do this:

List.Add(new Thing())

If I do that:
Code:
                List<string> TeamListe = new List<string>();

                TeamListe.Add(new Team(elem));

I get the same error (Cannot implicitly convert type 'soccer_table.Team' to 'string') plus a new one : "The best overloaded method match for 'System.Collections.Generic.List<string>.Add(string)' has some invalid arguments"

Edit: I am stupid, I need to change "List<string>" to List<whatever I am adding> right? But what am I adding? Its not a string, I know that much.

Edit2: Ha! Its an object! List<object>! HA!
 
Toma said:
If I do that:
Code:
                List<string> TeamListe = new List<string>();

                TeamListe.Add(new Team(elem));

I get the same error (Cannot implicitly convert type 'soccer_table.Team' to 'string') plus a new one : "The best overloaded method match for 'System.Collections.Generic.List<string>.Add(string)' has some invalid arguments"

Edit: I am stupid, I need to change "List<string>" to List<whatever I am adding> right? But what am I adding? Its not a string, I know that much.

Edit2: Ha! Its an object! List<object>! HA!
Crap, I completely missed that one.


Object is the base class in C# - everything inherits from an object. Or everything "is a" object. You need to be more specific or else you're going to have to cast everything you remove from the list to the type you want.

Make it a list of Team objects if that's what you are adding.

List<Team> myList = new List<Team>();
Team badNewsBears = new Team();
myList.add(badNewsBears);

// With your method, if you do this you will not be able to convert it to a Team without casting
Team foo = myList[0];

Team bar = (Team)myList[0];
 
Toma said:
Edit: I am stupid, I need to change "List<string>" to List<whatever I am adding> right? But what am I adding? Its not a string, I know that much.

Edit2: Ha! Its an object! List<object>! HA!

It's a Team. You are adding instances of the "Team" class no?
 
deadbeef said:
Crap, I completely missed that one.

Margalis said:
It's a Team. You are adding instances of the "Team" class no?

Thanks both of you. Glad you told me its not an "object". Otherwise I would have stuck with it to the bitter end. 2 steps forward, 1,99999 steps back for me, but hey at least I am learning a bit every time.
 
Code:
Team[] TeamArray = new Team[TeamCount];
TeamArray[counter] = new Team(elem);
I'm still getting used to Java, and to a lesser extent some OO stuff in general. The first command makes an array of Team, thats obvious. The second line replaces the Team at "counter" with a new Team who's constructor is fed "elem"?

Its just a bit weird for me because I'm used to "new" being used for memory allocation, but by the time of the second line the memory for the entire array already exists?
...and I don't know if I'm making any sense.
 
The_Technomancer said:
Code:
Team[] TeamArray = new Team[TeamCount];
TeamArray[counter] = new Team(elem);
I'm still getting used to Java, and to a lesser extent some OO stuff in general. The first command makes an array of Team, thats obvious. The second line replaces the Team at "counter" with a new Team who's constructor is fed "elem"?

Its just a bit weird for me because I'm used to "new" being used for memory allocation, but by the time of the second line the memory for the entire array already exists?
...and I don't know if I'm making any sense.

Yup, you cant directly change the sizes of arrays in c# as far as i understand. So I read out the count of items I have and created an array with "TeamCount" as the complete end size for the array, which I am then filling in the second line.

(Hope I am not too wrong, beginner here.)
 
Toma said:
Yup, you cant directly change the sizes of arrays in c# as far as i understand. So I read out the count of items I have and created an array with "TeamCount" as the complete end size for the array which I am then filling in the second line.

(Hope I am not too wrong, beginner here.)
Yeah, I just don't really see why you have to use "new" in the second line when you're overwriting an already existing element, but it could be that I'm just thinking about "new" the wrong way.
 
First fuck yeah programming moment? I was probably 10, and got a C64. I typed in a huuuuuge program from a magazine (in reality, it was probably like 30 lines of code) to prank my family, the best part was figuring out how to change it to accommodate my family / friends. Let me recreate the moment:

HELLO, WHAT IS YOUR NAME? > _

(Older sister looks at it and types)

LARISSA_

> HI LARISSA, I KNOW YOU LET ALL THE BOYS KISS YA

Of course it's ridiculous now, but at age 10 it was MAGIC. As she proceeded to beat the crap out of me, I knew I wanted to be a programmer.
 
The_Technomancer said:
Yeah, I just don't really see why you have to use "new" in the second line when you're overwriting an already existing element, but it could be that I'm just thinking about "new" the wrong way.

Because I am creating a new instance of the object "Team", which (creating new instances I mean) always uses the "new" operator, instead of just replacing the array with a string or number. Hope that made sense :D

If you'd use a string to replace the content it would be just:

TeamArray[counter]="lala";
 
Toma said:
Because I am creating a new instance of the object "Team", which (creating new instances I mean) always uses the "new" operator, instead of just replacing the array with a string or number. Hope that made sense :D

If you'd use a string to replace the content it would be just:

TeamArray[counter]="lala";
...I...think so, yeah, I'm really getting hung up on how I'm used to "new" working in a different context, but it makes sense now.
 
The_Technomancer said:
Code:
Team[] TeamArray = new Team[TeamCount];
TeamArray[counter] = new Team(elem);
I'm still getting used to Java, and to a lesser extent some OO stuff in general. The first command makes an array of Team, thats obvious. The second line replaces the Team at "counter" with a new Team who's constructor is fed "elem"?

Its just a bit weird for me because I'm used to "new" being used for memory allocation, but by the time of the second line the memory for the entire array already exists?
...and I don't know if I'm making any sense.
You first allocate memory for an array of pointers, but the pointers themselves only contain values that are basically memory addresses. You need to then allocate memory for whatever it is that is being referenced by the pointers unless you want them to be null references (or you could point them at some existing object).

(IIRC in languages like Pascal and Modula derived languages like Ada, arrays of records will be allotted memory for the whole shebang although you can define array of pointers to records.)
 
Lance Bone Path said:
You first allocate memory for an array of pointers, but the pointers themselves only contain values that are basically memory addresses. You need to then allocate memory for whatever it is that is being referenced by the pointers unless you want them to be null references (or you could point them at some existing object).
Yeah, but all that should be handled by the first line, no?
Using the constructor in the second line to overwrite one of the elements of the array with a new object of the same class shouldn't take any more memory, does it?
 
The_Technomancer said:
Yeah, but all that should be handled by the first line, no?
Using the constructor in the second line to overwrite one of the elements of the array with a new object of the same class shouldn't take any more memory, does it?
The first line just creates enough space for an array of values that contain addresses. The actual minimum required memory for an array that contains your objects would be n * <memory address value> size + n * <array object> size, but you've only allocated n * <memory address value> size with the first line (where n equals TeamCount).

The next line is allocating memory for an object that your pointer (TeamArray[counter]) will reference.
 
Lance Bone Path said:
The first line just creates enough space for an array of values that contain addresses. The actual minimum required memory for an array that contains your objects would be n * <memory address value> size + n * <array object> size, but you've only allocated n * <memory address value> size with the first line.

The next line is allocating memory for an object that your pointer (TeamArray[counter]) will reference.
Oh, really? So you have to actually construct each object in the array separately? I would have assumed that it would actually make that many objects using the default constructor.
 
The_Technomancer said:
Oh, really? So you have to actually construct each object in the array separately? I would have assumed that it would actually make that many objects using the default constructor.
I have to say I didn't look that carefully at the original post. I assumed it was java (which doesn't have a default constructor).

[edit]I meant implicit, not default.
 
Lance Bone Path said:
I have to say I didn't look that carefully at the original post. I assumed it was java (which doesn't have a default constructor).
I'm still trying to make sense of this from a C++ background with very little experience in Java, but there's no default constructor?

*shrug* learn something new every day.
 
The_Technomancer said:
I'm still trying to make sense of this from a C++ background with very little experience in Java, but there's no default constructor?

*shrug* learn something new every day.

There is a parameterless constructor in Java, but there is no guarantee that a class would have it.

Language designers usually try to follow the "Principle of least astonishment". Filling the array with default constructed classes would astonish me. Especially if my class didn't have a default constructor.
 
mike23 said:
There is a parameterless constructor in Java, but there is no guarantee that a class would have it.

Language designers usually try to follow the "Principle of least astonishment". Filling the array with default constructed classes would astonish me. Especially if my class didn't have a default constructor.
Yeah, it makes more sense the more I think about it, from an efficiency perspective.
 
Alright, question time! Looking to understand the following code for a Hash table:
Code:
 int get(int key) {
            int hash = (key % TABLE_SIZE);
            while (table[hash] != NULL && table[hash]->getKey() != key)
                  hash = (hash + 1) % TABLE_SIZE;
            if (table[hash] == NULL)
                  return -1;
            else
                  return table[hash]->getValue();

I understand everything except why
Code:
table[hash] != NULL
this is a condition. If I'm reading this right when it makes the while check it moves to the next slot if the keys mismatch (which makes sense as a check against the hash function screwing up) but I don't get why you only move up a slot if the entry !=NULL.

The way I think this works, it only moves on if the keys mismatch and the mismatched entry is full.
So then won't it return -1 if the keys mismatch but the entry is NULL? Why would you want that? Why wouldn't you always want to skip over entries where the keys mismatched?

EDIT: Wow, nevermind. It makes total sense once the entries in the table become linked lists.
 
Back in School my team of 5 had to create a working game, we had 3 months. Each person was in charge of different aspects. I took on the task of the Particle System, 2d Enviroment and gameplay and 2d to 3d transition system.

First fuck yeah moment during those 3 months was getting the Particle system to actually display correctly on the screen. We were using Renderware as our back end and basically had zero documentation for it outside of the basic API docs, and we really couldn't go on the boards to ask how to fix some problems. Basically the particles were always only facing one direction and you would only get the alpha blend from 1 direction making them look awesome from 1 spot and aweful from another. After alot of tinkering and coming up with different methods I was able to fix it.


But I would say my proudest moment was the day before we had our Project presentation. The person who was in charge of making the Sound system did a complete and total PATHETIC job at it, looked like he copied and pasted shit from the net actually, and it was his only job in the 3 months. I sat down in 1 night(probably about 10hours no sleep) and created the entire Sound/Music handling system and implemented it into the project.. Once it was done I felt high as a fucking kite.
 
The_Technomancer said:
So then won't it return -1 if the keys mismatch but the entry is NULL?

I don't think this is possible... if table[hash] is null then there is no key at that index to be mismatched. I imagine table[hash] would only be null when you reach the end of the array, but even that is not the case you certainly can't call table[hash]->getKey if table[hash] is null
 
Okay, sorry for bothering you all again..

I cant get calling methods from another class working.

Form1 is opening a window, in that I start another class from which i want to give the order to Form1 to write something in a box.

From what I understand I'd need to initialize a form before I can access it, right? Like

Code:
Form Form2 = new Form();
Form2.TextBox1("aaaa");

But the form I want to access is already initiated. Either I can access it right away or I need a way to get back to it, but I cant wrap my head around how.
 
Form.text just sets what the form is called if I remember right (in the top left), I don't think that's what you're looking for. If you're wanting text to appear inside the form you will need to use a textbox or a label.

I may be interpreting what you're doing wrong though.

Form.text is usually just set through the 'properties' window and put into the 'designer' code automatically by the compiler.
 
Kalnos said:
Form.text just sets what the form is called if I remember right (in the top left), I don't think that's what you're looking for. If you're wanting text to appear inside the form you will need to use a textbox or a label.

I may be interpreting what you're doing wrong though.

Form.text is usually just set through the 'properties' window and put into the 'designer' code automatically by the compiler.

Yeah I changed that already after I noticed my mistake.
My problem is that I cant access Form1.TextBox from another class. I'd need to call a method inside Form1, for example
Code:
Different Class
Form1.InputString("aaaa")

Code:
Form1
public void InputString(string Input)
{
TextBox1(Input);
}

But when I try to do that from another class, it gives me this error:
An object reference is required for the non-static field, method, or property 'blabla'

Which is the error I am normally getting when I try to access an object without making an instance of it before.

It feels like something VERY substantial.
 
Toma, you're calling Form1.Method(string) (I.E. the class, not the instance) version of that method rather than the actual instance of the form. It's expecting a static method because that's the only way you could do that.

You need to reference the actual form, not the class.
 
deadbeef said:
Can you please clarify what you are trying to do?

Sorry if I cant explain it well enough.
I have a Form open, and want to access methods of that window in a different class.

Form1.cs(the open window):

Code:
public void UpdateLeague(string Team, int Points, int GoalsPlus, int GoalsMinus)
        {

        }

...which i want to access from a different class

League.cs:

Code:
Form1.UpdateLeague("lala",6,6,4);

iyfup5.jpg


Sorry, if I dont use the correct terms for something =/
 
Kalnos said:
Toma, you're calling Form1.Method(string) (I.E. the class, not the instance) version of that method rather than the actual instance of the form. It's expecting a static method because that's the only way you could do that.

You need to reference the actual form, not the class.

Ah, but the instance of that Form was created on startup, how I find out how this exact instance is called?
 
Toma said:
Ah, but the instance of that Form was created on startup, how I find out how this exact instance is called?

Let me ask a few things:

League_Simulation = Form1.
League_Input = Form 2. (created when pressing Create League?).

EDIT: Fuck all of that, I'm even more retarded than I thought.

Just pass a string by reference into Form2.
 
Toma said:
Sorry if I cant explain it well enough.
I have a Form open, and want to access methods of that window in a different class.

Form1.cs(the open window):

Code:
public void UpdateLeague(string Team, int Points, int GoalsPlus, int GoalsMinus)
        {

        }

...which i want to access from a different class

League.cs:

Code:
Form1.UpdateLeague("lala",6,6,4);

http://i55.tinypic.com/iyfup5.jpg

Sorry, if I dont use the correct terms for something =/


In this type of situation, here is the approach I usually take. Form1 is your display form, where you display the league. You want a different form for editing the league. This is fine. But first things first. You need to learn to label the things in your form meaningfully. For example, Form2 is your LeagueEditor. Go ahead and call it that. You can modify the name of the class in the properties window of the Design View. By calling your Forms and your TextBoxes and everything something meaningful, the code becomes much more understandable.

So, I'm going to call Form1 your LeagueSimulation and Form2 your LeagueEditor. This way, when you instantiate a LeagueEditor, you will do this:

Code:
LeagueEditor editor = new LeagueEditor()

Okay, so now for the design of the solution.

LeagueEditor needs to hold the list of league teams. LeagueSimulation (and perhaps other forms) will need access to this league. So make your LeagueEditor have a public method called getTeams as follows:

Code:
public List<string> getTeams()
{
   // Good stuff goes here
}

Now, inside the guts of LeagueSimulation, when it is time for the user to click a button (Create League button perhaps), you will need to do this:

1. Instantiate a LeagueEditor.
2. Allow the user to enter the league teams.
3. When the user is done, harvest the teams from the LeagueEditor.
4. Update the League Table textbox in your LeagueSimulation.

One important thing to note here is that a Form can have something called a DialogResult. This is an enumeration that tells the result of an interaction with a Form, and some of the results are things like DialogResult.OK or DialogResult.Cancel. You can assign a button on a Form a DialogResult. In your case, you can assign a DialogResult of OK to the Done button on LeagueEditor.

Now, when the user clicks the "Create League" button, you need to do something like this:

Code:
// Inside create league click event
LeagueEditor editor = new LeagueEditor();

if(editor.ShowDialog() == DialogResult.OK)
{
  // User interacted with league editor and clicked Done.  
  // This means he/she is done editing a league.  Time to harvest.
  List<string> teams = editor.getTeams();

  // Fill LeagueSimulation TextBox with teams however you want.
}

MORE incoming....

The idea is this. LeagueEditor allows the user to enter the teams. Its state data is the list of teams. Other forms need access to that data, so provide an accessor function so that they can get the data. Then, when you need to get the data, you can instantiate a LeagueEditor, wait until the user is done (which you find out by treating the form like a Dialog as shown above) and then harvesting the input.

EDIT: Changed to a better definition of "state data"
 
Kalnos said:
Let me ask a few things:

League_Simulation = Form1.
League_Input = Form 2. (created when pressing Create League?).

EDIT: Fuck all of that, I'm even more retarded than I thought.

Just pass a string by reference into Form2.

Never did that. How would that work? And what would it exactly do?

And yeah, Form 2 is created when pressing Create League.

deadbeef said:

Edit: just saw your response, thanks :) Will take me a while to understand and try out what you wrote.
 
Getting your first app running on an iphone or Android (or any other smartphone) is pretty cool. You think to yourself - maybe it's not so difficult after all.
 
Toma said:
Never did that. How would that work? And what would it exactly do?

And yeah, Form 2 is created when pressing Create League.



Edit: just saw your response, thanks :) Will take me a while to understand and try out what you wrote.

It is common practice to have an OK button that has a DialogResult of OK and a Cancel button that has (surprise) a DialogResult of Cancel.

By treating LeagueEditor like a Dialog, it becomes "modal", which for your user means that they can't interact with the rest of your application until they get done with editing the list of teams. You get this behavior by using the ShowDialog method.

If you want the user to be able to have both the League Editor and the League Simulation windows open at the same time, then you need to use the Show method. But that will introduce additional problems for you that I don't think you really need to deal with at this time.
 
deadbeef said:
It is common practice to have an OK button that has a DialogResult of OK and a Cancel button that has (surprise) a DialogResult of Cancel.

By treating LeagueEditor like a Dialog, it becomes "modal", which for your user means that they can't interact with the rest of your application until they get done with editing the list of teams. You get this behavior by using the ShowDialog method.

If you want the user to be able to have both the League Editor and the League Simulation windows open at the same time, then you need to use the Show method. But that will introduce additional problems for you that I don't think you really need to deal with at this time.

Looooots of thanks for writing that up. Seriously.
 
His answer is much more elegant, but you should probably understand passing by value/by reference also:

Code:
class 
{
    void Add(int x)
     {
         x = x + 1;
     }
}

int Main()
{
   int x = 3;
   Class c = new Class();

   c.Add(x);
   Console.WriteLine(x);
   c.Add([B]ref[/B] x);
   Console.WriteLine(x)
}

Printout:
3
4

The difference is that the first call of the method passes x by value, meaning it only passes x's value into the function and thus x itself is not changed after the method is through executing. The second call of the method passes x by reference so that everything that happens to x during the duration of the method is kept outside of the method's scope (thus the value of x actually changes to 4).

In your case, I was suggesting that you keep a string in class Form1 and pass that string by reference into Form2 when creating that form, then when you accept the user input you simply mutate that string (which Form1 is also able to see).
 
Kalnos said:

Thanks you too :) It will probably take me a few days to understand and practice what both of you wrote, so I cant comment on whether your explanations make problems for me now or not.
 
0okHt.jpg

Thats sooo worth at least a small F. Yeah.

Now I only need to practice using it without your awesome explanations, but I think I got the main idea.

I tried the ref stuff as well, but didnt work out for some reason, but thats not a concern for now. I'll try looking into that later.
 
I fondly remember the time I got my old Amiga's copper to write blitter programs to draw 3D stuff with motion blur while the CPU was doing more interesting things.

Actually, I get my FUCK YEAH moments pretty much any time my code works (I work with Python all day so it's all win), but sadly most of the time the results aren't as fun as the old Amiga stuff :-)
 
Status
Not open for further replies.
Top Bottom