CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2012
    Location
    Wisconsin
    Posts
    4

    Passing namespace name in a console for project heading, possible?

    For my class I have to have a class for my Name, Course tittle, instructor and assignment to write in the console as a heading. The assignment portion I have to pass as an argument from the main class. My question is if there is a way that I can pass the namespace through instead of having to pass the assignment name as a string. Did that make sense?

    Essentially I want to make a class that I could stick into any of my assignments and have the namespace show up as the assignment title.

    Output:

    Name: John Doe
    Course: Object Oriented Programming
    Instructor: Joseph Middleton
    Assignment: <namespace>

    If anyone could point me in the right direction or let me know if this is even possible it would be appreciated!

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Passing namespace name in a console for project heading, possible?

    Sure, do you know how to make a class? Do you know how to use Console.WriteLine?

    If so, post your code here and I'll show you how to modify it to automatically retrieve the namespace.

    Btw, great idea. You're already thinking like a developer (it's called reusability).

  3. #3
    Join Date
    Sep 2012
    Location
    Wisconsin
    Posts
    4

    Re: Passing namespace name in a console for project heading, possible?

    I do know how to make a class and use System.WriteLine. I also have limited knowledge on calling class object
    methods to be executed in the Main( ).

    //**Using visual studio 2010 C# console app
    //**This is a program that is to display my information on the top using a call to Class "Id"
    //**The assignment requires I send "Project" and "2" through as separate arguments
    //**I want the namespace to automatically fill in the assignment portion
    //**DataFun is just a Class that asks for the first 4 letters of your name
    //** and displays them as bin, hex, oct and char
    //**I didn't think that class would be need to fix the "Id" class

    //**************MainApp class file*****************

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


    namespace Project_2
    {
    class MainApp
    {
    static void Main(string[] args)
    {
    string projectNm = "Project";
    string projectNmbr= "2";


    Id id = new Id(projectNm, projectNmbr);
    DataFun dataFun = new DataFun();

    dataFun.LoadName();
    dataFun.DisplayName();

    Console.ReadLine();
    }
    }

    //************************Id class file*************************

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

    namespace Project_2
    {
    class Id
    {
    public Id(string prjt, string prjtNum)
    {

    Console.WriteLine("My Name: John Doe");
    Console.WriteLine("Course Name: Introduction to Object Oriented Programming");
    Console.WriteLine("Instructor: Joseph Middlebottom");
    Console.WriteLine("Assignment: {0} {1}", prjt, prjtNum); *\
    Console.WriteLine("Today's Date: 09/20/12");
    Console.WriteLine("\n\n\n");

    }
    }
    }

    Thanks for taking a look at it!

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Passing namespace name in a console for project heading, possible?

    You can do this with reflection. As a warning you have to be careful with using the stackframe index value. For you example 3 works well, but keep in mind that if you change the dept of how you call the Id class, the code may break.

    Code:
    sing System;
    using System.Diagnostics;
    using SomeOtherNamespace;
    
    namespace Project_2 //CG.DynamicNamespace
    {
        class Program
        {
            static void Main( string [ ] args )
            {
                Id.LogAssignmentHeader();
    
                //
                // insert class work
                //
    
                Console.ReadLine();
            }
        }
    }
    
    namespace SomeOtherNamespace
    {
        class Id
        {
            /// <summary>
            /// Use a static factory method to make calling this class a one-liner
            /// </summary>
            /// <returns></returns>
            public static Id LogAssignmentHeader( )
            {
                return new Id(); 
            }
    
            public Id()
            {
                Console.WriteLine(String.Format( "{0,-14}{1}", "My Name:", "John Doe") );
                Console.WriteLine(String.Format( "{0,-14}{1}", "Course Name:", "Introduction to Object Oriented Programming" ));
                Console.WriteLine(String.Format( "{0,-14}{1}", "Instructor:", "Joseph Middlebottom" ));
                Console.WriteLine(String.Format( "{0,-14}{1}", "Assignment:", GetCallingNamespace() ));
                Console.WriteLine(String.Format( "{0,-14}{1}", "Today's Date:", "09/20/12" ));
                Console.WriteLine( "\n\n\n" );
            }
    
            /// <summary>
            /// Returns the Namespace of the class from the calling method (outside the Id class)
            /// 
            /// Experiment with the 3 value of the stack frame (try 1 and 2 to see what happens)
            /// </summary>
            /// <returns></returns>
            public string GetCallingNamespace()
            {
                var ns = new StackFrame( 3, false ).GetMethod( ).ReflectedType.FullName;
                return ns.Substring( 0, ns.LastIndexOf('.'));
            }
        }
    }
    Output:
    Code:
    My Name:      John Doe
    Course Name:  Introduction to Object Oriented Programming
    Instructor:   Joseph Middlebottom
    Assignment:   Project_2
    Today's Date: 09/20/12

  5. #5
    Join Date
    Sep 2012
    Location
    Wisconsin
    Posts
    4

    Re: Passing namespace name in a console for project heading, possible?

    It worked! I did try throwing the "Id" class file into a new program and it seems as though I need to update the Id classes namespace for each new program. Still does the trick and it's giving me a lot to read up on to figure out exactly what it's doing but at least now I know where to look!

    Thank you so much for your help! I really appreciate the time you took to help such a silly question. This is going to help me understand a lot more once I get into reading about system.diagnostic methods.

    Thanks again

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Passing namespace name in a console for project heading, possible?

    Quote Originally Posted by dvrobiqu View Post
    It worked! I did try throwing the "Id" class file into a new program and it seems as though I need to update the Id classes namespace for each new program.
    You don't really need to update the Id class namespace, but what you need to do (at the very least) is put a
    Code:
    using SomeOtherNamespace;
    statement in the file where you are referencing the Id class. In fact the Id class namespace can be anything as long as you reference it with a using block.

  7. #7
    Join Date
    Sep 2012
    Location
    Wisconsin
    Posts
    4

    Re: Passing namespace name in a console for project heading, possible?

    Worked like a charm! Thanks again.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured