|
-
May 15th, 2006, 12:46 AM
#1
Thread Starter
Hyperactive Member
Converting Function from VB.NET - C#
Anyone know how to make this function work?
This is the function that i made a while back in VB.NET(Works) that writes to an RTB (rich Text Box).
VB Code:
Public Sub AddChat(ByVal ParamArray aT() As Object)
Dim i As Integer
With Chat
.SelectionStart = .Text.Length
.SelectionLength = 0
End With
For i = 0 To UBound(aT) Step 2
With Chat
.SelectionColor = aT(i)
.SelectedText = aT(i + 1)
End With
Next i
With Chat
.SelectionLength = 0
.SelectionStart = .Text.Length
.SelectedText = Microsoft.VisualBasic.ControlChars.CrLf
End With
End Sub
to use the function in VB.NET
it would be:
VB Code:
AddChat(Color.White, "Blah Blah Blah")
How would this look in C#?
Last edited by PlaGuE; May 15th, 2006 at 01:13 AM.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
May 15th, 2006, 12:57 AM
#2
Re: Converting Function from VB.NET - C#
There are links to code converters in my signature. Note that C# has no equivalent to the With key word. Also, you should use aT.GetUpperBound(0) instead of UBound (which you should in VB.NET too if you ask me). Finally, the escape sequence "\r\n" is the equivalent of CrLf.
-
May 15th, 2006, 01:27 AM
#3
Thread Starter
Hyperactive Member
Re: Converting Function from VB.NET - C#
--EDIT-- Forgot to add the conversion that i made...(Not feelin well..Hada go to the latrine..hehe... so i musta hit post...sorry)
Using
VB Code:
public void AddChat(params object[] aT)
{
int i;
Chat.SelectionStart = Chat.Text.Length;
Chat.SelectionLength = 0;
for (int i = 0; i <= UBound(aT); i += 2) {
Chat.SelectionColor = aT(i);
Chat.SelectedText = aT(i + 1);
}
Chat.SelectionLength = 0;
Chat.SelectionStart = Chat.Text.Length;
Chat.SelectedText = Microsoft.VisualBasic.ControlChars.CrLf;
}
using http://www.developerfusion.co.uk/uti...btocsharp.aspx
EDIT#2::
Whats wrong with this?
VB Code:
public void AddChat(params object[] aT)
{
int i;
rtf_Chat.SelectionStart = rtf_Chat.Text.Length;
rtf_Chat.SelectionLength = 0;
for (int i = 0; i <= UpperBound(aT); i += 2)
{
rtf_iRC_Chat.SelectionColor = aT(i);
rtf_iRC_Chat.SelectedText = aT(i + 1);
}
rtf_Chat.SelectionLength = 0;
rtf_Chat.SelectionStart = rtf_Chat.Text.Length;
rtf_Chat.SelectedText = Microsoft.VisualBasic.ControlChars.CrLf;
}
i also tried
VB Code:
public void AddChat(params object[] aT)
{
int i;
rtf_wnd.rtf_Chat.SelectionStart = rtf_wnd.rtf_Chat.Text.Length;
rtf_wnd.rtf_Chat.SelectionLength = 0;
for (int i = 0; i <= UpperBound(aT); i += 2)
{
rtf_wnd.rtf_iRC_Chat.SelectionColor = aT(i);
rtf_wnd.rtf_iRC_Chat.SelectedText = aT(i + 1);
}
rtf_wnd.rtf_Chat.SelectionLength = 0;
rtf_wnd.rtf_Chat.SelectionStart = rtf_Chat.Text.Length;
rtf_wnd.rtf_Chat.SelectedText = Microsoft.VisualBasic.ControlChars.CrLf;
}
but rtf_Chat had a protection error.
What to do?
Sorry if i sound noobish...
Last edited by PlaGuE; May 15th, 2006 at 01:36 AM.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
May 15th, 2006, 01:36 AM
#4
Re: Converting Function from VB.NET - C#
When you use an array in a for loop in C# don't use the upper bound with a "<=", rather use the length with a "<". Also, when you index things like arrays in C# you use square brackets, not parentheses.
Code:
for (int i = 0; i < aT.Length; i += 2)
{
rtf_wnd.rtf_iRC_Chat.SelectionColor = aT[i];
rtf_wnd.rtf_iRC_Chat.SelectedText = aT[i + 1];
}
I must say too, that using an object paramarray as you have is not a good idea when each parameter has a specific meaning. There is no guarantee whatsoever that every the first parameter will be a colour and the second text and so on. That is, in my opinion, a flawed way to pass arguments.
-
May 15th, 2006, 01:38 AM
#5
Thread Starter
Hyperactive Member
Re: Converting Function from VB.NET - C#
It was my first function... Was learning...
Now i know.
I should read up more n fix it... Cuz im still learnin C#
Last edited by PlaGuE; May 15th, 2006 at 01:57 AM.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
May 15th, 2006, 06:47 AM
#6
Re: Converting Function from VB.NET - C#
"int i" is declared twice.
I don't live here any more.
-
May 29th, 2006, 01:33 AM
#7
Thread Starter
Hyperactive Member
Re: Converting Function from VB.NET - C#
Sorry for bumpin this... seemingly dead topic... but... i finally was able to get back to it.
okay i took out the first declaration of "int i" in the for loop.
which gives me this.
VB Code:
public void AddChat(params object[] aT)
{
rtf_Chat.SelectionStart = rtf_Chat.Text.Length;
rtf_Chat.SelectionLength = 0;
for (int i = 0; i < aT.Length; i += 2)
{
rtf_Chat.SelectionColor = aT[i];
rtf_Chat.SelectedText = aT[i + 1];
}
rtf_Chat.SelectionLength = 0;
rtf_Chat.SelectionStart = rtf_Chat.Text.Length;
rtf_Chat.SelectedText = Microsoft.VisualBasic.ControlChars.CrLf;
}
That takes care of that error.
which was....
VB Code:
Error 1
A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i',
which is already used in a 'parent or current' scope to denote something else
the only two errors left deal with the aT object.
VB Code:
Error 2
Cannot implicitly convert type 'object' to 'System.Drawing.Color'.
An explicit conversion exists (are you missing a cast?)
VB Code:
Error 3
Cannot implicitly convert type 'object' to 'string'.
An explicit conversion exists (are you missing a cast?)
I still cant figure out whats wrong.
Last edited by PlaGuE; May 29th, 2006 at 01:37 AM.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
May 29th, 2006, 01:41 AM
#8
Re: Converting Function from VB.NET - C#
This might help you; it has more features than your function and doesn't require that every other argument be a color. I wrote it a while ago.
Code:
public void WriteTextF(params object[] o)
{
Type tCurrent;
Color cCurrent = Color.White;
Font fCurrent = new Font("Courier New",8.25f);
for(int i = 0; i < o.Length; i++)
{
tCurrent = o[i].GetType();
if (tCurrent == typeof(string))
{
// write text to textbox
RTB.SelectionStart = RTB.Text.Length;
RTB.SelectionLength = 0;
RTB.SelectionFont = fCurrent;
RTB.SelectionColor = cCurrent;
RTB.SelectedText = (string)(o[i]);
RTB.SelectionStart = RTB.Text.Length;
}
//update color choice
else if (tCurrent == typeof(Color))
{
cCurrent = (Color)(o[i]);
}
//update font choice
else if (tCurrent == typeof(Font))
{
fCurrent = (Font)(o[i]);
}
// bad type! bad!
else
{
throw new Exception("Unsupported Type");
}
}
}
// usage:
Font f = new Font("Tahoma",12);
WriteTextF("White courier",Color.Gold,"gold courier",f, "gold tahoma", "more gold tahoma", Color.Red, "red tahoma.");
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
May 29th, 2006, 01:41 AM
#9
Re: Converting Function from VB.NET - C#
What's wrong is that references of type object cannot be implicitly converted to references of any other type because the compiler has no guarantee whatsoever that they will refer to an object of the specified type. I told you that you should pass objects of the specific type that you want to use. If you choose to ignore that advice then you need to explicitly convert the object references to the types you want using casts. By doing so you are telling the compiler that you do guarantee that the references will refer to objects of the type that you say. If the app crashes because they weren't then it's your fault and not the compiler's. Bascially it's bad practice to use a more general type than you intend to use without good reason. The fact that you don't want to rewrite the function to take objects of the correct types through two separate arrays, a keyed collection or your own custom type doesn't strike me as a good reason.
-
May 29th, 2006, 01:59 AM
#10
Thread Starter
Hyperactive Member
Re: Converting Function from VB.NET - C#
 Originally Posted by jmcilhinney
What's wrong is that references of type object cannot be implicitly converted to references of any other type because the compiler has no guarantee whatsoever that they will refer to an object of the specified type. I told you that you should pass objects of the specific type that you want to use. If you choose to ignore that advice then you need to explicitly convert the object references to the types you want using casts. By doing so you are telling the compiler that you do guarantee that the references will refer to objects of the type that you say. If the app crashes because they weren't then it's your fault and not the compiler's. Bascially it's bad practice to use a more general type than you intend to use without good reason. The fact that you don't want to rewrite the function to take objects of the correct types through two separate arrays, a keyed collection or your own custom type doesn't strike me as a good reason.
@ Sunburnt: Thanks for the function. It's exactly How my function worked in VB.Net... Exept it was barebones....
Thanks.I'll be usin it as what its meant to be... A learning tool.
@jmcilhinney:
I told you that you should pass objects of the specific type that you want to use. If you choose to ignore that advice then you need to explicitly convert the object references to the types you want using casts.
I am still new to this [Desktop Programming]... So I didnt understand what you were saying.Because i didnt know what i had to do. Nore could i find any refrences to that on the web... where i learn this stuff... cuz atm i cant get "books"...Sorry.
Last edited by PlaGuE; May 29th, 2006 at 02:30 AM.
Without balance, there could only be chaos.
Without chaos, there could be no balance.
I live with karma. Eat with destiny. Dream of life without shackles....
Yet. If life had no consequences, life could not exist, nor could it flourish.
If at first you dont succeed.You're screwed.
C++/Java NOOB.
I aint a professional at PHP, but if i can help i will.
-
May 29th, 2006, 02:24 AM
#11
Re: Converting Function from VB.NET - C#
If you are passing Color objects and only Color objects then your arguments should reflect that. If you're passing pairs of Color and String object sthen your arguments should refelect that. You'd have a number of choices. Two arguments of type Color[] and String[], one argument of type Dictionary(Of Color, String) or an array or collection of a custom type that has a Color property and a String property. All three of those ways will ensure that you get the correct types passed to your method in the correct ratios. The only thing you'd have to checkfor is that your Strings weren't null references.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|