Register for your free account! | Forgot your password?

You last visited: Today at 02:45

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Help in c#

Discussion on Help in c# within the .NET Languages forum part of the Coders Den category.

Reply
 
Old   #1
 
badguy4you's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 477
Received Thanks: 178
Help in c#

i want to read txtbox1.text from another class so i made this

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mains;

    public class iWebWrap
    {
        public void login()
        {
           OptimumMain om = new OptimumMain();
           string username = om.txtWebUser.Text;
           string password = om.txtWebPass.Text;


           MessageBox.Show(username);
           MessageBox.Show(password);
        }
    }
and changed the txtboxes modfiers to public but the problem is

even there is text in the textboxes the messagebox shows them empty
(idk what is the problem now)

UPDATE : HERE IS WHAT I WANT TO DO EXACTLY

What i really want to do is as the following

i have Form1 that has 2 textboxes and a button

and have login.cs which is the class that will handle the login process

so what i want to do is transfer the 2 textboxes values to the login class so the login class can determine whether the login info are correct or not
badguy4you is offline  
Old 07/08/2012, 14:23   #2
 
elite*gold: 42
Join Date: Jun 2008
Posts: 5,425
Received Thanks: 1,888
You made a new Object of the OptimumMain class, ofcoz they are empty
For this to work your OptimumMain class must be static, but well, thats not the way you should go. Pass ID and PW in your constructor for the IWebWrap class. If this is not possible for some reason, use a static data class or fire an event whenever txtWebUser
or txtWebPass text changes.

A textbox is private for a reason, you should not change it. Create a method to read it.
Code:
public string getID() {
      return txtWebUser.text;
}
MoepMeep is offline  
Old 07/08/2012, 17:25   #3
 
badguy4you's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 477
Received Thanks: 178
mmm i tried the way you told but still empty can you be a little more clear
badguy4you is offline  
Old 07/08/2012, 17:41   #4
 
elite*gold: 42
Join Date: Jun 2008
Posts: 5,425
Received Thanks: 1,888
Code:
public iWebWrap(string id, string pw) {
   this.id = id;
   this.pw = pw;
}

//And in your MainOptimum-Class
iWebWrap web = new iWebWrap("id","pw");
You should read some more about OOP.
MoepMeep is offline  
Old 07/08/2012, 19:10   #5
 
badguy4you's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 477
Received Thanks: 178
so there is no way to make the whole project as 1 thing so i can call any textbox any button any string from anywhere of the whole project ?

What i really want to do is as the following

i have Form1 that has 2 textboxes and a button

and have login.cs which is the class that will handle the login process

so what i want to do is transfer the 2 textboxes values to the login class so the login class can determine whether the login info are correct or not
badguy4you is offline  
Old 07/08/2012, 20:00   #6
 
elite*gold: 42
Join Date: Jun 2008
Posts: 5,425
Received Thanks: 1,888
Quote:
Originally Posted by badguy4you View Post
so there is no way to make the whole project as 1 thing so i can call any textbox any button any string from anywhere of the whole project ?

What i really want to do is as the following

i have Form1 that has 2 textboxes and a button

and have login.cs which is the class that will handle the login process

so what i want to do is transfer the 2 textboxes values to the login class so the login class can determine whether the login info are correct or not
Did you actually read my answer?
MoepMeep is offline  
Old 07/08/2012, 20:07   #7
 
badguy4you's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 477
Received Thanks: 178
man this.id is not declared ! what is that
what i do with id=id and what then when i called this with my login button click
iWebWrap web = new iWebWrap("id", "pw");

and i am not that bad with c# that i have to learn it but this thing just annoying
badguy4you is offline  
Old 07/08/2012, 20:09   #8
 
elite*gold: 42
Join Date: Jun 2008
Posts: 5,425
Received Thanks: 1,888
Quote:
Originally Posted by badguy4you View Post
man this.id is not declared ! what is that
nc.

Quote:
and i am not that bad with c# that i have to learn it but this thing just annoying
You know nothing about programming at all, start learning or quit.
MoepMeep is offline  
Old 07/08/2012, 20:10   #9
 
Coding Lounge's Avatar
 
elite*gold: 0
Join Date: Mar 2011
Posts: 806
Received Thanks: 160
Code:
//OptimumMain om = new OptimumMain();
           string username = OptimumMain.txtWebUser.Text;
           string password = OptimumMain.txtWebPass.Text;
And you have to declare 'username' and 'password' as a

Code:
public static username;
public static password;
Coding Lounge is offline  
Old 07/08/2012, 20:11   #10
 
badguy4you's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 477
Received Thanks: 178
MoepMeep Okey just stop posting here if you are insisting on that i dont wanna help from you if so
badguy4you is offline  
Old 07/08/2012, 20:12   #11
 
elite*gold: 42
Join Date: Jun 2008
Posts: 5,425
Received Thanks: 1,888
Quote:
Originally Posted by Coding Lounge View Post
Code:
[COLOR="Red"]OptimumMain om = new OptimumMain(); - DELETE[/COLOR]
           string username = OptimumMain.txtWebUser.Text;
           string password = OptimumMain.txtWebPass.Text;
This will just work if the whole class is static
MoepMeep is offline  
Old 07/08/2012, 20:16   #12
 
Coding Lounge's Avatar
 
elite*gold: 0
Join Date: Mar 2011
Posts: 806
Received Thanks: 160
And you have to declare 'username' and 'password' as a

Code:
public static username;
public static password;
Coding Lounge is offline  
Old 07/08/2012, 20:24   #13
 
badguy4you's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 477
Received Thanks: 178
Quote:
Originally Posted by MoepMeep View Post
This will just work if the whole class is static
Which whole class you mean ?

this

static class iWebWrap
{
string username = OptimumMain.txtWebUser.Text;
string password = OptimumMain.txtWebPass.Text;
}

still tells me An object reference is required for non static field ?
badguy4you is offline  
Old 07/08/2012, 20:26   #14
 
elite*gold: 42
Join Date: Jun 2008
Posts: 5,425
Received Thanks: 1,888
The OptimumMain class would have to be static, but as I told you, don't do it.
MoepMeep is offline  
Old 07/08/2012, 20:27   #15
 
badguy4you's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 477
Received Thanks: 178
... so what i do the code u provided is strange to me
badguy4you is offline  
Reply




All times are GMT +1. The time now is 02:45.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.