News:

Who uses forums anymore?

Main Menu

Ugh, Noob C# Question...

Started by abc, June 30, 2007, 08:57:17 PM

Previous topic - Next topic

0 Members and 5 Guests are viewing this topic.

abc

I'm sure this is the most pathetic question in C#...But I have a GUI on my form frmMain (go figure  :P) and then I have another class, that I want to control the text of frmMain, so I in frmMain.Designer.cs I made my GUI controls publics so I could alter the text. So in one of my voids, I put

frmMain Main = new frmMain();
Main.TabPage1.Text = "Blah";


To append text, I also use my AddChat function, which is in my frmMain.cs and nothing is working, I compile, and get no errors, nothing happens though...

I hope this wasn't too confusing.. I have such a headache..

Joe

I've got a neat solution but I can't attach it for some reason. Anyhow, IM me and I'll send it.
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


Ender

#2
Methods, not public fields :P aka data encapsulation. Granted, encapsulation can sometimes be a nuisance for things like setUsername() getUsername() etc., but in a situation like this, i.e. modifying the output on a GUI, I believe encapsulation is warranted.

I think learning some of the basics of OOP and software design would highly benefit.

Ender

Quote from: dlStevens on June 30, 2007, 08:57:17 PM
I'm sure this is the most pathetic question in C#...But I have a GUI on my form frmMain (go figure  :P) and then I have another class, that I want to control the text of frmMain, so I in frmMain.Designer.cs I made my GUI controls publics so I could alter the text. So in one of my voids, I put

frmMain Main = new frmMain();
Main.TabPage1.Text = "Blah";


To append text, I also use my AddChat function, which is in my frmMain.cs and nothing is working, I compile, and get no errors, nothing happens though...

I hope this wasn't too confusing.. I have such a headache..

Sounds like you're talking to the wrong object, i.e. you're instantiating a new object different from the one that displays your GUI, and telling that object to addChat, modifying variables that never come into play. You have to pass a reference around your program to the original GUI object (there really should be no more than one). There are many ways to do this, such as passing by constructors, passing by methods, Singletons, etc.

Joe

Quote from: Ender on July 01, 2007, 05:02:19 AM
Methods, not public fields :P aka data encapsulation. Granted, encapsulation can sometimes be a nuisance for things like setUsername() getUsername() etc., but in a situation like this, i.e. modifying the output on a GUI, I believe encapsulation is warranted.

I think learning some of the basics of OOP and software design would highly benefit.

private string c_sUsername;
public string Username
{
    set { this.c_sUsername = value; }
    get { return this.c_sUsername; }
}
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


MyndFyre

You need to learn object-oriented programming, as Ender said, before you try to tackle a bot or something similar in C#.
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

Hdx

Quote from: Joex86/64] link=topic=9724.msg123523#msg123523 date=1183309102]
private string c_sUsername;
public string Username
{
    set { this.c_sUsername = value; }
    get { return this.c_sUsername; }
}

Sorry to hijack this a bit.
Does this method work in java? (I'm at the library, no place to test)
I've never seen it done in java so.. ?
If it does this will help emensly in memory consumption of things i'm writing.
~Hdx
http://img140.exs.cx/img140/6720/hdxnew6lb.gif
09/08/05 - Clan SBs @ USEast
[19:59:04.000] <DeadHelp> We don't like customers.
[19:59:05.922] <DeadHelp> They're assholes
[19:59:08.094] <DeadHelp> And they're never right.

Warrior

Quote from: HdxBmx27 on July 03, 2007, 02:14:23 PM
Quote from: Joex86/64] link=topic=9724.msg123523#msg123523 date=1183309102]
private string c_sUsername;
public string Username
{
    set { this.c_sUsername = value; }
    get { return this.c_sUsername; }
}

Sorry to hijack this a bit.
Does this method work in java? (I'm at the library, no place to test)
I've never seen it done in java so.. ?
If it does this will help emensly in memory consumption of things i'm writing.
~Hdx

It should, or at least a variant of it. Most OO programming languages should have Property modifiers. (Hell, even VB has them)
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Ender

Java doesn't have property modifiers like that. I haven't tested it but I'm 99.9% sure :P

Warrior

You're right, Java isn't as advanced as C#. =/
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Joe

We could reverse that on you and say that C# is more complicated than Java and a pain in the ass to use. Please leave your bias at the door.

@Hdx: No. If you just do set and get methods though it shouldn't be too intensive. Perform some sanity checks where needed if you want a little edge.
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


Warrior

#11
Quote from: Joex86/64] link=topic=9724.msg123891#msg123891 date=1183812929]
We could reverse that on you and say that C# is more complicated than Java and a pain in the ass to use. Please leave your bias at the door.

@Hdx: No. If you just do set and get methods though it shouldn't be too intensive. Perform some sanity checks where needed if you want a little edge.

I can reverse this and say you're an idiot. Try to leave that at the door.
Also, who's "we" you're one person.

Shut up.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

MyndFyre

Quote from: Joex86/64] link=topic=9724.msg123891#msg123891 date=1183812929]
We could reverse that on you and say that C# is more complicated than Java and a pain in the ass to use. Please leave your bias at the door.
Not really.  Here's why.

C# is a superset of Java.  Everything that you can do in Java, you can do in C# with minimal modification (such as type name changes).  A java class will be valid C#.

However, C# supports treating primitives like objects.  Java's type system does not.  In Java you have (as types that can be represented by literals):


byte
short
int
long
float
double
Object
|-> String


In C# you have:

Object
|-> byte
|-> sbyte
|-> short
|-> ushort
|-> int
|-> uint
|-> long
|-> ulong
|-> float
|-> double
|-> decimal
|-> string

(Technically, all of the numerics in C# inherit from System.ValueType, but that's an internal compiler detail since ValueType isn't really used in any instance ever).

Because in C# all primitives are actually Objects, we can do something like so:

ArrayList al = new ArrayList();
al.Add(1);
al.Add(4485);
al.Add("This is a string.");

In Java, you can't; you have to provide the class wrapper (except in the case of strings):

al.add(new Integer(1));
al.add(new Integer(4485));
al.add("This is a string.");

Also, because C# supports overloaded operator this[], ArrayList can be treated like an array, which makes its syntax cleaner.  Example:

int val = (int)al[0];
int val2 = (int)al[1];
string val3 = al[2] as string;

In Java you'd have to:

int val = ((Integer)al.get(0)).intValue();
int val2 = ((Integer)al.get(1)).intValue();
String val3 = (String)al.get(2);

Note also that the semantic difference between the C# and Java implementations of getting "val3" are important.  In C#, if the third ArrayList element is not a String-compatible type, val3 will be null.  In Java, because you're forced to do typecasting in this way, you run the risk of generating a ClassCastException.  If you cast that way in C# you can generate an InvalidCastException as well.  The point is that you don't need to cast reference types in a way that generates an unnecessary exception.

I haven't even begun to talk about all other ways that Java is a pain in the ass - such as the necessity of an additional class where function pointers would suffice (Runnable, for example, vs. a ThreadStart calback in C#).
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

Warrior

I can tell you enjoyed that.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Hitmen

Quote from: MyndFyrex86/64] link=topic=9724.msg123915#msg123915 date=1183844796]
Because in C# all primitives are actually Objects, we can do something like so:

ArrayList al = new ArrayList();
al.Add(1);
al.Add(4485);
al.Add("This is a string.");

In Java, you can't; you have to provide the class wrapper (except in the case of strings):

al.add(new Integer(1));
al.add(new Integer(4485));
al.add("This is a string.");


I know this post is a bit old, but I just want to point out that this is entirely wrong. Clicky.

Though currently the code should look something like this:

ArrayList<Object> al = new ArrayList<Object>();
al.add(1);
al.add(4485);
al.add("This is a string.");
Quote
(22:15:39) Newby: it hurts to swallow