Using System.Text but can't use some classes within it!

martynball

Member
Joined
Jul 8, 2014
Messages
14
Programming Experience
Beginner
Okay, here is the top section of my form which includes the required namespaces?

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

Here is some code that checks if the email is valid.
C#:
private void DoLogin_Click(object sender, EventArgs e)
        {
            string GetEmail = LoginEmail.Text;
            System.Text.RegularExpressions.Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
            Match match = regex.Match(GetEmail);


            if (match.Success)
            {
                MessageBox.Show(GetEmail + " is correct");
            }
            else
            {
                MessageBox.Show(GetEmail + " is incorrect");
            }
        }

This isn't working as it's saying:
C#:
Error    1    The type or namespace name 'Regex' could not be found (are you missing a using directive or an assembly reference?)    C:\Users\Martyn Ball\documents\visual studio 2013\Projects\JamSnaps\JamSnaps\Database Testing.cs    244    62    JamSnaps

Now it works like this:
System.Text.RegularExpressions.Regex regex

But I though that because I have included "System.Text" at the top I wouldn't need to reference all of the classes?
 
Importing System.Text means that you can use members of that namespace unqualified, but Regex is not a member of System.Text. Your own code tells you what namespace it belongs to:
C#:
[B][U]System.Text.RegularExpressions[/U][/B].Regex regex
 
Importing System.Text means that you can use members of that namespace unqualified, but Regex is not a member of System.Text. Your own code tells you what namespace it belongs to:
C#:
[B][U]System.Text.RegularExpressions[/U][/B].Regex regex


Ooooh, so I can type "RegularExpressions.Regex regex" and it will work? As the first part "System.Text" is already imported?
 
Ooooh, so I can type "RegularExpressions.Regex regex" and it will work? As the first part "System.Text" is already imported?
In VB you can do that but, if I remember correctly, C# doesn't support partial namespaces like that. What you probably need to do, and what would be most sensible to do even if you don't need to, is import the System.Text.RegularExpressions namespace. You may not even need to import System.Text unless you're using other types that are direct members of that namespace.
 
Back
Top Bottom