Jump to content

Learning to use c# - Make a email csv for google


Recommended Posts

Posted

From a previous thread Im trying to lean c#

 

I have watched a few videos and read some chapters of books and was thinking of trying a little project to learn by doing something useful.

Im using Windows wpf application is this the best for the solution.

I want to take a csv file "firstname","Surname" then amend it to be

"firstname","Surname", "firstititial.surname+schoolemail variable", "password(colour+random4 digit number)"

WFP seams a bit of a pain at adding new classes (can you do this?) but thought its the best solution for the project rather then a console app, and that it would be a good way of learning how to interact with files.

I have created a window with a button that opens up a file picker this then writes it into a textfeild ( I have this working)

I then have another button that when clicked gets the textbox string and sends it to a readfile method which at the moment just outputs it to the console ( mainly to test to see if it was reading the file) I understand everything that I have done so far.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;

namespace EmailCreator
{
   /// 
   /// Interaction logic for MainWindow.xaml
   /// 
   public partial class MainWindow : Window
   {
       
       public MainWindow()
       {
           InitializeComponent();
       }

       private void BtnFileOpen_Click(object sender, RoutedEventArgs e)
       {
           var fileDialog = new System.Windows.Forms.OpenFileDialog();
           var result = fileDialog.ShowDialog();
           switch (result)
           {
               case System.Windows.Forms.DialogResult.OK:
                   var file = fileDialog.FileName;
                   TxtFile.Text = file;
                   TxtFile.ToolTip = file;
                   break;
               case System.Windows.Forms.DialogResult.Cancel:
               default:
                   TxtFile.Text = null;
                   TxtFile.ToolTip = null;
                   break;
           }
       }

       private void Run_Click(object sender, RoutedEventArgs e)
       {
           string csv = TxtFile.Text;
           Readfile(csv);
       }

       private void Readfile(string csv)
       {
           int counter = 0;
           string line;
           System.IO.StreamReader file =  new System.IO.StreamReader(csv);
           while ((line = file.ReadLine()) != null)
           {
               Console.WriteLine(line);
               counter++;
           }

           file.Close();

           // Suspend the screen.
           Console.ReadLine();
       }



   }
}

 

I know I have to modify the readfile method.

 

From my understanding I need to turn each line into an object or would a list be better so that I can amend the ending? or would you use something like linq.

 

Dont want you to do it, just a poke in the direction that you would attempt this.

 

My thoughts were

 

method to readfile and convert each line to a object

method to generate random password

method to generate the email address from a variable( textbox and the object)

method to amend the object to desired format

method to write objects to new csv file

 

?

Posted

"firstititial.surname+schoolemail variable", "password(colour+random4 digit number)"

 

Is the School Email in the CSV as a column? is the colour a random colour as well as the random 4 digits?

Posted

Thanks,

No the csv that I select will have headers of "firstname" and "surname" only followed by the names underneath.

 

The colour will be random to from a list that I have created

Posted
Thanks,

No the csv that I select will have headers of "firstname" and "surname" only followed by the names underneath.

 

The colour will be random to from a list that I have created

 

 

What I'd probably do (because I haven't used Linq much) is declare a list, then for each line split the string (using ',' or whatever the delimiter), then while still in this block format the strings you need (get the first char of surname etc) before adding them to the list. Outside of the reader block, generate the password and email and add both of those vars to the list as well.

 

 

Once the list has got the data you need, output it all using String.Join and the delimiter again (remember the quotation marks!)

Posted (edited)

For a simple task like this, I'd read each line in, create an instance of an object (specified in its own class), generate password, generate email, add the object to that list, then iterate through that list, writing the data out in the format you want.

 

Whether you do each thing as a method is up to you - that's more of a coding style decision. If you want to do it properly, so you can easily maintain the code, that would be a good idea though.

 

Adding a class in Visual Studio is simply a case of right clicking in the project explorer and choosing Add -> Class.

Edited by localzuk
Posted

OK so Im getting further with this making methods etc. I know think I have a OOP problem

in my main section I have

 private void Run_Click(object sender, RoutedEventArgs e)
       {
           
           correctFileType(setCsv()); // does not exist in current context??
           Readfile(setCsv());
       }
       // Helper methord
       private string setCsv()
       {
           string csv = TxtFile.Text;
           return csv;
       }

 

I have created a class in the same stack

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmailCreator
{
   public class Checkfiletype
   {
       private bool correctFileType(string myFile)
       {
           bool isValidFileType = false;
           if (myFile.Length <= 5)
           //if x.csv eg
           {
               string myFileType = myFile.Substring(myFile.Length - 3, 3);
               if (myFileType.Contains("csv") || myFileType.Contains("txt"))
               {
                   isValidFileType = true;
               }
               //else isValidFileType is false
           }
           //else isValidFileType is false
           return isValidFileType;
       }
   }
}

 

Why cant I call that methord

Posted (edited)

Two problems: first of all, correctFileType() is private, meaning it's inaccessible outside of the class. Change the scope to 'public' if you want access outside the class.

 

Secondly, to call a method from a class, either the method needs to be made static (simply add the word 'static' after the scope) or you need to create an instance of the class. Either way, you'll still then have to call the method via it's object/class.

 

If this class only contains that one method, I'd be tempted to put the method in to my main body of code to be honest - not much use creating classes you don't intend to use as objects in your program or reuse in other projects IMO.

 

So to get that working as a static method, you would change the class to this:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmailCreator
{
   public class Checkfiletype
   {
       public static bool correctFileType(string myFile)
       {
           bool isValidFileType = false;
           if (myFile.Length <= 5)
           //if x.csv eg
           {
               string myFileType = myFile.Substring(myFile.Length - 3, 3);
               if (myFileType.Contains("csv") || myFileType.Contains("txt"))
               {
                   isValidFileType = true;
               }
               //else isValidFileType is false
           }
           //else isValidFileType is false
           return isValidFileType;
       }
   }

 

Then call the method like this:

 

Checkfiletype.correctFileType(setCsv());

 

EDIT for clarity: when you create a class, your aim is usually to represent some object that you'd like to use repeatedly in your code. All of the methods and variables contained in that class will become methods/variables of the objects you create based on that class. For instance, if you wanted to create a new instance of (object based on) the Checkfiletype class, you could do something like this:

 

Checkfiletype checkFile = new Checkfiletype();

 

Now, you have a new object called checkFile, which has all the methods and variabels declared in the Checkfiletype class, allowing you to access them via the object (e.g. checkFile.correctFileType("some path"))

 

If you created another instance of the class, that second instance would also have all the same variables and methods, but it would work independently of your first instance. This is the power of OOP. You now have two objects based on the same class/template, but that can store totally separate data.

 

An exception to this is when a method/variable is made static. In this case, that method/variable belongs to the class itself, not it's individual instances. That's why by making the method static, you can call it directly via the class - it doesn't need to be instantiated by an object, it "exists" by virtue of the fact it's class exists. I tried to avoid this where possible - if the method I'm trying to write doesn't depend in any way on the class it'll be used with, I don't make it part of the class.

 

EDIT2: final note; I have massively simplified the concept of classes and objects here to try to make a clear explanation; there's a lot more to it when you really get in to it, but I'm certainly not the most qualified person to go any deeper in to that :)

Edited by LosOjos
Posted

Thanks @LosOjos .

Im going to go with the object route it may be overkill for the project, but hopefully with using it in something simple as this it will help me get my head around it.

 

I have also changed the class Checkfiletype to File.cs ( and renamed it everywhere) as I think I am going to add some more methods in there for working with the files.

 

Im still learning the static methods and still have a pbit of trouble with private and public, basically I'm trying to make everything private

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...