Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VERY Easy C# Question

Posted on 2004-04-18
4
Medium Priority
?
232 Views
Last Modified: 2010-04-15
Hello, i just began playing with C#. I’m used to console C/C++ programming. I am simply wondering how I can pass a text string from one .cs form to another.cs

I have a typical login window, which consists of some textboxes, labels and buttons.


On click I want the event handler to pass the user name and password to a function in a different .cs file that is part of the project, one that holds the authenticate function logic.

I want to do something like this

Form1.cs
...
private void OK_Click(object sender, System.EventArgs e)
                  {
                   authenticate(UserName.Text, Password.Text);
                                                }
...

---------------------------
Authenticate.cs

...
authenticate(string username, string password)
                                {
            if( username == ...etc etc )
                                return etc;
                                }
...

Could some one please explain and show with a basic example how I could pass the text string.  

Thanks in advance! =)
0
Comment
Question by:FOXBAT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
sohilm earned 240 total points
ID: 10853676
C# is class based, so the above wouldn't work unless authenticate.cs is in its own class and if so it needs to be static so that it can be launched without declaring an instance.

The easiest solution would be for you to keep authenticate subroutine in the same class as the ok_click subroutine. Ex:

Class Form1.cs

{

    private void OK_Click(object sender, System.EventArgs e)
    {
       authenticate(UserName.Text, Password.Text);
     }            

      authenticate(string username, string password)
                                {
          if( username == ...etc etc )
                                return etc;
                                }
...

}

hope this helps.
0
 

Author Comment

by:FOXBAT
ID: 10858657
I need to separate the process logic from Form1.cs

i tried something like this but it didnt work, how can i state the retrun type, or in other words how do i get it to work?

Form1.cs
...
private void OK_Click(object sender, System.EventArgs e)
                  {
                                                 Class1 = new Class1(UserName.Text); }
...


------------------------
Class1.cs
...
public class Class1
      {               public int Check(string uname)
            {
                  if(uname == "aa")
                        return 1;
                  else
                        return 0;
            }
      }

0
 

Author Comment

by:FOXBAT
ID: 10858932
i read a book and got the right syntax

 Class1 obj = new Class1();
if(obj.Check(UserName.Text))...
0
 
LVL 3

Expert Comment

by:sohilm
ID: 10859191
you need to either declare an instance of the class or make it static. I see you know how to declare the instance, and the example below is for static.

public static class class1
{
  public static int Check(string uname)
     {
        if(uname == "aa")
           return 1;
         else
            return 0;
      }
}

now in your ok_click you can do this
   int Valid = Class1.Check(UserName.Text);

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question