Click here to Skip to main content
15,888,527 members
Home / Discussions / C#
   

C#

 
AnswerRe: need help. No error in code but not getting output. here is my code. trying to accessing data from active directory. Pin
Richard Deeming21-Oct-15 1:48
mveRichard Deeming21-Oct-15 1:48 
QuestionWindows Remote Application is not working Pin
Josephss7318-Oct-15 22:35
Josephss7318-Oct-15 22:35 
QuestionHow should I access share folder from server Pin
Member 1201610618-Oct-15 21:10
Member 1201610618-Oct-15 21:10 
AnswerRe: How should I access share folder from server Pin
OriginalGriff18-Oct-15 23:12
mveOriginalGriff18-Oct-15 23:12 
Question[WIN2D] Randomly generated 2D drawing Pin
Member 858597418-Oct-15 14:10
Member 858597418-Oct-15 14:10 
Questionc# delegates and events Pin
Lillepige17-Oct-15 6:06
Lillepige17-Oct-15 6:06 
AnswerRe: c# delegates and events Pin
BillWoodruff17-Oct-15 6:25
professionalBillWoodruff17-Oct-15 6:25 
QuestionImplement my own IFormatProvider class Pin
Member 905073117-Oct-15 5:29
Member 905073117-Oct-15 5:29 
Hello together,

I'm trying to implement my own IFormatProvider class.
Here are two examples:
-String.Format() - works correctly as expected
-int.ToString() - doesn't work as expected!

Below is the code:
- FormatProvider for the format method:- FormatProvider für die Format-Methode:
C#
public class FormatProviderFormat : IFormatProvider, ICustomFormatter
    {
        public object GetFormat(Type formatType)
        {
            if (formatType == typeof(ICustomFormatter))
            {
                return this;
            }
            else
            {
                return null;
            }
        }

        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            switch (format)
            {
                case "A":
                    return "AA";
                case "B":
                    return "BB";
                default:
                    return "CC";
                    break;
            }
        }
    }

- The call:
DateTime dt = DateTime.Now;
IFormatProvider fpFormat = new FormatProviderFormat();
Console.WriteLine(String.Format(fpFormat, "{0:A} {0:B} {0}", dt));

- The output (makes no sense, but works as expected):
AA BB CC

Now to the problem:
-Format provider for ToString():
C#
public class FormatProviderToString: IFormatProvider, IFormattable
    {
        public object GetFormat(Type formatType)
        {
            if (formatType == typeof(IFormattable))
            {
                return this;
            }
            else
            {
                return null;
            }
        }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            switch (format)
            {
                case "A":
                    return "AA";
                case "B":
                    return "BB";
                default:
                    return "CC";
                    break;
            }
        }
    }

- The call:
int i = 123;
IFormatProvider fpToString = new FormatProviderToString();
Console.WriteLine(i.ToString("A", fpToString) + " " + i.ToString("B", fpToString) + " " + i.ToString(fpToString));

- The output:
Exception 'format specifier which invalid."

I don't understand what I am doing wrong.
Do you have a tip, an example or a corrected version for me?

Thanks for the help.
I'm a german Speaker. I hope you understand my question.
Regards, Manfred

modified 17-Oct-15 13:52pm.

SuggestionRe: Eigene IFormatProvider implemetieren Pin
Jochen Arndt17-Oct-15 6:14
professionalJochen Arndt17-Oct-15 6:14 
AnswerRe: Eigene IFormatProvider implemetieren Pin
Dave Kreskowiak17-Oct-15 6:29
mveDave Kreskowiak17-Oct-15 6:29 
QuestionRe: Implement my own IFormatProvider class Pin
Maciej Los18-Oct-15 7:56
mveMaciej Los18-Oct-15 7:56 
QuestionFastest way to modify the value in the dictionary Pin
Gilbert Consellado16-Oct-15 12:51
professionalGilbert Consellado16-Oct-15 12:51 
AnswerRe: Fastest way to modify the value in the dictionary Pin
BillWoodruff16-Oct-15 21:54
professionalBillWoodruff16-Oct-15 21:54 
GeneralRe: Fastest way to modify the value in the dictionary Pin
Gilbert Consellado17-Oct-15 19:01
professionalGilbert Consellado17-Oct-15 19:01 
QuestionCreating Events with Dynamically created ComboBox(es) Pin
KakitaIppatsu16-Oct-15 11:30
KakitaIppatsu16-Oct-15 11:30 
AnswerRe: Creating Events with Dynamically created ComboBox(es) Pin
Brisingr Aerowing16-Oct-15 15:32
professionalBrisingr Aerowing16-Oct-15 15:32 
AnswerRe: Creating Events with Dynamically created ComboBox(es) Pin
BillWoodruff16-Oct-15 22:14
professionalBillWoodruff16-Oct-15 22:14 
GeneralRe: Creating Events with Dynamically created ComboBox(es) Pin
KakitaIppatsu20-Oct-15 9:02
KakitaIppatsu20-Oct-15 9:02 
AnswerRe: Creating Events with Dynamically created ComboBox(es) Pin
NeillJam18-Oct-15 22:02
NeillJam18-Oct-15 22:02 
AnswerRe: Creating Events with Dynamically created ComboBox(es) Pin
Foothill19-Oct-15 11:19
professionalFoothill19-Oct-15 11:19 
GeneralRe: Creating Events with Dynamically created ComboBox(es) Pin
Pete O'Hanlon19-Oct-15 22:52
mvePete O'Hanlon19-Oct-15 22:52 
GeneralRe: Creating Events with Dynamically created ComboBox(es) Pin
Foothill20-Oct-15 3:58
professionalFoothill20-Oct-15 3:58 
QuestionHow to resolve network related or instance specific error Pin
Member 1200209515-Oct-15 20:26
Member 1200209515-Oct-15 20:26 
AnswerRe: How to resolve network related or instance specific error Pin
OriginalGriff15-Oct-15 21:37
mveOriginalGriff15-Oct-15 21:37 
AnswerRe: How to resolve network related or instance specific error Pin
NeillJam15-Oct-15 22:16
NeillJam15-Oct-15 22:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.